Docker Compose与Dockerfile - 哪个更好?

Docker Compose 和 Dockerfile 各有其应用场景。Dockerfile 用于构建单个镜像,而 Docker Compose 则用于定义和运行多容器应用。在开发环境中,Docker Compose 可以方便地组合和链接容器,但在生产环境中,建议使用 Dockerfile 构建每个服务的镜像,然后通过 Docker Compose 组合。使用 Docker Compose 可以通过一个命令启动整个应用,并且允许服务间通过名称进行通信,而不是依赖IP。

本文翻译自: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主机创建了一个网桥(网络覆盖)。

### Docker Compose Dockerfile区别及用途 #### 定义功能 Dockerfile 是一种用于定义如何构建镜像的脚本文件。它描述了从基础镜像开始,通过一系列指令逐步构建目标镜像的过程[^4]。这些指令可以包括安装软件包、复制文件到容器中以及设置环境变量等操作。 相比之下,Docker Compose 则专注于管理多容器应用。它可以将多个服务组合在一起,并通过 YAML 文件配置它们之间的依赖关系服务参数[^3]。这使得开发者能够轻松启动整个应用程序栈而无需手动运行每个单独的服务。 #### 使用场景 当只需要创建并部署单一容器时,通常会使用 Dockerfile 来定制化该容器所需的一切资源配置项。然而,在实际开发过程中往往涉及到不止一个相互协作的服务单元;此时便需要用到 Docker Compose 进行编排工作[^1]。 例如,在微服务体系结构下可能有数据库服务器、缓存系统以及其他业务逻辑处理模块等多个组件共同构成整体解决方案——这种情况下借助于Compose工具就可以简化运维流程并提高效率[^2]。 #### 配置方式对比 以下是两者在具体实现上的差异: - **语法形式**: - Dockerfile采用类命令式的语句来指示每一步该如何执行; -Docker Compose则利用YAML格式编写文档以声明各个服务及其属性。 - **作用范围**: - 前者主要负责镜像制作环节中的各项细节设定; - 后者侧重于安排好各独立部分之间交互模式的同时也兼顾到了网络端口映射等方面的内容。 ```yaml version: '3' services: web: build: . ports: - "5000:5000" redis: image: "redis:alpine" ``` 上述例子展示了如何用Compose文件定义两个关联的服务:“web”是从当前目录下的Dockerfile构建出来的,“redis”则是直接拉取官方提供的精简版镜像实例。 ### 总结 综上所述,Dockerfile适用于单个容器镜像的具体构造过程,提供了高度灵活性让用户自定义内部状态;Docker Compose更多关注的是跨不同类型的实体间协调一致的工作流设计,从而满足复杂项目需求.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值