Getting Started Guide for Docker Compose

Learn via video courses
Topics Covered

Overview

Docker Compose is a tool that simplifies the management and deployment of multi-container applications by enabling you to define all services in one file and start/stop them with one command.

This guide covers the basics of using Docker Compose, including creating Docker files, installing, working with the file structure, and more.

Introduction

Docker Compose is a tool that allows developers and DevOps engineers to define and run multi-container applications with Docker by managing services in a single file and starting/stopping them with one command. It uses a compose file, which contains service definitions and instructions on how to start and stop them, to simplify the management and deployment of applications.

Creating Docker Files

To use Docker Compose, one needs to first create Docker files that define the application's services. A Docker file is a script that describes the process of building a container image for the application. Writing a script in the Docker file format requires knowledge of its basics.

How to Use and Install Docker Compose?

Before using Docker Compose, it must be installed on the system. The appropriate package for the operating system can be obtained by downloading it from the Docker website. Once installed, Docker Compose can be used to create and manage the services of the application by defining them in a compose file and running the relevant commands.

Docker Compose File Structure

The Docker Compose file structure defines the services that make up an application. The file is in YAML format and is named docker-compose.yml. It contains a list of services that are described as objects in YAML. Each service has a name, an image, and a set of options that determine how it should be started and stopped.

Docker Compose Commands

Docker Compose offers a variety of commands for managing your application's services, including:

  • "docker-compose up", which starts the services defined in the compose file
  • "docker-compose down", which stops the services defined in the compose file
  • "docker-compose build", which builds the services' images
  • "docker-compose ps", which shows the status of the services defined in the compose file.

Creating Compose Files with Services

To utilize Docker Compose, you must first create a compose file. This file defines the services that constitute your application, along with their dependencies and configurations. You can create a compose file using a basic text editor or an IDE with YAML syntax support.

The compose file consists of the following basic structure:

This example illustrates the definition of two services: "app" and "db". The "app" service is constructed from the present directory and connects host port 80 to container port 80. Additionally, it relies on the "db" service, meaning it won't start until the "db" service is operational. The "db" service employs the official MySQL image version 5.7 and has an environment variable MYSQL_ROOT_PASSWORD set to "password".

Define the App Service

The "app" service runs your application. In the compose file, you can define the image to be used, the ports to map, and any environment variables required by the application.

For instance, if the application is a Node.js app, the "app" service can be defined as follows:

This example illustrates the use of the official Node.js image version 14. It maps host port 3000 to container port 3000. An environment variable NODE_ENV is set to "production", the working directory is set to "/app" and the command to initiate the app is "npm start".

Define the MySQL Service

The "MySQL" service runs the database for your application. In the compose file, you can specify the image to use, the ports to map, and any environment variables that the database requires.

For instance, if you are using MySQL, the "MySQL" service can be defined as follows:

This example illustrates the use of the official MySQL image version 5.7. It maps host port 3306 to container port 3306. Additionally, environment variables MYSQL_ROOT_PASSWORD is set to "password" and MYSQL_DATABASE is set to "mydb".

Running an App with Docker Compose

The first step in getting started with docker-compose is to install it on your local machine. This can be done by following the instructions on the Docker website. Once docker-compose is installed, you can create a new project by running the following command:

This command will create a new directory for your project and generate a basic docker-compose.yml file. This file is used to define the services and containers that make up your application.

Next, you will need to define the services that make up your application in the docker-compose.yml file. Here is an example of a simple application that consists of a web server and a database:

In this example, we have defined two services: web and db. The web service uses the latest version of the nginx image and exposes port 80 on the host machine. The db service uses the latest version of the postgres image and sets the user and password for the database.

Once your docker-compose.yml file is defined, you can start your application by running the following command:

This command will start all of the services defined in the docker-compose.yml file and run them in the background. You can view the logs for each service by running the following command:

When you're ready to stop your application, you can run the following command:

Conclusion

Docker-compose is a powerful tool that makes it easy to manage multiple containers and services in your application. By following this guide, you should now have a basic understanding of how to get started with docker-compose and run your first app. With the help of this tool, You can easily run and scale your application with minimal effort.