简体   繁体   中英

What is a docker-compose.yml file?

I can't find a real definition of what is a docker-compose file ?

Is it correct to say this ? :

A docker-compose file is a YAML file who allow us to deploy multiples docker containers at the same time.

I'd like to explain a bit better what is a docker-compose file

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.

Example:

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:

docker build --name nginx .
docker run --link django:django angular:angular --expose 80 443 8000 --net my_net nginx

So while you probably don't want to type all these options and commands for each image/container, you can write docker-compose.yml file in which you write all these instructions in a human-readable format.
(Same can be achieved with Ansible )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM