Day 19 Task: Docker for DevOps Engineers
Table of contents
🔹Docker-Volume
Docker volumes are a way to manage and persist data outside of a container. In the world of containers, where containers can be created and destroyed, having a way to store and share data beyond the container's lifecycle is important. Volumes allow you to store data separately from the container filesystem, ensuring that the data persists even if the container is removed.
Example:
Imagine you have a web application running in a Docker container, and you want to ensure that the user-uploaded images or files are not lost when the container is stopped or updated. This is where Docker volumes come into play.
Let's say you have a simple web app that allows users to upload images. You can create a Docker volume named "uploads" and mount it to the container's filesystem path where the uploaded images are stored. This way, even if you stop or update the container, the uploaded images will remain in the "uploads" volume.
Commands to Create and Use a Volume:
docker volume create uploads
🔹Docker Network
Think of Docker networks as virtual highways connecting different containers within your Docker environment. Just like real-life highways connect various cities, Docker networks connect different containers, enabling them to communicate and share information with each other.
Example:
Imagine you have a microservices-based application where each service is a separate Docker container. These services need to communicate with each other to perform tasks. Docker networks help these containers talk to one another, just like how different teams in an office need to collaborate to get work done.
here are examples of different types of Docker networks:
1. Bridge Network (Default): Think of a bridge network as a virtual office floor where different teams have their own designated space. Each team can work independently, but they can also collaborate when needed. Similarly, containers in a bridge network can communicate with each other, and they are isolated from containers in other networks.
Example: You have multiple applications like HR, Sales, and Marketing. You create a bridge network for each department. Containers within the same network can easily interact, but they are separate from containers in other networks.
docker run -d --network=bridge -p 80:80 nginx
2. Host Network: magine a co-working space where different companies share a common area. Here, containers are directly connected to the host's network, just like companies connecting to the shared space's resources.
Example: You have containers that need direct access to your host machine's network, like monitoring tools. By using the host network, these containers can access resources without additional network setup.
docker run -d --network=host nginx
3. Overlay Network: Picture a company with multiple branch offices located in different cities. Each office operates independently, but they need to communicate seamlessly. Overlay networks connect containers across different Docker hosts, just like branches of a company staying connected.
Example: You're running containers on different servers across multiple locations. Overlay networks enable containers to communicate across servers as if they're on the same network.
docker network create -d overlay my_overlay_network
docker service create --network=my_overlay_network --name=web nginx
4. Macvlan Network: Think of a macvlan network as assigning a dedicated phone number to each employee in an office. Similarly, each container in a macvlan network gets a unique MAC address and can be assigned an IP address like an individual entity.
Example: You want each container to have its own IP address, as if they were separate physical machines. Macvlan networks are useful when you need containers to appear as separate devices on the network.
docker network create -d macvlan --subnet=192.168.1.0/24 --gateway=192.168.1.1 -o parent=eth0 my_macvlan_network
docker run -d --network=my_macvlan_network --name=container1 nginx
5. None Network: Imagine you have a team working remotely from different locations. The "none" network is like isolating each team member in their own individual workspace with no direct communication between them.
Example: You want to run a container in complete isolation from any network. This can be useful for certain security-sensitive applications that shouldn't have any network access.
docker run -d --network=none nginx
6. Custom Bridge Network: Think of a custom bridge network as creating your own unique collaboration space within a co-working building. You define the rules for communication and interaction, allowing certain members to connect while keeping others separate.
Example: You need fine-grained control over communication between containers. You create a custom bridge network, add specific containers to it, and configure the network rules according to your requirements.
docker network create my_bridge_network
docker run -d --network=my_bridge_network --name=container1 nginx
docker run -d --network=my_bridge_network --name=container2 nginx
7. IPvlan Network: Imagine you have a large apartment building with multiple units, and each unit has its own separate entrance and address. The "ipvlan" network is similar to creating individual addresses for each unit within the same physical building.
In Docker, the ipvlan
network type allows you to create multiple virtual interfaces, each with its own unique IP address, all associated with the same physical network interface. This enables containers to have their own distinct network identities, even though they share the same underlying network infrastructure.
Example: Consider a scenario where you have a server with a single physical network interface. You want to run multiple containers on this server, each with its own IP address and network isolation, but all using the same physical network connection.
docker network create -d ipvlan --subnet=192.168.0.0/24 --gateway=192.168.0.1 -o parent=eth0 my_ipvlan_network
docker run -d --network=my_ipvlan_network --name=container1 nginx
refer: https://www.youtube.com/live/UNew_BBNVPk?feature=share
🔹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 )
docker pull gopalgtm001/django-auth:latest
version: '3.9' services: web: image: gopalgtm001/django-auth:latest command: bash -c "python3 manage.py makemigrations && python3 manage.py migrate && python3 manage.py runserver 0.0.0.0:8000" ports: - "8000:8000" environment: - DB_USER=django_user - DB_PASSWORD=django_pass - DB_NAME=django_db networks: - my_network depends_on: - db db: image: postgres:15 volumes: - db_data:/var/lib/postgresql/data/ environment: - POSTGRES_USER=django_user - POSTGRES_PASSWORD=django_pass - POSTGRES_DB=django_db networks: - my_network volumes: db_data: networks: my_network:
docker-compose up -d
To scale the number of replicas of a specific service (for example, scaling the
webapp
service to 3 replicas), you can use thedocker-compose scale
command:docker-compose up -d docker-compose scale web=3
To view the status of the containers:
docker-compose ps
To view the logs of a specific service (e.g.,
webapp
):docker-compose logs web
And finally, to stop and remove all containers, networks, and volumes associated with the application:
docker-compose down
🔹Task-2
Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.
Create two or more containers that read and write data to the same volume using the
docker run --mount
command.Create a Named Volume: Create a named volume using the
docker volume create
command. For example:docker volume create mydata
Create Containers with Shared Volume: Create two or more containers that share the same named volume using the
docker run --mount
command. Here's an example:docker run -d --name container1 --mount source=mydata,target=/app image_name docker run -d --name container2 --mount source=mydata,target=/app images_name
Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.
Verify Shared Data: You can use the
docker exec
command to run commands inside each container to verify that they are using the same volume. For instance:docker exec -it container1 sh ls /app
Write and Read Data: Inside each container, you can write and read data to/from the shared volume:
echo "Hello from container1" > /app/data.txt
Check Data in Other Containers: After writing data in one container, check if the data is visible in the other container:
docker exec -it container2 sh cat /app/data.txt
Use the docker volume ls command to list all volumes and docker volume rm command to remove the volume when you're done.
List and Remove Volumes: You can list all volumes using the
docker volume ls
command:docker volume ls
Remove Volume: When you're done, you can remove the named volume using the
docker volume rm
command:docker volume rm mydata
This task demonstrates how Docker Volumes and Named Volumes can be used to share files and directories between multiple containers. The named volume
mydata
is created, and two containers (container1
andcontainer2
) are connected to it. Data written in one container is visible in the other due to the shared volume. Finally, the volume is removed when you no longer need it.
Subscribe to my newsletter
Read articles from Gopal Gautam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Gopal Gautam
Gopal Gautam
Hii I am a backend/DevOps engineer.I have a experience with development and automation.I mostly work with Python, django, Cloud based technologies.