Skip to main content

Command Palette

Search for a command to run...

What is a docker-compose.yml file?

Published
2 min readView as Markdown

A docker-compose.yml is a config file for docker-compose.

it allows to deploy, combine and configure multiple docker-container at the same time. the Docker "rule" is to outsource every single process to an own docker container.

for example a simple web docker-compose. you need a server, a database, and PHP. so you can set three docker-container with Apache2, PHP, and MySQL.

the advantage of docker-compose is the easy configuration. you don't have to write a big bunch of parameters into the bash. You can predefine it in the docker-compose.yml.

db:
    image: mysql
    ports:
        - "3306:3306"
    environment:
        MYSQL_DATABASE: example_db
        MYSQL_USER: root
        MYSQL_PASSWORD: rootpw
php:
    image: php
    ports:
        - "80:80"
        - "443:443"
    volumes:
        - ./SRC:/var/www/
    links:
        - db

as you can see in my example. I set the ports, volumes for external data, and links to the other docker container. It's fast, reproducible, and not that hard to understand.

Docker-compose is a tool that allows you to deploy and manage multiple containers at the same time. docke-compose.yml file contains instructions on how to do that. In this file, you instruct Docker-compose for example:

From where to take Dockerfile to build particular image Which ports you want to expose How to link containers Which ports you want to bind to host machine etc... Docker-compose reads that file and executes commands. It is used instead of all optional parameters when building and running a single docker container.

version: '2'
services:
  nginx:
      build: ./nginx
      links:
          - django:django
          - angular:angular
      ports:
          - "80:80"
          - "8000:8000"
          - "443:443"
      networks:
       - my_net
  django:
      build: ./django
      expose:
          - "8000"
      networks:
       - my_net
  angular:
      build: ./angular2
      links:
          - django:django
      expose:
          - "80"
      networks:
        - my_net
networks:
  my_net:
    external:
      name: my_net

This example instructs Docker-compose to:

Build nginx from path ./nginx Links Angular and django containers (so their IP in docker network is resolved by name) Binds ports 80, 443, 8000 to the host machine Add it to network my_net (so all 3 containers are in the same network and therefore accessible from each other) Then similar is done for django and Angular container. If you would use just docker commands, it would be something like:

https://stackoverflow.com/questions/44450265/what-is-a-docker-compose-yml-file/44450631

More from this blog

Khang Nguyen

119 posts