Skip to content

Instantly share code, notes, and snippets.

@sam-ngu
Last active October 10, 2020 05:26
Show Gist options
  • Save sam-ngu/df2e8b3d5caffaf2e1ae4f1227595fb7 to your computer and use it in GitHub Desktop.
Save sam-ngu/df2e8b3d5caffaf2e1ae4f1227595fb7 to your computer and use it in GitHub Desktop.
Docker compose example
# we are using the v3.7 syntax to write this docker compose file
version: "3.7"
# define a list
networks:
backend:
# the services section tells docker compose how to run the docker images
# a service can have multiple containers, all based on the same image
services:
# there are 2 ways to build a service
#
# 1. extends from a Dockerfile using the 'build' option
# 2. use an existing image from dockerhub using the 'image' option
# In our example here, php-app uses the 'build' option while mysql uses 'image'
# we define the 'php-app' service
# you can name this to anything you want
php-app:
build:
# assume there is a Dockerfile exist in the specified dir
# context is a suboption of 'build'
context: ./php-app # path to the docker folder containing Dockerfile
# an alternate dockerfile to build this image
# dockerfile: Dockerfile-alt
args: # arguments supply to the docker file
- PHP_VERSION=7.4.1-fpm-buster
# port forwarding - binding host port to container's port
ports:
- "3001:80"
# list of directories to set up as volumes
volumes:
- "../src/index.php:/var/www/html/index.php"
# the starting working path when 'ssh' into docker container
working_dir: "/app"
# the php-app container will only run once the services below are ready
# in this case, php-app is only dependent on mysql
depends_on:
- mysql
# the networks that php-app will connect to
# the network name corresponds to the top level network key defined above
networks:
- backend
# exposing a port to internal network.
# They will only accessible to linked services, ie within the same network
# in this case, the port will be exposed to the 'backend' network
expose:
- 80
- ${PHP_PORT} # env variable read from .env
# list of hostname mapping, added to container's host file entry
extra_hosts:
- "testsite.dev.local:0.0.0.0"
env_file: # path of env file to reference env var in dockerfile
- "./.env"
environment: # adding one-off env vars
- PHP_IDE_CONFIG=vscode # env var to pass to container
# same as cmd in dockerfile. Will override the default command
command: "echo hello!!"
# or list form:
# command: ["echo", "hello!!!"]
# we can supply a script to entrypoint
entrypoint: ./entrypoint.sh # same as entrypoint in dockerfile
# or list form:
# entrypoint: ["echo", "hello"]
# defining the mysql service
mysql:
# using prebuild image
# if we have the 'build' and 'image' option simultaneously
# docker will build the image from the dockerfile, and tag the image with
# value provided in the image option
image: 'mysql:8.0.21'
networks:
- backend
expose:
- 3306
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment