Docker : Docker Compose - Defining Multi-Container Applications
This article describes how to use Docker Compose to create multi-container applications.
# curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose # chmod +x /usr/local/bin/docker-compose # docker-compose -version docker-compose version 1.22.0, build f46880fe #
When we were setting up a non-root user for Docker (here) we did the following
sudo and profile setup.
# useradd docker_user # echo "docker_user ALL=(ALL) NOPASSWD: /usr/bin/docker" >> /etc/sudoers # echo "alias docker=\"sudo /usr/bin/docker\"" >> /home/docker_user/.bash_profile # su - docker_user
We can do a similar setup for Docker Compose too.
# echo "docker_user ALL=(ALL) NOPASSWD: /usr/local/bin/docker-compose" >> /etc/sudoers # echo "alias docker-compose=\"sudo /usr/local/bin/docker-compose\"" >> /home/docker_user/.bash_profile # su - docker_user $ docker-compose -version docker-compose version 1.18.0, build 8dd22a9 $
Services are typically defined using YAML in a "docker-compose.yml" file, though JSON files can also be used. The compose file reference, explains how the file is defined. Once you have manually run a couple of containers you will recognise most of the pieces. This file contains a definition of how to start each of the containers that make up the multi-container application, with their associated dependencies, networks and volumes etc.
As an example, check out this docker-compose.yml file, which builds a 12cR2 database container and an ORDS container, allowing you to run APEX applications.
You should navigate to the directory containing the "docker-compose.yml" file before running the following commands.
You can start a service by using the
up commands, but you will typically use the
docker-compose up command. By default it doesn't release the screen, but it can be detached using the "-d" option.
cd ~/dockerfiles/compose/ol7_122_ords # Start containers and hold on to the screen. docker-compose up # Detach : Run containers in the background. docker-compose up -d
When you are using the "-d" option, you don't see the log, but this can be displayed using the following command.
docker-compose logs --follow
If you haven't used the "-d" option, you can use CTRL+C to gracefully stop the containers. If you did use the "-d" option, you will need to use the
docker-compose stop command.
cd ~/dockerfiles/compose/ol7_122_ords # Stop containers. docker-compose stop # Extend the timeout, which defaults to 10 seconds, to allow a graceful DB shutdown. docker-compose stop -t 120 docker-compose stop --timeout 120
You could also remove the services with the stop option, described below.
Services are removed using the
docker-compose rm command. It will remove any stopped containers associated with a service.
cd ~/dockerfiles/compose/ol7_122_ords # Remove any stopped containers associated with services. Requires confirmation. docker-compose rm # No confirmation. docker-compose rm -f docker-compose rm --force # Cleans up associated non-persistent volumes. docker-compose rm -vf # Stops the containers if necessary. docker-compose rm -vsf docker-compose rm -vf --stop
Remember, volumes defined in the "volumes:" section of the "docker-compose.yml" file are persistent, so they won't be removed by the "-v" option. This means they are safe for storing the database files in this example.
For more information see:
- Docker Compose : Defining Multi-Container Applications
- Docker : All Articles
- Overview of Docker Compose
Hope this helps. Regards Tim...