Running a multi-container application with docker-compose

Objectives

Run a sample multi-container app using docker-compose.

docker-compose uses a file to describe multiple containers and how they connect to each other.

(NOTE: docker-compose is really intended for development use, not production. For example, it can only run all the containers on a single host, and it won’t restart containers if they crash)

Login as root

ssh into your group’s host server, and switch to the “root” user:

$ sudo -s
#

Install docker-compose

(Skip to the next section if docker-compose has already been installed)

# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose

Check that it runs:

# docker-compose --version
docker-compose version 1.29.2, build 5becea4c

Download docker-compose files

The application we’re going to deploy is called “Netbox” - a tool for documenting your data center.

The first thing to do is to download the files needed:

# cd /root
# git clone https://github.com/netbox-community/netbox-docker.git
# cd netbox-docker
# cat docker-compose.yml

cat docker-compose.yml displays the contents of that file to the screen. Have a quick look at it: this is quite a complicated one, more than a screenful! It describes five containers: netbox, netbox-worker, netbox-housekeeping, postgres and redis. It also defines some data volumes for persistent storage.

If you look carefully, you can see it references some other files: env/netbox.env, env/postgres.env, env/redis.env which you can also inspect:

# cat env/netbox.env

# cat env/postgres.env

# cat env/redis.env

These contain environment file settings, like you’ve passed with -e before - such as passwords for the services to be able to communicate with each other.

There’s one thing missing at the moment, which is the port number for Netbox to listen on. The Netbox authors don’t know which ports are free on your system, so they have left it to you to choose one. Let’s choose port 8000.

To do this, still in the directory /root/netbox-docker, create a file called docker-compose.override.yml with the following contents:

version: '3.4'
services:
  netbox:
    ports:
      - 8000:8080

This forwards incoming connections on port 8000 to the container, which is listening on port 8080.

Run the container

Still in the /root/netbox-docker directory, type:

# docker-compose up

You should see it pulling down all the required container images (which are in multple layers, downloaded separately), and then starts them.

If you see the following error you can ignore it:

netbox-housekeeping_1  | psycopg2.errors.UndefinedTable: relation "django_session" does not exist
netbox-housekeeping_1  | LINE 1: DELETE FROM "django_session" WHERE "django_session"."expire_...
netbox-housekeeping_1  |                     ^

(The “netbox-housekeeping” container does periodic database cleanups, but here it has run before the database has even been created!)

If everything is successful, you should be able to go to

http://hostX.ws.nsrc.org:8000/

(this is http not https this time), and get access to the Netbox application. If you want to login, the username and password are “admin” and “admin”.

When you’ve finished, go back to the console where docker-compose is running, and hit Ctrl-C to shut it down cleanly:

^CGracefully stopping... (press Ctrl+C again to force)
Stopping netbox-docker_netbox_1                ... done
Stopping netbox-docker_netbox-worker_1         ... done
Stopping netbox-docker_netbox-housekeeping_1   ... done
Stopping netbox-docker_postgres_1              ... done
Stopping netbox-docker_redis-cache_1           ... done
Stopping netbox-docker_redis_1                 ... done