Introduction about Docker Compose

We use Dockerfile to configure and build Docker images. To run Docker containers from these Docker images, you can use the command line or Docker Compose. The benefit of Docker Compose is that you can define and run multiple Docker containers at the same time. How is it in detail? I will show you how to define and run Docker containers with Docker Compose in this tutorial!

Example application

As an example for this tutorial, I will create a simple Spring Boot project with a declaration to use the database but there will be no manipulations to the database, similar to the tutorial Deploy Spring Boot application in Docker, as follows:

Introduction about Docker Compose

Result:

Introduction about Docker Compose

SpringBootDockerComposeApplication:

application.properties

Result when running the application:

Introduction about Docker Compose

Build Dockerfile

To be able to use Docker Compose, we need to build a Docker Image for our application. The difference here is that we don’t need to declare the application’s environment variable in the Dockerfile, we will use Docker Compose to do this.

I will create a new Dockerfile in the project for example:

Introduction about Docker Compose

The content of the Dockerfile file for my application is as follows:

Writing Docker Compose

Once you have a Docker Image for your application, you can use Docker Compose to deploy your application. As I said, the biggest benefit of Docker Compose is that it helps us to configure and run multiple Docker images at the same time.

To illustrate this, I will no longer be using PostgreSQL on my local machine. I’m going to write a Docker Compose that can do a fresh install of PostgreSQL and then run my application. But first, let’s talk a little bit about Docker Compose!

Docker Compose normally uses .yml files for definitions.

By default, Docker uses a file called docker-compose.yml to run Docker Compose, so I will create a new docker-compose.yml file in the project as follows:

Introduction about Docker Compose

First, you can use the version attribute to define the version of Docker Compose that we will use. You can refer to the version of Docker Compose here.

I will declare to use Docker Compose version 3.8 as follows:

Each Docker Container is a service in the Docker Compose file and we will use the services attribute along with the name of each service for our Docker Containers. I will declare the following:

Our task is to declare information for each service. You can use some of the following common attributes to declare each service as follows:

  • image: Docker Image that we will use to run the container.
  • container_name: the name of the container.
  • restart: you can see more here. Normally we would configure this property to be on-failure and the number of restarts if the container start failed.
  • environment: declare the required environment variables for each container.
  • volumes: mount the directory between the running Docker machine (host) and the container.
  • ports: port mapping between the running Docker machine (host) and the container.
  • depends_on: indicates the dependency of this service to another service in Docker Compose. The container with the name declared in this attribute must be available before this service can be run.
  • networks: defines a shared network for containers. Usually, we will use the bridge driver for the network.

We will define the service for postgresql first. I would define it as follows:


You can refer to the tutorial Install PostgreSQL server using Docker to understand more configurations for this postgresql service.

As for the example application, I will define the following:

Since there must be a database for our example application to run, I use the depends_on attribute in the spring_boot_docker_compose service to declare this dependency.

Both of the two services that I have defined above use the same networks as huongdanjava with the bridge driver. We need to define this network as follows:

The entire content of the docker-compose.yml file is as follows:

OK, here we are done writing Docker Compose for the example application. Now let’s see how it runs!

I will build a Docker Image for the example application:

Result:

Introduction about Docker Compose

Now you can run Docker Compose. We will run the following command:

Result:

Introduction about Docker Compose

Add Comment