DockerCompose
Docker Compose is a tool that makes it easy to run multiple containers at once. It allows you to define all the containers, networks, and volumes for your application in a single "docker-compose.yml" file.
To start these services, we use the command docker-compose up
, and to stop them, docker-compose down
.
Volumes
Volumes in docker can be referred to as backups for the docker containers. Docker containers and the volumes will be in continuous sync thus in case of any failure in the container, whole data can be fetched from the volume and there would not be any loss of data.
These volumes can also be used by multiple containers at the same time. In short, Docker Volumes is a way to share storage between different containers, making it easy to share data and store persistent information that needs to survive even if the container is stopped.
To create a docker volume by the name node-todo
in the folder /home/ubuntu/node-todo-cicd/volumes/node-todo
which is of type none
and bind
to the host machine, the command will be as follows:
docker volume create --name node-todo_volume --opt type=none --opt device=/home/ubuntu/node-todo-cicd/volumes/node-todo --opt o=bind
To understand what is going on in the volume node_todo
, the docker volume inspect
command can be used.
docker volume inspect node-todo
Now let us understand docker-compose and volumes by creating a docker-compose file for a node app and attaching a volume to it.
I have cloned the application code from Shubham Londhe 's github account and created a docker-compose for it
Creating a docker-compose file and attaching a volume to it
- Update your system and download docker-compose
sudo apt-get update && sudo apt-get install docker-compose
- Create a file named docker-compose.yml. Add the current version and mention the services that you need in the container
version: "3.9"
services:
my-node-app # create a container1 for the application
my_db # create a cotainer2 for the database
volume # create volumes for container 1
Now, add everything that is needed to run my-node-app and attach the volume to this container
my-node-app:
container_name: "node-todo-app"
build: .
ports:
- 8000:8000
volumes:
- node_todo_volume:/app
- Next, add everything needed to run my database container
my_db:
container_name: "my_sql_db"
image: mysql:5.7
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD:"test@123"
- Declare the volume
volumes:
node_todo_volume
The docker-compose file :
version: "3.9" #the latest version of docker-compose
services:
my-node-app: # service 1
container_name: "node-todo-app" #name of the container
build: . # build the container
ports:
- 8000:8000 # bind the ports of host machine with the container
volumes:
- node_todo_volume:/app # bind node_todo_volume with /app
my_db: # service 2
container_name: "my_sql_db" # name of container
image: mysql:5.7 # image to be pulled
ports:
- 3306:3306 # bind the ports of host machine with the container
environment:
- MYSQL_ROOT_PASSWORD:"test@123" # declare environment variables required to run mu sql database
volumes:
node_todo_volume: # declare the volume
Run docker-compose up
command in the terminal:
docker-compose up
Services are created !!!! The final output is as follows: