Docker Network, Docker Compose & Docker Volumes
#90 Days of DevOps Challenge #Day 19 Task #TrainWithShubam
Docker Networking:
It is used to connect the Docker Containers
We can create a Network using the command
docker create network network-name
Types of Docker Networking:
Bridge: It is the default network created by docker to communicate between containers that are running on the same host.
Host: The container will not have any isolation or separate network it uses the Host network to communicate.
Overlay: It is used to communicate between two or more docker daemons i.e., two separate nodes It is mainly used in a docker swarm.
IPvlan: IPvlan networks give users total control over both IPv4 and IPv6 addressing.
macvlan: Macvlan networks allow you to assign a MAC address to a container, making it appear as a physical device on your network.
None: For this container, disable all networking. Usually used in conjunction with a custom network driver. none is not available for swarm services.
Docker Compose:
Docker Compose is a tool that was developed to help define and share multi-container applications.
We will use yaml file to define services
We can Automate the Containers using docker-compose.
docker-compose is a tool and we should install it.
Task 1
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 )
Sample docker-compose file (docker-compose.yaml)
In this file, I have mounted volumes.
build an image and created a container.
created container using docker hub Image.
version: '3.9' volumes: node-todo-volume: external: true services: #service 1 web: container_name: todo #building image from local docker file. build: . ports: - 8000:8000 volumes: #dockervolume:containerpath - node-todo-volume:/app #service 2 database: container_name: mysql #pulling image from docker hub. image: mysql:latest ports: - 3306:3306 environment: MYSQL_ROOT_PASSWORD: "test@123"
Use the docker-compose up command with the -d flag to start a multi-container application in detached mode.
docker compose up -d
Use the docker-compose scale command to increase or decrease the number of replicas for a specific service. You can also add replicas in the deployment file for auto-scaling.
#increasing replicas of web using cli docker compose up --scale web=3 -d
#Adding deploy tag in yaml file version: '3.9' volumes: node-todo-volume: external: true services: web: build: . ports: - 8000-8004:8000 deploy: replicas: 3 volumes: - node-todo-volume:/app
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 Volume:
Docker Volumes are used to persist the data from the container to the local host and mount it.
We can create a container with saved volume so that we can start the container from the same state where it has stopped.
There are two types of volumes:
Anonymus Volumes: These volumes are created automatically when we run a container but when we remove the container the volume will also delete automatically.
Named Volumes: These volumes are created by us to a specified path in this scenario if we remove the container also the data will be persisted.
Task 2
Create two or more containers that read and write data to the same volume using the docker run --mount command.
#creating volume docker volume create --name todo-volume --opt device=/home/ubuntu/projects/volumes/node-volume/ --opt o=bind --opt type=none
Attaching two containers to the same volume in the below Image
Interchanging Containers Creating files and checking if the files are syncing
Removing Docker Volumes
Congratulations we completed Docker Networking, Volumes and Compose ๐๐๐๐.