Skip to main content

Command Palette

Search for a command to run...

Day 19 Task: Docker for DevOps Engineers

Updated
9 min readView as Markdown

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.

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.

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

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

Creation two-tier flask application

create a new directory

a)mkdir projects

b)cd projects

c)Clone the GitHub repository in your local.

d)git clone https://github.com/LondheShubham153/two-tier-flask-app.git

e)cd two-tier-flask-app

First, you have to install docker-compose and then execute the command.

Step 1 — Installing Docker Compose

To make sure you obtain the most updated stable version of Docker Compose, you’ll download this software from its official Github repository.

First, confirm the latest version available in their releases page. At the time of this writing, the most current stable version is 1.29.2.

The following command will download the 1.29.2 release and save the executable file at /usr/local/bin/docker-compose, which will make this software globally accessible as docker-compose:

Command\=The following command will download the 1.29.2 release and save the executable file at /usr/local/bin/docker-compose, which will make this software globally accessible as docker-compose:

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Next, set the correct permissions so that the docker-compose command is executable:

sudo chmod +x /usr/local/bin/docker-compose

To verify that the installation was successful, you can run:

docker-compose --version

Run the docker-compose command

docker-compose up -d

2.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.

the docker-compose scale command allows you to change the number of containers for a particular service defined in your docker-compose.yml file. However, keep in mind that docker-compose doesn't support auto-scaling natively; you'd need to manually adjust the number of replicas. Here's how you'd use it:

Let's say you have a service named web defined in your docker-compose.yml:

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"

To scale the web service to 3 containers, you'd use the docker-compose scale command:

docker-compose up -d  # Start the initial service
docker-compose scale web=3  # Scale the 'web' service to 3 containers

This will create two additional containers based on the nginx:latest image, effectively having three instances of the web service running.

3.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.

Command name\=docker-compose ps

docker-compose logs

4.Use the docker-compose down command to stop and remove all containers, networks, and volumes associated with the application.

docker-compose down

Task-2

1.Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.

Docker Volumes and Named Volumes are powerful features for sharing files and directories between containers or persisting data even after containers are stopped or removed. Here's a breakdown of how to use them:

Docker Volumes:

1. Creating a Volume:

You can create a Docker volume using the docker volume create command:

docker volume create my_volume

2. Attaching a Volume to a Container:

To use this volume in a container, you specify it when running the container:

docker run -d -v my_volume:/path/in/container my_image

This command will run a container using my_image and mount the my_volume volume to the specified path inside the container (/path/in/container).

3. Mounting Host Directories as Volumes:

You can also mount a directory from your host machine to a container:

docker run -d -v /host/path:/container/path my_image

Here, /host/path is a directory on your host machine, and /container/path is the path where it will be mounted inside the container.

Named Volumes:

Named volumes are a specific type of Docker volume that allows easier management and reference by a user-defined name instead of a random string assigned by Docker.

1. Creating a Named Volume:

docker volume create my_named_volume

2. Using Named Volumes in Containers:

Similar to the previous method, you can use named volumes when running a container:

docker run -d -v my_named_volume:/path/in/container my_image

3. Inspecting Named Volumes:

To see information about a named volume:

docker volume inspect my_named_volume

Example Use Case:

Let's say you have two containers: app and database, and you want to share data between them. You could use a named volume to share a directory where the database stores its data:

docker volume create db_data
docker run -d -v db_data:/var/lib/database database_image
docker run -d -v db_data:/app/database_data app_image

Both containers (database_image and app_image) will now share the db_data named volume, allowing them to access and modify data in the /var/lib/database directory from within their respective containers.

Volumes and named volumes provide flexible ways to manage data and share files between containers in Docker, offering durability and persistence even after containers are stopped or removed.

2.Create two or more containers that read and write data to the same volume using the docker run --mount command.

Using the docker run --mount command, you can create multiple containers that read from and write to the same volume. Let's create two containers that read and write data to a shared volume:

1. Create a Docker Volume:

First, let's create a volume named shared_volume:

docker volume create shared_volume

2. Start Containers with Shared Volume:

Container 1:

Run the first container and mount the shared_volume to a specific path inside the container (/data in this example):

docker run -d --name container1 --mount source=shared_volume,target=/data my_image1

Replace my_image1 with the image you want to use for the first container.

Container 2:

Similarly, run the second container and mount the same shared_volume to a different path:

docker run -d --name container2 --mount source=shared_volume,target=/data my_image2

Replace my_image2 with the image you want to use for the second container.

Data Interaction:

Now, both container1 and container2 have access to the same volume mounted at /data.

Any changes made by container1 in the /data directory will be reflected in container2, and vice versa. They can read, write, and modify files within this shared volume.

For instance, within each container, you can interact with the shared volume by creating, modifying, or deleting files within the /data directory:

# Inside container1
echo "Data from container1" > /data/file_from_container1.txt

# Inside container2
cat /data/file_from_container1.txt  # This will display "Data from container1"

# Inside container2
echo "Data from container2" > /data/file_from_container2.txt

# Inside container1
cat /data/file_from_container2.txt  # This will display "Data from container2"

Both containers will have access to the changes made within the shared volume, allowing them to read and write data interchangeably.

Remember to replace my_image1 and my_image2 with the actual images you want to use for these containers and adjust the paths and commands based on your specific use case and requirements.

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

You can verify that the data remains consistent across all containers sharing the same volume by using the docker exec command to run commands inside each container.

Let's assume you have two containers, container1 and container2, both sharing the same volume named shared_volume. To verify the data consistency:

1. Access container1 and create a file inside the shared volume:

docker exec -it container1 bash  # Access container1's shell
echo "Data from container1" > /data/file_from_container1.txt  # Create a file
exit  # Exit container1's shell

2. Access container2 and check if it can read the file created by container1:

docker exec -it container2 bash  # Access container2's shell
cat /data/file_from_container1.txt  # Check content of the file
exit  # Exit container2's shell

By running these commands, you can confirm that container2 is able to read the file file_from_container1.txt created by container1. This demonstrates that the data is indeed shared and consistent across both containers due to their common access to the shared volume shared_volume.

You can perform additional actions like modifying files, creating directories, or deleting content within the shared volume from different containers using docker exec to verify that all changes are reflected consistently across all containers sharing that volume.

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

The docker volume ls command allows you to list all Docker volumes on your system, while docker volume rm is used to remove a specified volume.

List All Volumes:

To list all volumes available on your system, use the following command:

docker volume ls

This command will display a list of all Docker volumes along with their names and other details.

Remove a Volume:

To remove a specific volume, you'll use the docker volume rm command followed by the name of the volume you want to delete. For example, to remove a volume named shared_volume:

docker volume rm shared_volume

Replace shared_volume with the actual name of the volume you want to remove.

Remember, make sure the volume is not being used by any running containers before attempting to remove it. If a volume is in use, you might need to stop or remove the containers associated with it before you can delete the volume.

Always be cautious when removing volumes as it will also remove all data stored within that volume permanently.

After you're done and have confirmed that the volume is no longer needed, you can use docker volume rm to remove it to free up space or for cleanup purposes.

Thank you for reading this Blog. Hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future😊😊

More from this blog

90daysofdevopschallenge

37 posts