Day 19 of #90DaysOfDevOps Challenge
Aishwarya keshri
Azure DevOps Support Engineer at Microsoft || MS Certified - AZ900 || Ex-Onclusive || NIT Raipur
Docker Volumes
When we are working on docker, the data we store gets lost when the container is destroyed. So, to create persistent storage and store the data safely, and also share the data between containers we can create docker volumes.
Docker volumes are part of the file system and are managed by docker in /var/lib/docker/volumes/ path in Linux systems.
There are two ways of storing files in docker -
One volume can be attached to multiple containers and that is how it can also be used as a file-share system between containers.
Volumes also support?volume drivers, allowing storing the data on remote hosts or cloud providers, among other possibilities.
on the other hand, Bind-mounts can be stored anywhere in the system and can be modified by non-docker processes as well. The files and directories that we are mounting need not to be existing already in the host machine, they can be created at the run time. And Bind mounts have lesser functionality than volumes.
Options:
--mount Attach a filesystem mount to the containe
--volume , -v Bind mount a volumer
There are two options -v (--volume) or --mount.
-v
Example -
docker run -d --name C1 -v myvol:/app nginx:latest\
--mount
Example -
docker run -itd?--name C1?--mount source=myvol,target=/app nginx:latest
Docker Network
To establish communication among Docker containers and with the Docker host, the Docker network is to be used. Docker's default network is bridge and it is created with the name docker0.
There are several types of network drivers -
Tasks:
Task - 1
Created a dockerfile which is built on Apache image. It will host my webpage on Apache and will expose port 80 on the container -
My webpage -
docker-compose.yml file to build the above image and then use it to create my web container and also the DB container from the public image -
version: '3.3'
services:
db:
image: mysql
container_name: mysql_db
restart: always
environment:
MYSQL_ROOT_PASSWORD: pass
web:
image: apache
build: ./webapp
depends_on:
- db
restart: always
ports:
- 8090-8095:80
First, build the image to be used in our container.
docker-compose build
The image has been built and tagged as Apache:latest.
Now run -
docker-compose up -d
Now check both the containers are running -
docker-compose scale command is now deprecated.
Use the --scale flag with the docker-compose up command.
领英推荐
Syntax:
docker-compose up -d --scale service=num
docker-compose up -d --scale web=2
I had to remove the container name and provide the range of ports since multiple containers with the same name and mapped to the same port cannot be created.
Now, verify that it has created 2 web containers.
Use the docker-compose ps command to see all the running containers by your docker-compose script.
Syntax:
docker compose ps [OPTIONS] [SERVICE...]
Use the docker-compose logs command to view the
Syntax:
docker compose logs [OPTIONS] [SERVICE...]
Task - 2
Docker Volumes:
There are 2 ways of attaching a volume to a container. Either you can map a directory from the host machine to the container using the -v flag, or you can create a volume and then attach it to an absolute path inside the container.
If the directory to which you have mapped has some data then it will be copied to the volume and if that volume is further attached to any other container then that data will be shared with that container. That is how a shared volume is created.
If the path which is mapped does not exist then it will be created by docker.
Example:
To create the volume, use-
docker volume create --name dockervol
To map the volume, use-
docker run -itd -v dockervol:/var/www/html/ --name=C1 nginx
Now let's add some files in the /var/www/html/ path in C1 container.
I will create a new container C2, with the same volume and the same data added above should reflect in Container C2.
We have verified that in the above screenshots.
docker exec -it C3 bash
Here, the -t flag gives us access to the terminal, -i flag allows us to interact with the container through the terminal.
You can also use the docker-compose.yml file to create the volume and attach it to the container.
version : "3.3
services:
? web:
? ? image: varsha0108/local_django:latest
? ? deploy:
? ? ? ? replicas: 2
? ? ports:
? ? ? - "8001-8005:8001"
? ? volumes:
? ? ? - my_django_volume:/app
? db:
? ? image: mysql
? ? ports:
? ? ? - "3306:3306"
? ? environment:
? ? ? - "MYSQL_ROOT_PASSWORD=test@123"
volumes:
? my_django_volume:
? ? external: true"
Note:
If you have multiple containers and you want to remove all of them in one go, then use the below command -
docker rm -f $(docker ps -a -q)
-f Used when containers are running. If they are stopped then remove -f.
docker ps -a -q lists all the container IDs.
Thank you for reading! ??
Happy Learning!
DevOps |AWS Cloud| Git| Docker| Jenkins |Kubernetes| Terraform| Ansible| Linux| Python| Mainframe
1 年good going! Aishwarya keshri