Day 19: Docker for DevOps Engineers

Radheya Zunjur
4 min readJul 6, 2023

--

Till now we have learned how to create docker-compose.yml file and pushed it to the Repository. Let’s move forward and dig more on other Docker-compose.yml concepts.
Today let’s learn Docker Volume & Docker Network

What is Docker-Volume?

Docker allows you to create something called volumes. Volumes are like separate storage areas that can be accessed by containers. They allow you to store data, like a database, outside the container, so it doesn’t get deleted when the container is deleted.
You can also mount from the same volume and create more containers having same data.

Ref : https://docs.docker.com/storage/volumes/

What is Docker Network?

Docker allows you to create virtual spaces called networks, where you can connect multiple containers (small packages that hold all the necessary files for a specific application to run) together. This way, the containers can communicate with each other and with the host machine (the computer on which the Docker is installed).

When we run a container, it has its own storage space that is only accessible by that specific container. If we want to share that storage space with other containers, we can’t do that.

Ref : https://docs.docker.com/network/

Tasks

Task-1
A)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 )

mkdir myproject
cd myproject
touch docker-compose.yml

Create a docker-compose.yml file

version: ‘3’
services:
app:
build:
context: ./app
ports:
— 8080:80
depends_on:
— db
db:
image: postgres:latest
environment:
POSTGRES_USER: radhey
POSTGRES_PASSWORD: radhey
POSTGRES_DB: projectdb

Create a new directory “app” for project

mkdir app
cd app
touch Dockerfile

create a docker file

FROM nginx:latest
COPY . /usr/share/nginx/html

In the project directory, run the following command to build and start the containers.

docker-compose up -d

Web application running on browser

B)Also add [`replicas`](https://stackoverflow.com/questions/63408708/how-to-scale-from-within-docker-compose-file) in deployment file for *auto-scaling*.

docker-compose up — scale app=5

Below are the 5 replica’s created,

We were facing error while scaling down the resources

Fixed by running-

sudo aa-remove-unknown

C)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.

docker-compose ps
docker-compose logs

D) Use the ‘docker-compose down’ command 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.
A)Create two or more containers that read and write data to the same volume using the `docker run — mount` command.

Step 1: Create a Docker volume or named volume.

docker volume create mydata

Step 2: Create the containers.

docker run -d — name container1 — mount source=mydata,target=/data alpine sh -c “echo ‘Hello from Container 1’ > /data/test.txt && sleep 3600”
docker run -d — name container2 — mount source=mydata,target=/data alpine sh -c “cat /data/test.txt && sleep 3600”

Step 3: Verify the shared data.

To verify that the data is shared between the containers, you can use the docker logs command to view the logs of container2:

docker logs container2

B)Verify that the data is the same in all containers by using the docker exec command to run commands inside each container.

Step1) Make sure both containers are running.

docker ps

Use the docker exec command to run a command inside container2 and inspect the shared volume:

docker exec -it container2 cat /data/test.txt

C) Use the docker volume ls command to list all volumes and docker volume rm command to remove the volume when you’re done.

docker volume ls
docker volume rm mydata

--

--

Radheya Zunjur
Radheya Zunjur

Written by Radheya Zunjur

Database Engineer At Harbinger | DevOps | Cloud Ops | Technical Writer

No responses yet