Table of contents
Introduction
Till now we have learned how to create docker-compose.yml file and pushed it to the Repository. Let's move forward and dig more on other Docker-compose.yml concepts.
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.
Docker provides several types of networks
Bridge Network
Host Network
Overlay Network
Macvlan Network
None Network
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 )
Let us set up a multi-container Docker Compose file that brings up an application container and a database container, allowing you to start and stop them together.
Here is a docker-compose.yml
file:
version: '3'
services:
app:
image: nginx:latest
ports:
- "80:80" # Map port 80 in the container to port 80 on the host
depends_on:
- db # Make sure the database container is started first
db:
image: mysql:latest
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root123
In the above example I use nginx webserver along with mysql database
Use the docker-compose up
command with the -d
flag to start a multi-container application in detached mode.
docker-compose up -d
This command will start both the application and database containers in the background.
Use the docker-compose scale
command to increase or decrease the number of replicas for a specific service. You can also add replicas
in deployment file for auto-scaling.
To scale the app
service to have, for example, 3 replicas, you can run the following command:
docker-compose up -d --scale app=3
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.
Use the docker-compose down
command to stop and remove all containers, networks, and volumes associated with the application
docker-compose down
This command will stop and remove the containers, as well as any networks and volumes defined in the docker-compose.yml
file.
Task-2
Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.
Create a Volume: You can create a named volume using the docker volume create
command.
docker volume create mydata
Mount the Volume in Containers:
When running containers, you can mount the named volume using the -v
or --volume
option. This makes the data in the volume accessible to the containers. For example, if you have two containers that need to share data, you can use the same named volume for both:
docker run -d -v mydata:/app/data container1-image
You can inspect and manage named volumes using the docker volume
commands. For example, to see information about the mydata
named volume:
docker volume inspect mydata
Create two or more containers that read and write data to the same volume using the docker run --mount
command.
First, let's create a named volume using the following command:
docker volume create mysharedvolume
Run Containers with Shared Volume:
Now, we'll run two containers that read and write data to the shared volume:
# Run the first container
docker run -d --name writer-container --mount source=mysharedvolume,target=/app/data busybox sh -c "echo 'Hello from Writer' > /app/data/message.txt && sleep infinity"
# Run the second container
docker run -d --name reader-container --mount source=mysharedvolume,target=/app/data busybox sleep infinity
Verify that the data is the same in all containers by using the docker exec
command to run commands inside each container.
We'll use the docker exec
command to verify that the data is the same in both containers:
# Verify data in the writer container
docker exec writer-container cat /app/data/message.txt
# Verify data in the reader container
docker exec reader-container cat /app/data/message.txt
Now both containers should display the same "Hello from Writer" message
Use the docker volume ls command to list all volumes and docker volume rm command to remove the volume when you're done.
Clean Up: Once you're done, stop and remove both containers:
docker container stop writer-container reader-container
docker container rm writer-container reader-container
Then, remove the named volume:
docker volume rm mysharedvolume
This demonstrates how Docker volumes allow multiple containers to share and maintain data integrity.
Happy Learning!