Docker-compose is a tool that is used for multi-container applications in a single host. We can run multi containers as services in a single host with the help of docker-compose.yaml.

Docker Compose is really useful when we don't have the development environment setup on our local machine to run all parts of the application to test or we want to run all parts of the application with one command. For example, if you want to run React and nodeJS express server on different ports and need a single command to set up and run the whole thing. You can accomplish that with Docker Compose.

In this post, we will see some useful Docker Compose commands.

  • Prerequisites
  • Example Project
  • Docker Compose Commands
  • Summary
  • Conclusion

Prerequisites

There are prerequisites for this post you have to install Docker for Desktop or Docker on your machine based on the OS. Since we have used the React app as an example, install NodeJS on your machine. Finally, install the Docker Compose from this page based on your OS.

You can check the Docker and Docker-Compose versions after the installation with these commands

docker -v
docker-compose -v

Example Project

This is a simple project which demonstrates developing and running a React application with NodeJS. We have a simple app in which we can add users, count, and display them at the side, and retrieve them whenever you want.

None
Example Project

Here is a Github link to this project. You can clone it and run it on your machine.

// clone the project
git clone https://github.com/bbachi/react-nodejs-docker-compose.git

Let's set up the development environment with Docker Compose. First, we need to install Docker Compose on your machine here is the link. Check the installation with this command docker-compose --version

API Dockerfile

Once you have docker-compose installed, you need to have two separate docker files for each service. Here is the Dockerfile for the node express API and the following are the steps.

  1. We are starting from the base image node:10.
  2. Set the working directory as /usr/src/app/api.
  3. copy the package.json to install all the dependencies
  4. Install all the dependencies
  5. We need to put this expose command for the documentation purpose so that other developers know this service runs on port 3080.
  6. Finally, we run the command npm run dev

UI Dockerfile

Here is the Dockerfile for the React UI and the following are the steps.

  1. We are starting from the base image node:10.
  2. Set the working directory as /usr/src/app/my-app
  3. copy the package.json to install all the dependencies
  4. We need to install all the dependencies
  5. Exposing the port 3000
  6. Finally, we run the command npm start

Start Commands in Package.json

In the above Dockerfiles, we are running commands with npm on instantiating the containers. Let's see what are those commands in each package.json file.

For the API, we are running this command npm run dev which in turn runs this command nodemon ./server.js localhost 3080. Since it's a development environment we are using nodemon which listens for the changes in files and restart the server automatically.

None
API package.json

For the UI, all you need to run react-scripts start

None
UI package.json

Docker Compose File

Finally, let's look at the docker-compose file here. Since we need to run React on port 3000 and express API on port 3080 we need to define two services: nodejs-server and react-ui.

If you look at the above file we defined two services each has its own docker file. The most important thing here is the volumes part we need to mount the whole part of the application and node_modules folder as well. We need to mount the node_modules folder because the volume is not mounted during the build.

Since the command react-scripts start opens the browser when we run we have to add this stdin_open: true in the docker-compose file to start in an interactive mode.

Docker Compose Commands

We have seen the example project and all the related configured Docker Compose files. Let's understand the more common commands for the Docker Compose.

Building the Services

Services are built once and then tagged by default, as project service. We can build the services with this command docker-compose build.Let's see some of the useful flags for building services.

If you don't want to use cache for building service, you need to use the following command.

docker-compose build --no-cache
None
docker-compose build — no-cache

When you have multiple services in the docker-compose file and when you run the above command, it builds the services in series. You can issue this flag to build it in parallel.

docker-compose build --parallel
None
docker-compose build — parallel

There is a scenario where you want to pull images every time you build your services. You need to use the following command

docker-compose build --pull

Go to this link if you want to know more flags like this.

Start the Services

You need to start the services after the build. You can even issue this command without building first. If the services are not available this command builds those services.

docker-compose up
None
docker-compose up

If you want to run the containers in detached mode, add the following flag.

docker-compose up -d

If you want to recreate the services if the containers already exist, add the following flag.

docker-compose up -d --no-recreate

If you want to build the images before starting the services, add the following flag.

docker-compose up -d --build

Go to this link if you want to know more flags like this.

Listing Images

If you want to list all the images that docker-compose built, you can run this command.

docker-compose images
None
docker-compose images

If you want to show only image ids, you can add this flag.

docker-compose images -q

Go to this link if you want to know more flags like this.

Listing Containers

If you want to list all the running containers that docker-compose built, you can run this command.

docker-compose ps
None
docker-compose ps

If you want to see stopped containers as well, add this flag

docker-compose ps --all

Go to this link if you want to know more flags like this.

Start Existing Containers

If you want to start existing containers for the service, you can run the following command

docker-compose start <service name>

Go to this link if you want to know more flags like this.

Stop Existing Containers

If you want to stop running containers without removing them, you can run the following command

docker-compose stop <service>
None
docker-compose stop

Go to this link if you want to know more flags like this.

Display Running Processes

If you want to list running processes, you can run the following command

docker-compose top
None
docker-compose top

Go to this link if you want to know more flags like this.

Force Stop Running Containers

This command forces running containers to stop by sending a SIGKILL signal.

docker-compose kill <service name>

You can pass this signal optionally.

docker-compose kill -s SIGINT

Go to this link if you want to know more flags like this.

Remove Stopped Containers

When you stop or kill the process the containers are not removed. You can run the following command if you want to remove the stopped service containers.

docker-compose rm <service name>
None
docker-compose rm

Go to this link if you want to know more flags like this.

Stop and remove the services

If you need one command to stop and remove the containers, here is the command.

docker-compose down
None
docker-compose down

Go to this link if you want to know more flags like this.

Service Logs

The following command logs the output from the services.

docker-compose logs
None
docker-compose logs

Go to this link if you want to know more flags like this.

Exec Into Running Services

This command is equivalent to the docker exec command. You can run arbitrary commands in your services.

docker-compose exec -it nodejs-server /bin/sh
None
docker-compose exec

Go to this link if you want to know more flags like this.

Validate the Compose File

If you want to validate the compose file, you can run the following command in the location where your compose file resides.

docker-compose config
None
docker-compose config

Go to this link if you want to know more flags like this.

Stream the Container Events

if you want to know what events occur on the containers and stream those, use the following command. Pass the json flag to print in a json format.

docker-compose events --json

Issue this command in one terminal and open another terminal and start or stop the container. You can see the output like below.

None
docker-compose events — json

Go to this link if you want to know more flags like this.

Pause and Unpause the containers

If you want to pause and unpause the container you can use the following commands.

docker-compose pause <service name>
docker-compose unpause <service name>
None
docker-compose pause

Summary

  • Docker Compose is really useful when we don't have the development environment setup on our local machine to run all parts of the application to test
  • Docker-compose is a tool that is used for multi-container applications in a single host.
  • We can run multi containers as services in a single host with the help of docker-compose.yaml.
  • We have seen and tested 15 more common useful commands.
  • You can find more command on this page

Conclusion

Getting used to these commands comes in handy especially in the development environment as the Docker-compose is used mostly in the development phase.