Table of contents
- Docker-Volume
- Advantages:
- Types of Docker Volume
- Docker Network
- Task 1:-
- Create a multi-container docker-compose file which will bring UP and bring DOWN containers in a single shot
- Use the docker-compose scale command to increase or decrease the number of replicas for a specific service.
- Use the docker-compose ps command to view the status of all containers
- docker-compose logs to view the logs of a specific service.
- Task 2:-
- Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.
- Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.
- Create two or more containers that read and write data to the same volume using the docker run --mount command.
- list down all the volumes and to remove a particular volume.
Docker-Volume
Images are a series of read-only layers
A container is merely an instantiation of those read-only layers with a single read-write layer on top.
Any file changes that are made within a container are reflected as a copy of modified data from the read-only layer.
The version in the read-write layer hides the underlying file but does not remove it.
When deleting a container, the read-write layer containing the changes is destroyed and gone forever!
To persist these changes we use docker volumes
Advantages:
To keep data around when a container is removed
To share data between the host filesystem and the Docker container
Types of Docker Volume
Two types of volume mounts: Named and Bind
- Named Volume: Mounting a volume created using the ‘docker volume create command and mounting it from default volume location /var/lib/docker/volumes
Below command to create a named volume
docker volume create my-vol
docker run -d --name nginx -v myvol:/app nginx
docker run -d --name nginx --mount source=myvol2,target=/app nginx
- Bind Volume: External mounting(external hard disks etc.) Bind mounts may be stored anywhere on the host system. They usually start with ‘/’
Below command to create a bind volume
docker run –name web -v /root/html:/var/www/html/ nginx
Docker Network
Connecting containers is very important for most of the applications out there. A classic web application consists of at least a web server, an application server and a database server. The web server needs to talk to the application server and the application server would need to talk to the database server. A legacy, but popular way to do so is via passing --link flag to the docker run command.
Docker gives 3 default networks: bridge, none and host
When you start Docker, a default bridge network (also called a bridge) is created automatically, and newly started containers connect to it unless otherwise specified.
Using the below command we will get the default network list
docker network ls
Using the below command we can see in the details
docker network inspect bridge
Bridge:-
In Bridge network, all containers get private internal IPs and they are isolated from host.
Port forwarding forwards outside traffic to the containers.
Containers on the default bridge network can only access each other by IP addresses, unless you use the link option, which is considered legacy.
You can also create user defined custom bridge network
User defined bridge networks are superior to the default bridge network.
On a user defined bridge network, containers can resolve each other by name or alias(DNS)
Create a bridge network:-
docker network create --driver bridge my-net
Attach a container to it:-
sudo docker run -itd --name con5 --network my-net alpine ash
# Here we are using alpine beacuse lite image
When containers are run in the default bridge network they cannot find each other using their container names. Simply put, DNS resolution through container names will not work under the default bridge network
In the above screenshot, you can see if we ping through IP address then we can communicate with another container but if we ping the container name then we can not communicate with another container and we are receiving the error "Bad address"
Now a new bridge network is created and containers are attached to that network. In this case, containers find each other using their container names( DNS resolution through container names
After creating a custom bridge container we can see above screenshot that we can ping through the IP address and we can ping using the container name also
Host:-
In host network, all containers directly get connected to host. Multiple containers cannot run on same hosts because of port conflicts on host side
Need to run the below command using the host network
docker run -d --name web -net host nginx
None:-
This offers a container-specific network stack that lacks a network interface.
Containers run in pure isolation
This container only has a local loopback interface (i.e., no external network interface)
Need to run the below command using the host network
docker run -d --name web --net none nginx
Overlay :-
Bridge networks apply to containers running on the same Docker daemon host. For communication among containers running on different Docker daemon hosts, we should use an overlay network which spans across the entire cluster github.
Task 1:-
Create a multi-container docker-compose file which will bring UP and bring DOWN containers in a single shot
We have to modify the docker-compose file
version : "3.3"
services:
web:
image: nginx:latest
ports:
- "80:80"
db:
image: mysql
ports:
- "3306:3306"
environment:
- "MYSQL_ROOT_PASSWORD=test@123"
- Now we can start the container by using the below command
docker-compose up -d
Use the
docker-compose scale
command to increase or decrease the number of replicas for a specific service.
docker-compose up -d --scale web=4 --scale db=3
Use the
docker-compose ps
command to view the status of all containers
docker-compose ps
docker-compose logs
to view the logs of a specific service.
docker-compose logs
Use the
docker-compose down
command to stop and remove all containers, networks, and volumes associated with the applicationdocker-compose down
Task 2:-
Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.
First, we need to create a volume
docker volume create my-vol
Now we need to create a different container using the same volume
docker run -d --name con1 -v my-vol:/app nginx:latest
docker run -d --name con3 -v my-vol:/app nginx:latest
Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.
Now we need to go to inside any conatiner and we have created sample file and exit
docker exec -it con3 bash
echo "Creating sample text file for docker volume" > /app/hello.txt
exit
Now if we go to the next container which is con3 and if we search the same file name. The file should be refect here
Create two or more containers that read and write data to the same volume using the
docker run --mount
command.
We need to create a new volume
docker volume create mine-volume
Now we are creating conatiner using --mount and same volume
docker run -itd --name myapp --mount source=mine-volume,target=/my_data alpine ash
docker run -itd --name myphapp --mount source=mine-volume,target=/my_data alpine ash
Now we need to go to myapp conationer and need to create text file and exit
echo "Creating same text file from myapp container" > /my_data/Hello.txt
Now we need to use below command verify from second conationer
cat /my_data/Hello.txt
list down all the volumes and to remove a particular volume.
# this is use to check the volume list
docker volume ls
# This is use for to remove volume
docker volume rm <volume_name>
# If you are getting error removing volume try the below coomand
docker system prune
Thank you for reading!! I hope you find this article helpful!!
if any queries or corrections to be done to this blog please let me know.
Happy Learning!!
Saikat Mukherjee