Day 19: Docker for DevOps Engineers

ยท

4 min read

Till now you have learned how to create a docker-compose.yml file and pushed it to the Repository. Let's move forward and dig more into other Docker-compose.yml concepts. Aaj thodi padhai krte hai on Docker Volume & Docker Network ๐Ÿ˜ƒ

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 the same data.

Docker Volume Commands -

Here's a list of Docker volume commands:

  1. docker volume create <VOLUME_NAME>: Create a named volume.

  2. docker volume ls: List available volumes.

  3. docker volume inspect <VOLUME_NAME>: Display detailed volume information.

  4. docker volume rm <VOLUME_NAME>: Remove a named volume (must be unused).

  5. docker volume prune: Remove all unused volumes.

  6. docker run -v <VOLUME_NAME>:<CONTAINER_PATH>: Mount a named volume to a container.

  7. docker run -v <HOST_PATH>:<CONTAINER_PATH>: Mount a host directory to a container.

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.

Docker Network Commands -

Here's a list of Docker network commands:

  1. docker network create <NETWORK_NAME>: Create a new user-defined network.

  2. docker network ls: List available networks.

  3. docker network inspect <NETWORK_NAME>: Display detailed network information.

  4. docker network rm <NETWORK_NAME>: Remove a user-defined network (must be unused).

  5. docker network prune: Remove all unused networks.

  6. docker run --network <NETWORK_NAME>: Specify a network for a container to join.

  7. docker network connect <NETWORK_NAME> <CONTAINER_NAME>: Connect a container to a user-defined network.

  8. docker network disconnect <NETWORK_NAME> <CONTAINER_NAME>: Disconnect a container from a user-defined network.

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 )

Use the docker-compose up command with the -d flag to start a multi-container application in detached mode.

Check whether the service is running or not by using docker ps

Check if the web is working or not, use localhost and add the port number

Use the docker-compose scale command to increase or decrease the number of replicas for a specific service. You can also add replicas in deployment file for auto-scaling.

Now, we can access our website on port 8005 also.

Keeping a single instance for our web service

Now, we can only access our website on a single port 8003.

Now, If you try to access it on another port it will give an error.

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.

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.

Creating two containers using nginx -

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

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

Using docker volume ls command -

Removing the volume using docker volume rm command -

I hope you like my blog..!!

Stay Connected with me for more interesting articles on DevOps, if you like my blog follow me on Hashnode and Linkedin (https://www.linkedin.com/in/som-shanker-pandey/

Did you find this article valuable?

Support Som Pandey's blog by becoming a sponsor. Any amount is appreciated!

ย