Skip to content

Instantly share code, notes, and snippets.

@ccdle12
Last active August 21, 2019 10:35
Show Gist options
  • Save ccdle12/6095c492af084f67a0b730dd01d75865 to your computer and use it in GitHub Desktop.
Save ccdle12/6095c492af084f67a0b730dd01d75865 to your computer and use it in GitHub Desktop.
Best Practice - "Coding Convention" for Docker-Compose

Docker Compose

This file is documenting best practices when it comes to using docker-compose files.

File Organization

  • Each service should be sorted in alpabetical order
  • Each variable in environment should be sorted in alphabetical order

Service Structure

  • Each service must have a container_name and it must be the same as the service name.
 version: '3'
  services:
    api_gateway:
      container_name: api_gateway    
      ...
  • Each service must have the following structure (where applicable)
service_name:
  container_name:
  build: *OPTIONAL - if building images locally from a Dockerfile.*
  image: *OPTIONAL - if pulling images from dockerhub repo.*
  restart: *OPTIONAL - if container needs to continually restart or reconnect on failure.*
  volumes:
    - local_folder:container_folder
  environment:
    - container_env_variable=$variable_from_env_file
  ports: *OPTIONAL - if container needs to publicly expose ports.*
    - $some_port:$some_port
  expose: *OPTIONAL - if contianer only needs to expose ports within private network.*
    - $some_private_port
  networks:
    - network_name

Network Modes

host

Allows different docker containers to communicate via the localhost.

Important to note, this means each docker container that exposes a port, has each others ports exposed inside of the containers.

At the moment can't interact from an external to the exposed ports.

ports:
      # Only internally exposed.
      - $P2P_PORT:$P2P_PORT
      - $RPC_PORT:$RPC_PORT
      network_mode: host

Create a network

$ docker network create --subnet=172.0.0.0/25 local_dev
$ docker network inspect local_dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment