Docker Compose vs. Dockerfile - which is better?

2019-01-29 15:03发布

I have been reading up and learning about Docker, and am trying to correctly choose the Django setup to use. So far there is either:

Docker Compose or Dockerfile

I understand that Dockerfiles are used in Docker Compose, but I am not sure if it is good practice to put everything in one large Dockerfile with multiple FROM commands for the different images?

I want to use several different images that include:

uwsgi
nginx
postgres
redis
rabbitmq
celery with cron

Please advise on what is best practices in setting up this type of environment using Docker.

If it helps, I am on a Mac, so using boot2docker.

Some Issues I've had:

  1. Docker Compose is not compatible with Python3
  2. I want to containerize my project, so if one large Dockerfile is not ideal, then I feel I'd need to break it up using Docker Compose
  3. I am ok to make the project Py2 & Py3 compatible, so am leaning towards django-compose

7条回答
做自己的国王
2楼-- · 2019-01-29 15:28

"better" is relative. It all depends on what your needs are. Docker compose is for orchestrating multiple containers. If these images already exist in the docker registry, then it's better to list them in the compose file. If these images or some other images have to be built from files on your computer, then you can describe the processes of building those images in a Dockerfile.

I understand that Dockerfiles are used in Docker Compose, but I am not sure if it is good practice to put everything in one large Dockerfile with multiple FROM commands for the different images?

Using multiple FROM in a single dockerfile is not a very good idea because there is a proposal to remove the feature. 13026

If for instance, you want to dockerize an application which uses a database and have the application files on your computer, you can use a compose file together with a dockerfile as follows

docker-compose.yml

mysql:
  image: mysql:5.7
  volumes:
    - ./db-data:/var/lib/mysql
  environment:
    - "MYSQL_ROOT_PASSWORD=secret"
    - "MYSQL_DATABASE=homestead"
    - "MYSQL_USER=homestead"
  ports:
    - "3307:3306"
app:
  build:
    context: ./path/to/Dockerfile
    dockerfile: Dockerfile
  volumes:
    - ./:/app
  working_dir: /app

Dockerfile

FROM php:7.1-fpm 
RUN apt-get update && apt-get install -y libmcrypt-dev \
  mysql-client libmagickwand-dev --no-install-recommends \
  && pecl install imagick \
  && docker-php-ext-enable imagick \
  && docker-php-ext-install pdo_mysql \
  && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
查看更多
太酷不给撩
3楼-- · 2019-01-29 15:30

The answer is neither.

Docker Compose (herein referred to as compose) will use the Dockerfile if you add the build command to your project's docker-compose.yml.

Your Docker workflow should be to build a suitable Dockerfile for each image you wish to create, then use compose to assemble the images using the build command.

You can specify the path to your individual Dockerfiles using build /path/to/dockerfiles/blah where /path/to/dockerfiles/blah is where blah's Dockerfile lives.

查看更多
Juvenile、少年°
4楼-- · 2019-01-29 15:30

In my workflow, I add a Dockerfile for each part of my system and configure it that each part could run individually. Then I add a docker-compose.yml to bring them together and link them.

Biggest advantage (in my opinion): when linking the containers, you can define a name and ping your containers with this name. Therefore your database might be accessible with the name db and no longer by its IP.

查看更多
Deceive 欺骗
5楼-- · 2019-01-29 15:31

The Compose file describes the container in its running state, leaving the details on how to build the container to Dockerfiles. http://deninet.com/blog/1587/docker-scratch-part-4-compose-and-volumes

When you define your app with Compose in development, you can use this definition to run your application in different environments such as CI, staging, and production. https://docs.docker.com/compose/production/

It is also seems that Compose is considered production safe as of 1.11, since https://docs.docker.com/v1.11/compose/production/ no longer has a warning not to use it in production like https://docs.docker.com/v1.10/compose/production/ does.

查看更多
看我几分像从前
6楼-- · 2019-01-29 15:36

Dockerfiles are to build an image for example from a bare bone Ubuntu, you can add mysql called mySQL on one image and mywordpress on a second image called mywordpress.

Compose YAML files are to take these images and run them cohesively. for example if you have in your docker-compose.yml file a service call db:

services:
   db:
     image: mySQL  --- image that you built.

and a service called worpress such as:

wordpress: 
    image: mywordpress

then inside the mywordpress container you can use db to connect to your mySQL container. This magic is possible because your docker host create a network bridge (network overlay).

查看更多
甜甜的少女心
7楼-- · 2019-01-29 15:43

Dockerfile

enter image description here

A Dockerfile is a simple text file that contains the commands a user could call to assemble an image.

Example, Dockerfile

FROM ubuntu:latest
MAINTAINER john doe 

RUN apt-get update
RUN apt-get install -y python python-pip wget
RUN pip install Flask

ADD hello.py /home/hello.py

WORKDIR /home

Docker Compose

enter image description here

Docker Compose

  • is a tool for defining and running multi-container Docker applications.

  • define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.

  • get an app running in one command by just running docker-compose up

Example, docker-compose.yml

version: '3'
services:
  web:
    build: .
    ports:
    - "5000:5000"
    volumes:
    - .:/code
    - logvolume01:/var/log
    links:
    - redis
  redis:
    image: redis
volumes:
  logvolume01: {}
查看更多
登录 后发表回答