More on the Docker Compose File
Learn more properties that you can add in the docker-compose.yml file.
Attach persistent Docker volumes
Mount a Docker volume (created on first use) or bind a host directory:
mycontainer:
volumes:
#Docker volume
- type: volume
source: rootfiles
target: /var/www/html
#bind host directory
- type: bind
source: ./myfiles
target: /var/www/html/myfiles
A shorter syntax can also be used to define <source>:<destination>
. The <source>
is presumed to be Docker volume unless it starts with .
or ..
relative file paths.
mycontainer:
#idential to above
volumes:
- rootfiles:/var/www/html
- ./myfiles:/var/www/html/myfiles
Docker volumes (and optional configurations) must be referenced after the services:
definition at the bottom of docker-compose.yml
:
volumes:
rootfiles:
đź“Ś The containers you specify in your Compose file are under your
services
tag.
Set a custom dns
server
Define one or more DNS servers:
mycontainer:
dns:
- 1.1.1.1
- 8.8.8.8
expose
ports
Ports can be exposed to the host using “host:container” notation:
mycontainer:
expose:
- "3000:3000"
Using a single container value, such as:
mycontainer:
expose:
- "3000"
It only exposes the port to other containers on the same Docker network and not to the host this file is running on.
More than one port can be exposed for a container and even to a different port, e.g.,
nodejs:
expose:
- "8000:3000"
- "9229:9229"
The default port 3000
of the container or service nodejs
is mapped on 8000
so now it’s available on 8000
. Moreover, if you want to use devTools for Node.js debugging , you need to expose the 9229
port to 9229
, which can be done by adding another port under the expose
property.
Define external_links
to other containers
Containers started outside the current docker-compose.yml
file can be referenced assuming they are on the same Docker network:
mycontainer:
external_links:
- mysql
Override the defaults
Override the Dockerfile
CMD
command using shell or exec
(array) forms:
mycontainer:
command: node ./test.js
#or [ "node", "./test.js" ]
Override the Dockerfile
ENTRYPOINT
command using shell or exec
(array) forms:
mycontainer:
entrypoint: node ./test.js
#or [ "node", "./test.js" ]
Specify a restart
policy
Restart policies include:
no
: never restart the container (the default)always
: always restart the container when it stopson-failure
: restart the container when it fails with an exit code
mycontainer:
restart: on-failure
Run a healthcheck
Configure a check that is run periodically to check a container is alive and responding:
mycontainer:
healthcheck:
test: [ "CMD", "curl", "-f", "http://localhost:3000/test" ]
interval: 1m00s
timeout: 10s
retries: 3
Define a logging service
A log can be output using the driver
of "json-file"
, "syslog"
, or "none"
:
mycontainer:
logging:
driver: syslog
options:
syslog-address: "tcp://192.168.1.100:1234"
The option "none"
does not display logs on the console. For both options, "syslog"
and "json-file"
, the log-driver
and log-opts
keys need to be set with appropriate values in the daemon.json
.
Get hands-on with 1400+ tech skills courses.