Day 18/90 Docker for DevOps Engineers

Day 18/90 Docker for DevOps Engineers

Docker Compose

  • Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It uses a YAML file to define the services, networks, and volumes required for the application, making it easy to spin up and manage complex container-based environments.
  • Compose works in all environments; production, staging, development, testing, as well as CI workflows. It also has commands for managing the whole lifecycle of your application: Start, stop, and rebuild services View the status of running services Stream the log output of running services Run a one-off command on a service

Working of Docker Compose

Using Docker-Compose is essentially a three-step process:

1. Define application environment with Dockerfile for all services.

2. Create a docker-compose.yml file defining with all services under application.

3. Run docker-compose up to run all services under applications.

Key features of Docker Compose

  1. Declarative Configuration: Docker Compose uses a YAML file (usually named docker-compose.yml) to define the services, networks, and volumes that make up your application. This file describes how containers should be configured, connected, and run.
  2. Service Definition: With Docker Compose, you can define multiple services (containers) for your application in a single file. Each service can be configured independently, specifying details such as the Docker image to use, environment variables, ports to expose, volumes to mount, and more.
  3. Container Orchestration: Docker Compose simplifies the process of managing multiple containers by allowing you to define relationships and dependencies between them. You can specify which containers depend on others, and Docker Compose will ensure that they are started in the correct order.
  4. Networking: Docker Compose automatically creates a default network for your application, allowing containers to communicate with each other using service names as hostnames. You can also define custom networks to isolate different parts of your application if needed.
  5. Volume Management: Docker Compose allows you to define volumes that can be shared between containers or persisted beyond the lifecycle of a container. This is useful for storing persistent data, such as databases or configuration files.
  6. Environment Variables: Docker Compose supports defining environment variables for your services, making it easy to configure your application for different environments (e.g., development, testing, production).
  7. Scaling: While Docker Compose is primarily designed for development and testing environments, it does support limited scaling capabilities. You can specify the number of container instances to run for each service, allowing you to scale your application horizontally during development or testing.
  8. Integration with Docker CLI: Docker Compose integrates seamlessly with the Docker CLI, allowing you to use familiar commands like docker-compose up, docker-compose down, docker-compose build, etc., to manage your application.
  9. Extensibility: Docker Compose can be extended with third-party tools and plugins to add additional functionality, such as logging, monitoring, and orchestration.


YAML

  • YAML stands for YAML Ain’t Markup Language or yet another markup language
  • YAML is a human-readable data serialization format used to represent structured data in a simple and easily understandable way. It can be understood as a way to write down information in a format that both humans and computers can read and understand.
  • YAML is often used for configuration files, data exchange between systems, and defining complex structures. It’s commonly used in various programming languages and tools, including Docker Compose.
  • YAML files use a .yml or .yaml extension.

The syntax of the YAML file is:

keyword: argument        

Example of a YAML file

name: XYZ
age: 100
email: [email protected]        


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 can be installed as Docker Desktop (Docker Engine + Docker Compose), Docker Compose Plugin, or a Standalone Docker Compose.

I am going to install docker-compose as a standalone in Ubuntu. To install docker-compose, the following commands are used:

sudo apt-get update
sudo apt-get install docker-compose        

To verify the successful installation of docker-compose, check the version:

docker-compose version        

Once installed, we can start configuring services for the Docker engine by creating docker-compose.yaml file. Let us consider the sample docker-compose.yaml file provided to us. The file has the following information:

version : "3.3"
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    depends_on:
      - db
  db:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      - "MYSQL_ROOT_PASSWORD=test@123        

Let’s break down and understand line by line of the above file.

| version: "3.3"

This line specifies the version of the Docker Compose file format being used. In this case, it’s version 3.3.

| services:

Next part, we define two services: ‘web’ and ‘db’. Each service represents a containerized component of the application.

web:
    image: nginx:latest
    ports:
      - "80:80"
    depends_on:
      - db        

For the web service:

  • The image specifies that the nginx image should be used for this service, with the latest tag.
  • The ports section maps the host machine's port 80 to the container's port 80.
  • With the depends_on option set, Docker Compose ensures that the db service is started before the web service. This means that when you start the containers using docker-compose up, the db service will be started first, and then the web service will start.

NOTE:

depends_on keyword is used for linking two containers.

The web service depends on the db service. Here's how the linking works:

  • The web service is defined first. It specifies a build context (./web), exposing port 80 on the host machine, and includes the depends_on option.
  • The db service is defined after the web service. It uses the MySQL image and sets the root password environment variable.

db:
    image: mysql
    ports:
      - "3306:3306"
    environment:
      - "MYSQL_ROOT_PASSWORD=test@123"        

For the db service:

  • The image specifies that the mysql image should be used for this service.
  • The ports section maps the host machine's port 3306 to the container's port 3306.
  • The environment section under the db service sets an environment variable for the MySQL container. In this case, it sets the MYSQL_ROOT_PASSWORD environment variable to test@123. This password will be used for the root user of the MySQL database.

Once the services are defined, to start containers, use the following command:

docker-compose up        

We can observe that db is pulled first and then the web.


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 (Hint- Use usermod command to give user permission to docker). Make sure you reboot instance after giving permission to user.


----> I am pulling the Nginx image. and running the container.> I am pulling the Nginx image. and running the container.

docker pull nginx
docker run -d -p 8080:80 nginx        


----> To run the container as a non-root user and use the docker commands without sudo, we need to give the user permission to docker using the following command and then reboot the system:

sudo usermod -aG docker ubuntu
sudo reboot        


----> The docker inspect command is used to retrieve detailed information about Docker objects such as containers, images, networks, and volumes.

docker images
docker inspect nginx        

The output of docker inspect command has various details in JSON format. Look for the following sections to use custom formatting and inspect running processes and exposed ports:

  • "State" section: This section provides information about the container's current state, including whether it is running or stopped.
  • "NetworkSettings" section: Here, you can find details about the container's network configuration, including the IP address and exposed ports.


----> Use the docker logs command to view the container’s log output.

docker logs <container_id>        


----> Use the docker stop and docker start commands to stop and start the container.

docker stop cont_id
docker start cont_id        

We can observe that once the container is stopped, it is not up. As soon as we start the container, it becomes up.


----> Use the docker rm command to remove the container when you’re done.

We cannot remove a running container. Hence, either we need to stop the container and then delete the container or perform the forceful removal of the container. Let’s forcefully remove the container:

docker rm -f container_id        


Thanks.. for reading..

Ishmeet Singh

Frontend Developer?? || Growth expert ?? || Follow-> Devops related content?? || Helping Client's to Grow their Profile and Business?? || Feel Free to connect ??|| DM for Promotion ??

11 个月

Nice share

要查看或添加评论,请登录

Dnyaneshwari khapekar的更多文章

社区洞察

其他会员也浏览了