添加一个等同于docker-compose文件的Dockerfile RUN命令。

huangapple go评论54阅读模式
英文:

Adding a Dockerfile RUN command equivalent to docker-compose file

问题

有没有一种方法可以在Compose文件中添加一个与Dockerfile的RUN命令等效的命令?

背景:

docker-compose.yaml 文件中有一个名为 webapp 的服务,配有一个相应的 Dockerfile (webapp.Dockerfile)。

在开发时,我有一个名为 docker-compose.local.yaml 的文件,它为 webapp 服务添加了一些额外的开发配置,例如:

services:
  webapp:
    stdin_open: true
    tty: true
    ... # 其他用于我的本地开发需求的内容

我还想运行一些命令到正在构建的镜像中,但我不想将它们放在 webapp.Dockerfile 中的 RUN 命令中。我想以某种方式将它们包含在 docker-compose.local.yaml 中,以便它是我可以追加而不更改基本配置的额外内容。(顺便说一下,我使用 COMPOSE_FILE 环境变量来调用额外的 YAML 文件)。

英文:

Is there a way to add a Dockerfile RUN command equivalent to a compose file?

Context:

docker-compose.yaml has service webapp with an accompanying Dockerfile (webapp.Dockerfile)

When I do development, I have a docker-compose.local.yaml that adds some additional development configurations to webbapp service, e.g.:

services:
  webapp:
    stdin_open: true
    tty: true
    ... # other stuff for my local development needs

I'd also like to run a couple of commands to the image being built, but I do not want to put it in a RUN command in webapp.Dockerfile. I'd like to somehow include it in docker-compose.local.yaml so that it's something additional I can tack on without changing the base configs. (BTW I invoke the additional YAML file with COMPOSE_FILE environment variable).

答案1

得分: 1

一个 RUN 命令是镜像构建过程的一部分;它只能在 Dockerfile 中使用。如果我想要在我的组合配置中使用的镜像中安装额外的命令,我可以基于旧的镜像构建一个新的镜像。

例如,如果我有一个看起来像这样的 docker-compose.yaml 文件:

services:
  web:
    image: docker.io/alpinelinux/darkhttpd:latest

并且我真的希望在这个容器内部可用 curl 命令,我可以创建一个像这样的 docker-compose.override.yaml 文件:

services:
  web:
    build: .

然后,在与 docker-compose.yaml 文件相同的目录中放置一个像这样的 Dockerfile

services:
  web:
    image: development
    build:
      context: .

当运行 docker-compose up 命令时,它将构建自定义镜像并使用它,而不是使用 docker-compose.yaml 中指定的镜像:

$ docker-compose up
Building web
[+] Building 0.1s (6/6) FINISHED
 => [internal] load build definition from Dockerfile 0.1s
 => => transferring dockerfile: 124B 0.0s
 => [internal] load .dockerignore 0.1s
 => => transferring context: 2B 0.0s
 => [internal] load metadata for docker.io/alpinelinux/darkhttpd:latest 0.0s
 => [1/2] FROM docker.io/alpinelinux/darkhttpd:latest 0.0s
 => CACHED [2/2] RUN apk add curl 0.0s
 => exporting to image 0.0s
 => => exporting layers 0.0s
 => => writing image sha256:23e53345322fa1787aa6fd291347732501499f60656805675c6fd4217d040006 0.0s
 => => naming to docker.io/library/development 0.0s
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating containers_web_1 ... done
Attaching to containers_web_1
web_1  | darkhttpd/1.14, copyright (c) 2003-2022 Emil Mikulic.
英文:

A RUN command is part of the image build process; it can only be used in a Dockerfile. If I want to install additional commands into the image used by my compose configuration, I can build a new image based on the old one.

For example, if I have a docker-compose.yaml that looks like this:

services:
  web:
    image: docker.io/alpinelinux/darkhttpd:latest

And I really want the curl command to be available inside this container, I can create a docker-compose.override.yaml like this:

services:
  web:
    build: .

And then put a Dockerfile in the same directory as the docker-compose.yaml that looks like this:

services:
  web:
    image: development
    build:
      context: .

And when docker-compose up the environment, it will build the custom image and use that rather than using the image specified in docker-compose.yaml:

$ docker-compose up
Building web
[+] Building 0.1s (6/6) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                     0.1s
 => => transferring dockerfile: 124B                                                                                                                     0.0s
 => [internal] load .dockerignore                                                                                                                        0.1s
 => => transferring context: 2B                                                                                                                          0.0s
 => [internal] load metadata for docker.io/alpinelinux/darkhttpd:latest                                                                                  0.0s
 => [1/2] FROM docker.io/alpinelinux/darkhttpd:latest                                                                                                    0.0s
 => CACHED [2/2] RUN apk add curl                                                                                                                        0.0s
 => exporting to image                                                                                                                                   0.0s
 => => exporting layers                                                                                                                                  0.0s
 => => writing image sha256:23e53345322fa1787aa6fd291347732501499f60656805675c6fd4217d040006                                                             0.0s
 => => naming to docker.io/library/development                                                                                                           0.0s
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating containers_web_1 ... done
Attaching to containers_web_1
web_1  | darkhttpd/1.14, copyright (c) 2003-2022 Emil Mikulic.

huangapple
  • 本文由 发表于 2023年6月1日 22:16:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定