Docker Compose
Docker Compose
#docker-
compose
Table of Contents
About 1
Remarks 2
Examples 2
Installation 2
Remarks 7
Examples 7
Introduction 9
Examples 9
Credits 10
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version
from: docker-compose
It is an unofficial and free docker-compose ebook created for educational purposes. All the content
is extracted from Stack Overflow Documentation, which is written by many hardworking individuals
at Stack Overflow. It is neither affiliated with Stack Overflow nor official docker-compose.
The content is released under Creative Commons BY-SA, and the list of contributors to each
chapter are provided in the credits section at the end of this book. Images may be copyright of
their respective owners unless otherwise specified. All trademarks and registered trademarks are
the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor
accurate, please send your feedback and corrections to [email protected]
https://riptutorial.com/ 1
Chapter 1: Getting started with docker-
compose
Remarks
Compose is a tool for defining and running multi-container Docker applications. With Compose,
you use a Compose file to configure your application’s services. Then, using a single command,
you create and start all the services from your configuration. To learn more about all the features
of Compose see the list of features.
Examples
Installation
On Linux you can get the latest binaries straight from the GitHub release page:
https://github.com/docker/compose/releases
You can install the specific release with the following commands:
This example comes from the official document. Suppose you have a python application using
redis as backend. After writing Dockerfile, create a docker-compose.yml file like this:
version: '2'
services:
web:
build: .
ports:
- "5000:5000"
https://riptutorial.com/ 2
volumes:
- .:/code
depends_on:
- redis
redis:
image: redis
Then run docker-compose up will setup the entire application includes: python app and redis.
A volumes and networks section can be added as well. Using the volumes section allows for
disconnected volume that can live independently of the docker compose services section. The
networks section has a similar result.
redis:
image: redis
volumes:
- redis-data:/code
networks:
-back-tier
Next, add the following sections to the bottom of the docker compose version 2 file.
volumes:
# Named volume
redis-data:
driver: local
networks:
back-tier:
driver: bridge
redis-data provides an accessible label from the services section. driver:local sets the volume to
the local file system.
back-tier sets the networks section label to be accessible in the services section as bridged.
https://riptutorial.com/ 3
If, for example, you wanted to run rake db:create in your web service, you'd use the following
command:
If you get a Permission denied error, Run sudo -i before the two commands below, then
exit.
You can change version 1.7.1 to match your desired version. Try get version from
https://github.com/docker/compose/releases
chmod +x /usr/local/bin/docker-compose
docker-compose --version
version: '2'
services:
hello_world:
image: ubuntu
command: [/bin/echo, 'Hello world']
This file is making it so that there's a hello_world service, that's initialized from the ubuntu:latest
image and that, when it's run, it just runs echo 'Hello world'
If you're on the folder directory (and it contains this docker-compose.yml file), you can do docker-
compose up and you should see
Creating folder_hello_world_1
Attaching to folder_hello_world_1
hello_world_1 | Hello world
folder_hello_world_1 exited with code 0
https://riptutorial.com/ 4
This created the container from the ubuntu image, and ran the command that was specified on the
docker-compose.yml
Docker-Compose uses the folder name as the project name to prefix containers and networks. To set
another project name, you can either call docker-compose --project-name NAME {up|down|...} or you
suppy a file called .env next to your docker-compose.yml and write COMPOSE_PROJECT_NAME=name in it.
Better avoid long project names with hyphens (-) because docker compose bahaves strange with
this kind of names.
Note: docker-compose allows you to run multiple docker containers on a single host. If you want to
run multiple containers on more than one node, please refer to solution such as swarm /
kubernetes.
If you want to use docker for rails app, and use database, you need to know that all the data in the
docker container will be destroyed (unless you configure the container specifically for keeping
data) Sometimes, you need to create a docker container with an application and attach it to an old
container with a database.
As an example of rails application, I used a simple app. You can create it from command:
Then, create Dockerfile in your project, and set this data to it:
FROM ruby:2.3.1
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /compose-app
WORKDIR /compose-app
ADD Gemfile /compose-app/Gemfile
ADD Gemfile.lock /compose-app/Gemfile.lock
RUN bundle install
ADD . /compose-app
version: '2'
services:
db:
image: postgres
web:
build: .
command: bundle exec rails s -e development -p 80 -b '0.0.0.0'
volumes:
- .:/compose-app
ports:
- "80:80"
depends_on:
- db
https://riptutorial.com/ 5
You can replace 80 port (-p 80 ) with another.
development: &default
adapter: postgresql
encoding: unicode
database: postgres
pool: 5
username: postgres
password:
host: db
docker-compose build
docker-compose up
If everything is done correctly, you will be able to see logs from rails in the console.
If you want to delete only the container with the rails application without the database, you need to
run then in project directory:
https://riptutorial.com/ 6
Chapter 2: Docker-compose multi-container
example with default network
Remarks
By default Compose sets up a single network for your app. Each container for a service
joins the default network and is both reachable by other containers on that network,
and discoverable by them at a hostname identical to the container name.
Links allow you to define extra aliases by which a service is reachable from another
service. They are not required to enable services to communicate – by default, any
service can reach any other service at that service’s name.
https://docs.docker.com/compose/networking/
Examples
How to create a basic LAMP environment with default networking
docker-compose.yml
version: '2'
services:
php:
image: phpmyadmin/phpmyadmin
links:
- mysql:db
depends_on:
- mysql
mysql:
image: k0st/alpine-mariadb
volumes:
- ./data/mysql:/var/lib/mysql
environment:
- MYSQL_DATABASE=mydb
- MYSQL_USER=myuser
- MYSQL_PASSWORD=mypass
nginx:
image: nginx:stable-alpine
ports:
- "81:80"
volumes:
- ./nginx/log:/var/log/nginx
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- php
nginx/nginx.conf
https://riptutorial.com/ 7
worker_processes 1;
events {
worker_connections 1024;
}
http {
sendfile off;
server {
listen 80;
location / {
proxy_pass http://php;
proxy_set_header Host $host;
proxy_redirect off;
}
}
}
Note the nginx config is simplified but above should work for testing — basically all it’s doing is
proxying the php app. Maps to port 81 to avoid conflicts on the host - adjust as needed.
Regarding linking, you can see that if you run: docker-compose exec mysql ping -c2 nginx to ping
from the mysql container to the nginx container, you will succeed even though there are no links
specified between these containers. Docker Compose will maintain those links in the default
network for you.
https://riptutorial.com/ 8
Chapter 3: Environment variables external file
Introduction
There a number of ways to include environment variables into the docker application. Here are
some examples:
Examples
Via External File
docker-composer.yml
web:
...
env_file:
- ./filename
filename
variable=value
app:
...
environment:
- var=value
https://riptutorial.com/ 9
Credits
S.
Chapters Contributors
No
Docker-compose
multi-container
2 ldg
example with default
network
Environment
3 Simon Tsang
variables external file
https://riptutorial.com/ 10