Docker撰写指定映像vs Dockerfile [英] Docker compose specifying image vs Dockerfile

查看:61
本文介绍了Docker撰写指定映像vs Dockerfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 docker-compose 的新手,在阅读了文档之后,我仍然有一些不清楚的事情。



到目前为止,当我使用docker时,我将构建版本保存在以下目录树中:

 构建版本:
服务A:
Dockerfile
ServiceA.jar
服务B:
Dockerfile
ServiceB.jar

因此,当我想全部运行时,我会使用一些shell脚本,直到我读到 docker-compose 为止。 / p>

我看到有两种创建和运行服务的方式(以复制文件的方式)


  1. 指定 build:路径/到/我/构建/目录并将其与
    量关联:,以便它可以看到实时代码并刷新服务

  2. 指定图片:(例如 java:8 ),然后使用 volumes:如上

我想了解的内容在我深入研究它之前,使用 docker-compose
的最佳实践是什么,如果我要创建指定的 image 每个服务(并替换为 Dockerfile 中的 FROM ),还是应该指定构建文件夹的路径和数量以保持实时代码更改,以及使用 image 标签时体积的工作方式及其用法



谢谢!

解决方案

在Docker中,您可以简单地将服务作为容器运行,并且您可以将每个服务的状态放到一个卷中。这对您来说意味着:




  • 该服务作为运行时容器运行,将从图像开始。

  • 服务的二进制文件位于映像内部,服务本身将数据写入卷。

  • 可以从映像存储库中提取映像,也可以在目标环境上构建映像。
  • li>
  • 可以使用命令 docker build 或docker-compose build部分来构建图像。



在您的示例中,这意味着:


  1. 保留目录结构。

  2. 使用docker-compose build部分根据Dockerfile构建映像。

  3. 配置您的Dockerfile以将二进制文件放入映像中。

  4. 只需使用 docker-启动整个堆栈,包括构建-组成-d

  5. 您的二进制文件已更改?只需将整个堆栈替换为 docker-compose up --build --force-recreate -d 。此命令将重建所有映像并替换容器。

为什么不将二进制文件放在卷中:




  • 您将失去映像版本控制的优势。

  • 如果替换二进制文件,您将无法简单地回退到较早的映像版本。

  • 您可以在部署新版本和回退之前重新标记映像,如果发生错误。

  • 您可以标记并保存正在运行的容器,以进行回退和错误调查。


I'm new to docker-compose and after reading the docs I still have some unclear things that comes to my mind.

So far when I used docker I kept the builds in the following directory tree:

builds:
    Service A:
        Dockerfile
        ServiceA.jar
    Service B:
        Dockerfile
        ServiceB.jar

So when I want to run all I use some shell script, until I read about docker-compose.

I saw that there are 2 ways of creating and running a service (in manner of copying files)

  1. Specifying build: path/to/my/build/directory and linking it with volumes: so it can see live code and refresh the service
  2. Specifying image: (for example java:8) and then use the volumes: as above

What I want to understand is what is the best practice using docker-compose before I dive in to it, should I create specify image for each service (and replace with the FROM inside the Dockerfile) or should I specify paths to build folders and volumes to keep live code changes, and how does volumes work and their usages when using image tag

Thanks!

解决方案

In Docker you can simply run services as containers and you can put the state of each service in a volume. This means for you:

  • The service is run as a runtime container which will be started from an image.
  • The service's binaries are inside an image, the service itself writes data to volumes.
  • The image can be either pulled from a image repository or build on the target environment.
  • Images can be build with the command docker build or the docker-compose build section.

In your example this means:

  1. Keep the directory structure.
  2. Use the docker-compose build section to build your immages according your Dockerfiles.
  3. Configure your Dockerfile to put the binaries inside the image.
  4. Simply start the whole stack including the build with docker-compose up -d
  5. Your binaries changed? Simply replace the whole stack with docker-compose up --build --force-recreate -d. This command will rebuild all images and replace containers.

Why you do not place the binaries inside the volume:

  • You lose the advantage of image versioning.
  • If you replace the binaries files you cannot simply fallback to an older image version.
  • You can retag images before you deploy your new version and fallback if an error occurs.
  • You can tag and save running containers for fallback and error investigation.

这篇关于Docker撰写指定映像vs Dockerfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆