Home Docker Getting Started With Docker Compose In Linux

Getting Started With Docker Compose In Linux

By editor
Published: Updated: 2.5K views

Docker Compose is a tool for defining and running multi-container docker applications in Linux. With Compose, we use a YAML file to configure our application’s services. And then we create and start all the services from the configuration with a single command. Here is a simple graphical illustration that shows how Docker compose works.

How Docker compose works - A graphical illustration

How Docker compose works - A graphical illustration

Docker Compose is basically a three-step process:

  1. First, we need to define application environment with a Dockerfile so it can be reuse again.
  2. Secondly, we define the services that make up the app in docker-compose.yml so they can be run together in an isolated environment.
  3. Finally, we run docker-compose up command and Compose will start and run your entire application.

Getting Started With Docker Compose In Linux

Docker Compose can be installed in two ways. You can find the installation instructions under "Install Docker Compose" section in the following links.

After installation, check the version of Docker compose using commands:

# docker-compose -version


# docker-compose -v


# docker-compose --version
check docker compose version

Check docker compose version

To get help, simply run:

# docker-compose -help

This command will list out the commands which docker compose can perform.

docker compose help

docker compose help

Now let us take a simple docker compose file as an example. Here is the contents of my compose file.

Sample docker compose file

Sample docker compose file

Above docker compose file is a bare minimum file to understand the basic content inside the compose file.

We can check the validity of the file by using command:

# docker-compose config
check compose file validity

Check docker compose file validity

If you give a wrong version inside the docker compose file, then it will give you an error message.

docker compose file 1

Docker compose file error message

Now let us run the compose file using command:

# docker-compose up -d

It will start all services with single command.

run docker compose

Run docker compose

To list out the running containers created by compose file, run:

# docker-compose ps
list running containers

List running docker containers

We can bring down the application at any time with the following command:

# docker-compose down
stop containers

Stop docker containers

We can also assign a different port to nginx, for example 8181.

To do so, just define the port in compose file as shown in the following image.

define port nginx container

Define port nginx container

Now the nginx is exposed to port 8181. Let us start the container and check if Nginx is running on 8181 port.

start nginx container

Start nginx container

Open the browser and verify if it is running on 8181 port.

test nginx container in web browser

Test nginx container in web browser

If you want to scale the service, you can do it using command:

# docker-compose up -d --scale database=3
scale service

Scale services using docker compose

To display running services, run:

# docker-compose top
display running services

Display running services using docker compose

To stop, start. restart the whole service at once, the commands would be:

# docker-compose stop
# docker-compose start
# docker-compose restart

We can view logs of the services using command:

# docker-compose logs
show logs of services

Show logs of services

Networking in docker-compose

Docker Compose sets up a single network for your app by default. Each container joins the default network and containers will able to communicate on that default network.

You can however create a new network using compose if you don't want the default network.

For the purpose of this guide, I use the following three files:

  1. Dockerfile
  2. docker-compose.yml
  3. server.py

Here is the contents of Dockerfile:

dockerfile contents

dockerfile contents

Contents of docker-compose.yml:

Docker compose file contents

Docker compose file contents

Contents of server.py:

application file contents

application file contents

Now build the image using command:

# docker-compose build

Once the build is complete, you will see the following output:

build docker image

Build docker image

As you can see the image is successfully built. You can verify it using command:

# docker images
List docker images

List docker images

As you see in the above output, a Docker image named image1 is created.

Now run the compose file:

# docker-compose up -d
run compose file

Run docker compose file

Check if the new network is created or not with command:

# docker network ls
check docker network

Check docker network

From the above screenshot, we can confirm a new network named dc_network_my-network has been created.

Let us check whether container is running or not by using "docker ps" command:

# docker ps
docker ps command output

docker ps command output

Yes, the container is running!

Finally run the application code file (server.py) using curl command:

# curl localhost:15001


# curl
application output

application output

We are getting output - Hello World from node 1! which is written in the server.py file. Meaning it works!

Suggested read:

About the Author:

Dhruv Tiwari is a DevOps Engineer who loves automating things, working with Linux at scale and dream of the day when systems are smart enough to never need to login to a Linux box. Journey Of CI/CD from source code to code deployment to production.


You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More