Docker Compose與Dockerfile - 哪個更好?

本文翻譯自:Docker Compose vs. Dockerfile - which is better?

I have been reading up and learning about Docker , and am trying to correctly choose the Django setup to use. 我一直在閱讀並瞭解Docker ,我正在嘗試正確選擇要使用的Django設置。 So far there is either: 到目前爲止,要麼:

Docker Compose or Dockerfile Docker ComposeDockerfile

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? 我知道Dockerfiles是在Docker Compose中使用的,但是我不確定將所有內容放在一個大型Dockerfile中,併爲不同的圖像添加多個FROM命令是不錯的做法?

I want to use several different images that include: 我想使用幾個不同的圖像,包括:

uwsgi
nginx
postgres
redis
rabbitmq
celery with cron

Please advise on what are best practices in setting up this type of environment using Docker . 請告知使用Docker設置此類環境的最佳做法。

If it helps, I am on a Mac, so using boot2docker . 如果它有幫助,我在Mac上,所以使用boot2docker

Some Issues I've had: 我遇到的一些問題:

  1. Docker Compose is not compatible with Python3 Docker Compose與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 我想將我的項目容器化,所以如果一個大的Dockerfile不理想,那麼我覺得我需要使用Docker Compose來解決它
  3. I am ok to make the project Py2 & Py3 compatible, so am leaning towards django-compose 我可以讓Py2和Py3項目兼容,所以我傾向於django-compose

#1樓

參考:https://stackoom.com/question/1zh7T/Docker-Compose與Dockerfile-哪個更好


#2樓

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 . 如果將build命令添加到項目的docker-compose.yml Docker Compose(此處稱爲compose)將使用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. 您的Docker工作流程應該是爲您要創建的每個圖像構建合適的Dockerfile ,然後使用compose使用build命令組合圖像。

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. 您可以使用build /path/to/dockerfiles/blah指定單個Dockerfiles的build /path/to/dockerfiles/blah其中/path/to/dockerfiles/blah是blah的Dockerfile所在的Dockerfile


#3樓

The Compose file describes the container in its running state , leaving the details on how to build the container to Dockerfiles . Compose文件描述了處於運行狀態的容器 ,並詳細說明了如何將容器構建Dockerfiles http://deninet.com/blog/1587/docker-scratch-part-4-compose-and-volumes 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 . 使用Compose in development定義應用程序可以使用此定義在不同的環境(如CI,登臺和生產)中 運行應用程序 https://docs.docker.com/compose/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. 1.11開始 ,Compose似乎也被視爲生產安全 ,因爲https://docs.docker.com/v1.11/compose/production/不再警告不要在生產中使用它,如https:// docs .docker.com / v1.10 / compose / production / does。


#4樓

In my workflow, I add a Dockerfile for each part of my system and configure it that each part could run individually. 在我的工作流程中,我爲系統的每個部分添加了一個Dockerfile,並將其配置爲每個部分可以單獨運行。 Then I add a docker-compose.yml to bring them together and link them. 然後我添加一個docker-compose.yml將它們組合在一起並鏈接它們。

Biggest advantage (in my opinion): when linking the containers , you can define a name and ping your containers with this name. 最大優勢(在我看來): 鏈接容器時 ,您可以定義名稱並使用此名稱ping容器。 Therefore your database might be accessible with the name db and no longer by its IP. 因此,您的數據庫可以使用名稱db訪問,而db其IP訪問。


#5樓

Dockerfile Dockerfile

在此輸入圖像描述

A Dockerfile is a simple text file that contains the commands a user could call to assemble an image. Dockerfile是一個簡單的文本文件,其中包含用戶可以調用以組合圖像的命令。

Example, Dockerfile 例如, 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 Docker撰寫

在此輸入圖像描述

Docker Compose Docker撰寫

  • is a tool for defining and running multi-container Docker applications. 是一個用於定義和運行多容器Docker應用程序的工具。

  • define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment. docker-compose.yml定義構成應用程序的服務,以便它們可以在隔離環境中一起運行。

  • get an app running in one command by just running docker-compose up 通過運行docker-compose up獲取在一個命令中運行的應用程序

Example, docker-compose.yml 例如, docker-compose.yml

version: "3"
services:
  web:
    build: .
    ports:
    - '5000:5000'
    volumes:
    - .:/code
    - logvolume01:/var/log
    links:
    - redis
  redis:
    image: redis
    volumes:
      logvolume01: {}

#6樓

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 . Dockerfiles是從裸骨的Ubuntu建立圖像。例如,你可以添加mysql稱爲mySQL一個圖像和mywordpress稱爲第二圖像mywordpress

Compose YAML files are to take these images and run them cohesively. 撰寫YAML文件是爲了獲取這些圖像並以內聚方式運行它們。 for example if you have in your docker-compose.yml file a service call db : 例如,如果您在docker-compose.yml文件中有一個服務調用db

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

and a service called worpress such as: 以及一項名爲worpress的服務,例如:

wordpress: 
    image: mywordpress

then inside the mywordpress container you can use db to connect to your mySQL container. 然後在mywordpress容器中,您可以使用db連接到mySQL容器。 This magic is possible because your docker host create a network bridge (network overlay). 這種魔力是可能的,因爲您的docker主機創建了一個網橋(網絡覆蓋)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章