本文翻译自: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 Compose或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? 我知道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: 我遇到的一些问题:
- Docker Compose is not compatible with Python3 Docker Compose与Python3不兼容
- 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来解决它
- 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.ymlso 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主机创建了一个网桥(网络覆盖)。
Docker Compose 和 Dockerfile 各有其应用场景。Dockerfile 用于构建单个镜像,而 Docker Compose 则用于定义和运行多容器应用。在开发环境中,Docker Compose 可以方便地组合和链接容器,但在生产环境中,建议使用 Dockerfile 构建每个服务的镜像,然后通过 Docker Compose 组合。使用 Docker Compose 可以通过一个命令启动整个应用,并且允许服务间通过名称进行通信,而不是依赖IP。


1万+

被折叠的 条评论
为什么被折叠?



