Docker Compose for DevOps Engineers

Docker Compose for DevOps Engineers

#90DaysofDevOps Challenge - Day 18

▶What is Docker-Compose?

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

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

The key features of Compose that make it effective are:

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

I created a Project of WordPress with MySQL DB (Available on my GitHub).

  • Step 1 - Create a working directory
$ mkdir docker-compose-wordpress-mysql
$ cd docker-compose-wordpress-mysql

  • Step 2 - Create a docker-compose.yml file
version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "80:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
volumes:
    db_data:

  • Step 3 - Build and run your app with Compose
$ docker-compose up -d

Compose pulls a MySQL image, builds an image for your code, and starts the services you defined.

Enter http://localhost/ in a browser to see the application running.

If this doesn’t resolve, you can also try http://127.0.0.1:8000.

You should see this view in your browser:

  • Step 4 - Listing images at this point should return WordPress and MySQL.
$ docker images

  • Step 5 - Stop and remove the containers
$ docker-compose down

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

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

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

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

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

Pull a pre-existing Docker image from the Docker hub and run it.

It was a Docker Compose UI app.

Some Docker Inspect commands:

ID of a container by name

$ docker container inspect -f '{{.Id}}' [container_name]

Container's main process

docker container inspect -f '{{printf "%s " .Path}}{{range .Args}}{{printf "%s " .}}{{end}}' [container_name|id]

Listing the port bindings

$ docker container inspect -f '{{range $target, $published := .NetworkSettings.Ports}}{{range $published}}{{printf "%s -> %s:%s\n" $target .HostIp .HostPort}}{{end}}{{end}}' [container_name|id]

Listing its IP addresses

$ docker container inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' [container_name|id]

Listing the environment variables

$ docker container inspect -f '{{range .Config.Env}}{{printf "%s\n" .}}{{end}}' [container_name|id]

Checking Docker Logs

$ docker logs [container_name|id]

Stop and Start a Docker

$ docker stop [container_name|id]

$ docker start [container_name|id]

To delete Docker containers

Note - Per recommendation, you must run kill command to force a stop to the containers before deleting them.

$ docker kill [container_name|id]
$ docker rm [container_name|id]

Thank you for reading this Blog, and hope it was helpful to you.