version: '3'
services:
container: # this is not the same as container name
container_name: name # must be defined here, especially in a stack, otherwise it will be named based on the current folder
ports:
- 1234:1234/tcp
environment:
- PUID=1000 # define environment variable in this format
volumes:
- /host/path:/container/path
restart: unless-stoppedA basic docker-compose in home settings will consists of service name (container), container_name (name), ports (1234), networks, volumes, environments(PUID) and restart (unless-stopped).
version: '3'
services:
my-ubuntu: # name = folder-name-my-ubuntu
image: ubuntu:latest
my-nginx:
image: nginx
name: compose-nginx # name = compose-nginx- In this example, the container name would be
folder-name-my-ubuntuandfolder-name-my-nginx - Must need to define it under container name for the desired name.
Networking
Adding Container to Custom Network
networks:
- net1
- net2 # attaching to multiple networks
- default- multiple networks can be specified as a list
- when a container is in multiple networks, it will have multiple interfaces with multiple IP addresses
- when specifying
defaultin networks, this is the default network that docker compose will create, eg.folder-name_default
Additional Options for each network:
- when specifying additional options with multiple networks, list cannot work
networks:
net-1:
net-2:
ipv4_address: 192.168.120.254ipv4_addressis specify custom IP address as long as it’s in correct subnet
Network Definition
YAML
networks:
public:
name: public
ipam:
config:
- subnet: 192.168.120.0/24
gateway: 192.168.120.1- however, when defined as such, the network cannot be removed if there is active container, and when changing network configurations
External
networks:
net1:
name: net1
external: true- the
external: truespecify that the network is managed outside of compose and already exists
Volumes
Environments
docker compose config will print out the file with all the environment variables present
environments
environment:
- PUID=1000
- SHELL=${SHELL}
TZ: "America/Vancouver"- the
environmentcan be set as a listX=yor a dictionaryX: 2 - it can also consist of shell variables with
${}syntax$default value if such variable is unset, similar to bash-variables
- environment variables will take precedence over env_file
Command substitution such as$(id -u)does not work, consider using system variables
The commands also work from non-env keys
user: ${PUID:-1000}:${PGID:-1000}- when starting the container it will run with PUID/PGID defined in shell variables
- it’s a good idea to export these in bashrc for seamless deployment
env_file
env_file:
- secret.env
- ../folder/config.env- using
env_filekey, the files are provided at list - the file’s location can be given as relative position or in another folder
- the file that is defined first will take precedence if duplicate arise
Long Strings
For multiline strings use | and for single line strings use >
multiline: |
this should be multiple lines
singleline: >
this is all in one lineThe strings has to be indented correctly.
Interactive
stdin_open: true
tty: trueThis is equivalent to docker run -it