Docker for DevOps Engineers - Part 2
Docker-Volume
When you run a Docker container, any data that's stored inside the container is lost when the container is stopped or removed. To make the data persistent, you can use Docker volumes. A Docker volume is like a folder on your computer that can be accessed by one or more Docker containers. You can create a Docker volume using the docker volume create
a command and then use it to store data that needs to be persisted.
Docker volumes are a useful way to manage persistent data in Docker containers and can be especially helpful when working with complex applications that require data to be stored across multiple containers.
Docker Network
Docker Network is a feature in Docker that enables the communication between containers running on the same host or different hosts. With Docker Network, you can create virtual networks to isolate containers and allow them to communicate securely.
Bridge network: The default network that allows containers to communicate with each other on the same host.
Host network: This network allows a container to use the host's networking stack, which means that the container does not have its own IP address.
Overlay network: This network is used to connect containers running on different hosts, and it is commonly used in container orchestration systems like Docker Swarm or Kubernetes.
MACVLAN network: This network enables containers to have their own MAC address and IP address, making them appear as physical hosts on the network.
You can create a Docker network using the docker network create
command and specify the network type and other parameters. Once the network is created, you can attach containers to it using the docker network connect
command.
Task-1
Create a multi-container docker-compose file that will bring UP and bring DOWN containers in a single shot (Example - Create application and database container)
Below is the yaml file for django-todo-app,
To bring up the containers, navigate to the directory containing this
docker-compose.yml
file in a terminal and run:docker-compose up -d
Check the container status using :
docker ps
To increase or decrease the number of replicas for a specific service using the
docker-compose scale
command, you can use the following syntax:docker-compose up -d
docker-compose scale web=3
Note that the
docker-compose scale
command only works for services defined in thedocker-compose.yml
file.To bring down the containers, run:
docker-compose down
This will stop and remove the containers, but will not remove the
db_data
volume. If you want to remove the volume as well, you can use the-v
flag:docker-compose down -v
Task-2
Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.
To create a named volume, you can use the
docker volume create
command:Create two or more containers that read and write data to the same volume using the
docker run --mount
command.This is for django app.
This is for node-todo app,
Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.
Here our data is safe in volume even if our container gets deleted.
Use the docker volume ls command to list all volumes and docker volume rm command to remove the volume when you're done.
Note that you cannot remove a named volume that is currently in use by a container. You will need to stop and remove all containers that are using the named volume before you can remove it.
docker volume rm apps-volume
Thank you for reading this blog!!