docker compose
- subcommand compose
(since
Compose V2)docker-compose
- a separate program for Compose V1Typical steps to use Docker Compose.
Dockerfile
docker-compose.yml
orcompose.yml
(since Docker Engine
version 20.10)docker compose up
to start and run your entire
appdocker compose down
to stop your appDefault Docker Compose file is
docker-compose.yml
orcompose.yml
(since Docker Engine
version 20.10)The file provides:
The file follows YAML file format.
The file contains one or more top-level elements.
Top-level elements:
version
- obsoletedname
- application nameservices
- (required) one or more
services in the applicationvolumes
- any volume used by the applicationnetworks
- any network used by the applicationconfigs
- configurations used by services in the
applicationsecrets
- any sensitive data used in the
applicationblock collections
use indentation for scopesequences
use a dash and space
("- "
) for each itemmappings
use a colon and space
(": "
) for each key/value paircomments
begin with an octothorpe
"#"
also called a "hash", "sharp",
"pound" or "number sign"american:
- Boston Red Sox
- Detroit Tigers
- New York Yankees
national:
- New York Mets
- Chicago Cubs
- Atlanta Braves
or
or
or
Minimum contents of docker-compose.yml
:
services
top-level
elementNote the indentation, typically 2 spaces.
my_app
- your custom service name
within a service, there can be one or more keys that configure the container
image
image
- specify the image to start the container
from
the format is the same for instuction
FROM
in Dockerfile
Format:
[<registry>/][<project>/]<image>[:<tag>|@<digest>]
Examples:
ports
ports
- specify container ports to hostFormat: short syntax
[HOST:]CONTAINER[/PROTOCOL]
where:
HOST
is [IP:](port | range)
CONTAINER
is port | range
PROTOCOL
is tcp
, udp
or
platform-specific protocol namesHOST:CONTAINER
should be specified as a (quoted)
string
ports
(2)Examples:
ports
(3)Format: long syntax
target:
the container portpublished:
the publicly exposed port, cn be a
rangehost_ip:
the host IP mapping, unspecified means all
network interfaces (0.0.0.0)protocol
: the port protocol (tcp or udp), unspecified
means any protocolmode:
host
for publishing a host port on each nodeingress
for a port to be load balancedports
(4)Examples:
Run all services in the application:
docker compose up --detach
[+] Running 2/2
⠿ Network ex1_default Created 0.1s
⠿ Container ex1-my_app-1 Started 0.9s
Note:
docker-compose.yml
will be used to scope all resources for
the applicationExplaination
compose
- management command for Compose APIup
- subcommand to create and start containers--detach
- run containers in the backgroundList containers created by Compose.
docker compose ps
NAME COMMAND SERVICE STATUS PORTS
ex1-my_app-1 "/docker-entrypoint.…" my_app running 0.0.0.0:8001->80/tcp
Stop and remove all services in the application.
docker compose down
[+] Running 2/2
- Container ex1-my_app-1 Removed 0.7s
- Network ex1_default Removed 0.2s
Explaination
down
- subcommand to stop and remove containers,
networksSimple docker-compose.yml
using local
Dockerfile
build
build
- specify the build configuration for creating
container imageFormat: string syntax
build: path
path
Dockerfile
Dockerfile
Create a simple Dockerfile
with the following
contents.
Run all services in the application:
docker compose up --detach
[+] Building 0.2s (5/5) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 31B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/nginx:alpine 0.0s
=> CACHED [1/1] FROM docker.io/library/nginx:alpine 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:c1eb88974f71d9e4831be1314fc28d633075f0b644c256664e36e02d8578f030 0.0s
=> => naming to docker.io/library/ex2-my_app 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 2/2
- Network ex2_default Created 0.0s
- Container ex2-my_app-1 Started 0.7s
docker compose ps
NAME COMMAND SERVICE STATUS PORTS
ex2-my_app-1 "/docker-entrypoint.…" my_app running 0.0.0.0:8002->80/tcp
Stop and remove all services in the application.
docker compose down
[+] Running 2/2
- Container ex2-my_app-1 Removed 0.7s
- Network ex2_default Removed 0.3s
Simple docker-compose.yml
using local
Dockerfile
build
Format:
build:
context: path
dockerfile: path
args:
VAR: value
build:
context: path
dockerfile: path
args:
- VAR=value
context
- (required) a path to a directory containing a
Dockerfile, or a url to a git repositorydockerfile
- relative path from build context to
Dockerfile
args
- a map or a list of variable-value for build
stepDockerfile
Create a simple Dockerfile
with the following
contents.
Run all services in the application:
docker compose up --detach
[+] Building 0.2s (5/5) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 56B 0.0s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/nginx:alpine 0.0s
=> CACHED [1/1] FROM docker.io/library/nginx:alpine 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:1b4590ffd7d76c57e3337056ef46dfb461fc9e5c6e579c82d54a3bbc6d25204c 0.0s
=> => naming to docker.io/library/ex3-my_app 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 2/2
- Network ex3_default Created 0.0s
- Container ex3-my_app-1 Started 0.8s
docker compose ps
NAME COMMAND SERVICE STATUS PORTS
ex3-my_app-1 "/docker-entrypoint.…" my_app running 0.0.0.0:8003->80/tcp
Stop and remove all services in the application.
docker compose down
[+] Running 2/2
- Container ex3-my_app-1 Removed 0.8s
- Network ex3_default Removed 0.3s
With multiple services (containers)
build
In subdirectory my_app_a
, create
Dockerfile_a
In subdirectory my_app_b
, create
Dockerfile_b
Directory structure for this example:
.
├── my_app_a
│ └── Dockerfile_a
├── my_app_b
│ └── Dockerfile_b
└── docker-compose.yml
Build any image if not already exists.
docker compose build
[+] Building 1.3s (9/9) FINISHED
=> [ex4-app_b internal] load build definition from Dockerfile_b 0.0s
=> => transferring dockerfile: 33B 0.0s
=> [ex4-app_a internal] load build definition from Dockerfile_a 0.1s
=> => transferring dockerfile: 33B 0.0s
=> [ex4-app_b internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [ex4-app_a internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [ex4-app_b internal] load metadata for docker.io/library/httpd:alpine 1.1s
=> [ex4-app_a internal] load metadata for docker.io/library/nginx:alpine 0.0s
=> CACHED [ex4-app_a 1/1] FROM docker.io/library/nginx:alpine 0.0s
=> [ex4-app_b] exporting to image 0.1s
=> => exporting layers 0.0s
=> => writing image sha256:4111470cf2a4ff6157bdcdb63bca685fb2116f0ce1e72f3538bad259e5dcbdf8 0.0s
=> => naming to docker.io/library/ex4-app_a 0.0s
=> => writing image sha256:1a40790611892a64c25f1aad1af0935379a16f269d155e9d38ff555fed976d28 0.0s
=> => naming to docker.io/library/ex4-app_b 0.0s
=> CACHED [ex4-app_b 1/1] FROM docker.io/library/httpd:alpine@sha256:4658c554fe215c8a17d57adbd9f8595e4922abda40d 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Run all services in the application:
docker compose up --detach
[+] Running 3/3
- Network ex4_default Created 0.0s
- Container ex4-app_a-1 Started 2.6s
- Container ex4-app_b-1 Started 2.6s
docker compose ps
NAME COMMAND SERVICE STATUS PORTS
ex4-app_a-1 "/docker-entrypoint.…" app_a running 0.0.0.0:8004->80/tcp
ex4-app_b-1 "httpd-foreground" app_b running 0.0.0.0:9004->80/tcp
Stop and remove all services in the application.
docker compose down
[+] Running 3/3
- Container ex4-app_b-1 Removed 1.5s
- Container ex4-app_a-1 Removed 0.7s
- Network ex4_default Removed 0.2s
phpAdmin
with mysql
database.
default.env
Run all services in the application:
docker compose up --detach
[+] Running 4/4
- Network ex5_db-net Created 0.1s
- Volume "ex5_db-mysql" Created 0.0s
- Container ex5-db-1 Started 1.1s
- Container ex5-pma-1 Started 1.6s
docker compose ps
NAME COMMAND SERVICE STATUS PORTS
ex5-db-1 "docker-entrypoint.s…" db running 3306/tcp, 33060/tcp
ex5-pma-1 "/docker-entrypoint.…" pma running 0.0.0.0:8005->80/tcp
docker volume ls
DRIVER VOLUME NAME
local ex5_db-mysql
docker network ls
NETWORK ID NAME DRIVER SCOPE
60bf4ba277f2 bridge bridge local
12b6d75e9106 ex5_db-net bridge local
a8c4ebbecec3 host host local
8a9fe937f5ac none null local
Stop and remove all services in the application.
docker compose down
[+] Running 3/3
- Container ex5-pma-1 Removed 0.5s
- Container ex5-db-1 Removed 1.9s
- Network ex5_db-net Removed 0.3s
adminer
with mariadb
database.
default.env
Run all services in the application:
docker compose up --detach
[+] Running 4/4
- Network ex6_db-net Created 0.1s
- Volume "ex6_db-mariadb" Created 0.0s
- Container ex6-db-1 Started 1.4s
- Container ex6-pma-1 Started 2.2s
docker compose ps
NAME COMMAND SERVICE STATUS PORTS
ex6-db-1 "docker-entrypoint.s…" db running 3306/tcp
ex6-pma-1 "entrypoint.sh docke…" pma running 0.0.0.0:8006->8080/tcp
docker volume ls
DRIVER VOLUME NAME
local ex6_db-mariadb
local ex6_db-mysql
docker network ls
NETWORK ID NAME DRIVER SCOPE
60bf4ba277f2 bridge bridge local
be8e5de3ef37 ex6_db-net bridge local
a8c4ebbecec3 host host local
8a9fe937f5ac none null local
Stop and remove all services in the application.
docker compose down
[+] Running 3/3
- Container ex6-pma-1 Removed 0.5s
- Container ex6-db-1 Removed 1.0s
- Network ex6_db-net Removed 0.3s