Docker for DevOps Engineers

Docker for DevOps Engineers

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. With Docker Compose, you define your application's services, networks, and volumes in a YAML file, and then use the docker-compose command-line tool to start and stop your application. The YAML file includes information such as the container images to use, ports to expose, and environment variables to set.

Docker Compose simplifies running multi-container applications by enabling you to start and stop your entire application with just one command instead of managing each container individually.

You can package your entire application into a single Docker Compose file, and then share it with others or deploy it to production environments.

Install Docker-Compose on Ubuntu

Make sure you have Docker installed on your system.

Once Docker is installed, you can install Docker Compose by running the following command in your terminal:

sudo apt install docker-compose

Verify that Docker Compose is installed correctly by running:

docker-compose --version

What is YAML?

YAML stands for yet another markup language or YAML ain’t markup language it is a human-readable data serialization language.

YAML is designed to be easy to read and write for humans, with a syntax that uses indentation to indicate hierarchy and key-value pairs represented by colons. YAML files are commonly used in web development and DevOps workflows, including configuration files for tools like Docker, Kubernetes, and Ansible.

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.

Below is the docker-compose file for Django app to build and run multiple containers using a single command,

It will create two container when you use below command,

docker-compose up -d

Task-2

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

    To run a container as a non-root user we can use the below commands,

    sudo usermod -aG docker $USER

    sudo reboot

    Then we can create a simple docker-compose file to build and run nginx.

    And after this use docker-compose up this will start the container.

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

    To view the container's log output, you can press Ctrl+C to stop the container, and then run the following command:

    docker-compose logs

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

    To stop the container, run the following command:

    docker-compose down

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

    To remove the container and any associated volumes and networks, run the following command:

    docker-compose down --volumes --remove-orphans

Thank you for reading this blog!!