Home VirtualizationContainers Introduction To Docker Compose
introduction to docker compose

Introduction To Docker Compose

By editor
184 Views

Docker Compose is a tool for defining and running multi-container docker applications. 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.

docker compose diagram

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

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

Or,

# docker-compose -v

Or,

# 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

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

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

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

docker compose file 1

Now let us run the compose file using command:

# docker-compose up -d

It will start all services with single command.

run docker compose

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

# docker-compose ps

list running containers

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

# docker-compose down

stop 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

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

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

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

To display running services, run:

# docker-compose top

display running services

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

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

Contents of docker-compose.yml:

Docker compose file contents

Contents of server.py:

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

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

# 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

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

# docker network ls

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

Yes, the container is running!

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

# curl localhost:15001

Or,

# curl 10.26.35.244:15001

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.


Resource:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

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. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More