Day 18 of 90daysofdevops: Docker for DevOps Engineers Part 2 : Docker -compose

Day 18 of 90daysofdevops: Docker for DevOps Engineers Part 2 : Docker -compose

Docker Compose

Docker Compose is a tool that was developed to help define and share multi-container applications.

With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

What is YAML?

YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.

YAML is a popular programming language because it is human-readable and easy to understand.

YAML files use a .yml or .yaml extension.

Task-1

Learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Step 1 : Install docker-compose:

No alt text provided for this image

Step 2 : Create a docker-compose.yml file inside project folder

No alt text provided for this image

  • version:"3.3" denotes that we are using version 3.3 of Docker Compose.

  • The services section defines all the different containers we will create. In our example, we have two services, web and database.

  • The build keyword specifies the location of our Dockerfile, and . represents the directory where the docker-compose.yml file is located.

  • The image keyword is used to specify the image from docker hub for mysql containers

  • For the database and web , we are using the ports keyword to mention the ports that need to be exposed.

  • And then, we also specify the environment variables for mysql which are required to run mysql.

Step 3 : Run docker-compose.yml file

docker-compose up: This command does the work of the docker-compose build and docker-compose run commands. It builds the images if they are not located locally and starts the containers. If images are already built, it will fork the container directly.

docker ps or docker-compose ps command list all the containers in the current docker-compose file.

No alt text provided for this image

Step 4 : docker-compose down :

This command stops all the services and cleans up the containers, networks, and images.

No alt text provided for this image

Task-2

1.Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine. Run the container as a non-root user.

No alt text provided for this image

  • for running container as a non-root user, use command usermod to give user permission to docker
sudo usermod -a  -G docker $USER
  • Then reboot instance after giving permission to user.

No alt text provided for this image

2.Inspect the container's running processes and exposed ports using the docker inspect command.

 docker inspect <container_name or ID>

No alt text provided for this image

No alt text provided for this image

3.Use the docker logs command to view the container's log output.

 docker logs <container_name or ID>

No alt text provided for this image

4.Use the docker stop and docker start commands to stop and start the container.

docker stop : To stop one or more running Docker containers.

 docker stop <container-name or ID>

docker start : Start one or more stopped containers

 docker start <container-name or ID>

No alt text provided for this image

5. Use the docker rm command to remove the container when you're done.

docker rm : Remove one or more containers

 docker rm <container_name or ID>

--force , -f : Force the removal of a running container.

No alt text provided for this image

Thank you for reading!