Day 18 of #90DaysOf DevOps Challenge

Day 18 of #90DaysOf DevOps Challenge

?Docker Compose

Using docker commands, we can only run and manage a single container at a time, but there can be scenarios when you need to create multiple containers and manage them. It would help if you had a single command solution to create various containers, manage them and then stop them with a single command.

Docker-compose does precisely what I mentioned above. It allows docker to set up the multi-container environment.

There is a three-step process to work with Docker Compose.

1. Define the application environment with Dockerfile for all services.

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

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

You can run the docker-compose down command to destroy the above infrastructure.

Pre-requisites:

  • You should have a Docker engine running on your system.

Docker-compose comes by default with docker installation.

  • You should know YAML because the docker-compose files are written in the YAML language.

Now, let's learn a little bit about YAML which is very important for a DevOps engineer because it is used in many DevOps tools, like docker, Ansible, K8s, pipelines, etc.

?YAML

YAML is a human-readable data serialization language that is often used for writing configuration files.?

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 files use .yml or .yaml extension.
  • The indentation is very important in YAML as it does not use '{ }' like json or '[ ]', '?' symbols like other programming languages. The hierarchy of objects is defined by indentation only. So, you have to be very careful with indentation (white spaces) while using YAML.

Tip: Try to use spaces instead of tabs.

  • You can use the hash symbol '#' to comment out a line.
  • 3 dashes (---) are used to signal the start of a document, while each document ends with three dots (...).??
  • The structure of a YAML file is a map or a list.

?Docker-Compose Commands

Sample docker-compose file -

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

build –

The build option is used to build images for services for which the build is defined.

$ docker-compose build             # Build all services
$ docker-compose build web         # Build single service        

up –

Use to create docker containers with available services in the docker-compose.yml file in the current directory. Use the -d switch to launch containers in daemon mode.

$ docker-compose up -d            # Create all containers
$ docker-compose up -d web        # Create single container        

down –

This will stop and delete all containers, network, and associated images for the services defined in a config file.

$ docker-compose down           # Restart all containers
$ docker-compose down web       # Restart single container        

ps –

This will list all containers created for the services defined in a config file with their status, port bindings, and command.

$ docker-compose ps         

exec –

This will execute a command to the running container. For example list files in a container associated with?a web?service. (Similar to the docker exec command)

$ docker-compose exec web ls -l        

start –

This will start stopped containers of the services defined in the config file.

$ docker-compose start            # Start all containers
$ docker-compose start web        # Start single container        

stop –

This will stop running containers for the services defined in the config file

$ docker-compose stop             # Stop all containers
$ docker-compose stop web         # Stop single container        

restart –

This will restart containers of the services defined in the config file

$ docker-compose restart           # Restart all containers
$ docker-compose restart web       # Restart single container        

pause –

This will pause running containers for the services defined in the config file.

$ docker-compose pause            # Pause all containers
$ docker-compose pause web        # Pause single container        

unpause –

This will start paused containers for the services defined in the config file.

$ docker-compose unpause            # Start all paused containers
$ docker-compose unpause web        # Start single paused container        

rm –

This will remove stopped containers for the services defined in the config file.

$ docker-compose rm               # Start all paused containers
$ docker-compose rm web           # Start single paused container        

?Task

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.

Example:

In below configuration file, we are creating two containers. One is for the Apache webpage and the other is for the database.

version: '3.3'
services:
? db:
? ? ?image: mysql
? ? ?container_name: mysql_db
? ? ?restart: always
? ? ?environment:
? ? ?  MYSQL_ROOT_PASSWORD: pass
? web:
? ? image: apache
? ? build: ./webapp
? ? depends_on:
? ?   - db
? ? container_name: apache_web
? ? restart: always
? ? ports:
? ?   - 8080:80        

  1. To see what are all the available environment variables for the image which you are using for your docker container, you can refer to the docker hub page for that image where you will get all the required information and environment details you need to specify in your configuration file.

In this case, I used the MySQL image so I referred to the below page -https://hub.docker.com/_/mysql

You can refer to the example on the same page.

No alt text provided for this image

2. To link two different containers or define the dependency, we can use the depends_on option.

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

From user Aish, I cannot do docker ps as it is a non-root user.

So, I added this user to the group - docker.

No alt text provided for this image

Need to reboot the system.

No alt text provided for this image

Now I can run docker commands without sudo. You can see that the user is added to the docker group.

id        

Above command gives details about the current user's uid, gid, and groups to which the user is added.

No alt text provided for this image

I can create the docker container with the name C1 and image nginx.

docker run -itd --name C1 nginx        
No alt text provided for this image

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

docker inspect C1        

The above command gives detailed information about the container and we can find the ExposedPorts under config.

No alt text provided for this image
docker top C1        

The above command will show the running processes in the container.

No alt text provided for this image

You can also use the ps aux command.

docker exec C1 ps aux        

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

docker logs C1        
No alt text provided for this image

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

To stop the running container, run the below command -

docker stop C1        

To start the stopped container, run the below command -

docker start C1        
No alt text provided for this image

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

To remove a running container, run the below command -

docker rm -f C1        

Or you can first stop the container and then remove it without the -f (forcefully) flag.

No alt text provided for this image

That's it for this Article.

Thank you for reading! ??

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

Aishwarya keshri的更多文章

  • Day26 of #90DaysOfDevOps Challenge

    Day26 of #90DaysOfDevOps Challenge

    Jenkins Pipeline: Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery…

  • Day24 of #90DaysOfDevOps Challenge

    Day24 of #90DaysOfDevOps Challenge

    ??Project: Containerise and deploy a node.js application using Jenkins Job.

  • Day23 of #90DaysOfDevOps Challenge

    Day23 of #90DaysOfDevOps Challenge

    CI/CD Continuous integration and continuous delivery help in automating the software development lifecycle stages…

    2 条评论
  • Day22 of #90DaysOfDevOps Challenge

    Day22 of #90DaysOfDevOps Challenge

    ??Jenkins Jenkins is an open-source tool that helps in creating pipelines and automating the software development…

  • Day21 of #90DaysOfDevOps Challenge

    Day21 of #90DaysOfDevOps Challenge

    Important interview questions and Answers for Docker: 1. What is the difference between an Image, Container and Engine?…

  • Day20 of #90DaysOfDevOps Challenge

    Day20 of #90DaysOfDevOps Challenge

    Docker Cheat Sheet: ??Docker images- Show all locally stored top-level images: docker images Pull an image from a…

  • Day 19 of #90DaysOfDevOps Challenge

    Day 19 of #90DaysOfDevOps Challenge

    Docker Volumes When we are working on docker, the data we store gets lost when the container is destroyed. So, to…

    2 条评论
  • Day17 of #90DaysOfDevOps Challenge

    Day17 of #90DaysOfDevOps Challenge

    Dockerfile: Instead of manually creating docker images by running multiple commands one by one, we can write a script…

    1 条评论
  • Day 16 of #90DaysOfDevOps Challenge

    Day 16 of #90DaysOfDevOps Challenge

    ?Docker: Docker is a containerization tool that helps us create a lightweight container with all the required packages…

  • Day15 of #90DaysOfDevOps Challenge

    Day15 of #90DaysOfDevOps Challenge

    ?Python Libraries: The collection of modules used in Python while writing different programs is known as libraries…

社区洞察

其他会员也浏览了