Table of contents
Docker-Volume
Docker allows you to create something called volumes. Volumes are like separate storage areas that can be accessed by containers. They allow you to store data, like a database, outside the container, so it doesn't get deleted when the container is deleted. You can also mount from the same volume and create more containers having same data.
Docker Network
Docker allows you to create virtual spaces called networks, where you can connect multiple containers (small packages that hold all the necessary files for a specific application to run) together. This way, the containers can communicate with each other and with the host machine (the computer on which the Docker is installed). When we run a container, it has its own storage space that is only accessible by that specific container. If we want to share that storage space with other containers, we can't do that.
Task-1
Create a multi-container docker-compose file which will bring UP and bring DOWN containers in a single shot ( Example - Create application and database container )
Use the docker-compose up
command with the -d
flag to start a multi-container application in detached mode.
For this lets clone the code of an app from Github first
For that, copy the link of repo from GitHub:
In our Linux machine, use command
git clone <repo link>
The repo is now cloned on local machine
cd django-todo-cicd
Since, we are creating a docker-compose file from scratch, deleting the docker-compose.yml file.
Now lets create a docker-compose.yaml file from scratch
vim docker-compose.yaml
Now write the following code:
version : "3.3"
services :
my_web_app:
container_name: "django-todo-app"
build: .
ports:
- 8000:8000
volumes:
- django-todo-volume:/app
my_db:
container_name: "django-mysql-db"
image: mysql:5.7
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: "test@123"
volumes:
django-todo-volume:
Now use command:
docker-compose up -d
Use the docker-compose ps
command to view the status of all containers, and docker-compose logs
to view the logs of a specific service.
Now check whether container is running using docker-compose ps
Thus multiple containers are now created using a single docker compose file
Use docker-compose
logs to check the logs
Use the docker-compose down
command to stop and remove all containers, networks, and volumes associated with the application
Now, to stop the containers, use command
docker-compose down
To check whether they are stopped use command
docker ps
The containers are now stopped.
Task-2
Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.
Create a diectory for volumes using
mkdir volumes
Now create a docker volume using
sudo docker volume create my_volume
Inspect the volume using
docker volume inspect my_volume
Create two or more containers that read and write data to the same volume using the docker run --mount
command.
sudo docker run -d -p 8000:8000 --mount source=my_volume,target=/app django-todo-app:latest
sudo docker run -d -p 8001:8001 --mount source=my_volume,target=/app django-todo-cicd_my_web_app:latest
Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.
docker exec -it <cont id> sh
Use the docker volume ls command to list all volumes and docker volume rm command to remove the volume when you're done.
docker volume ls
To remove volume
docker volume rm my_volume
If it returns error saying volume in use, enter command:
docker container prune
Now try to remove volume