问题描述
是否可以创建在构建映像时在主机上执行命令的 Dockerfile
?
Is it possible to create Dockerfile
that executes a command on host when image is being build?
现在我正在做
./script_that_creates_magic_file.sh
docker build .
使用Dockerfile:
with Dockerfile:
FROM alpine
COPY magic_file
我想成为能够做到:
docker build .
使用Dockerfile:
with Dockerfile:
FROM alpine
# invoke script_that_creates_magic_file.sh on the host
COPY magic_file
当然,此脚本与Dockerfile位于同一目录。
Of course, this script is in the same directory as Dockerfile.
推荐答案
(仅是建议)
我们通常使用以下结构来构建docker镜像:
We usually have the following structure for building our docker images:
my-image/
├── assets
│ ├── entrypoint.sh
│ └── install.sh
├── build.sh
├── Dockerfile
├── README.md
└── VERSION
- build.sh :这是您应该调用
script_that_creates_magic_file.sh
的原因。其他常见任务包括下载所需文件或从主机临时复制ssh密钥。最后,此脚本将调用docker build。
- Dockerfile :照常,但取决于数量我们需要运行的命令中,我们可能有一个
install.sh
- install.sh :这是复制并在容器中运行,安装软件包,删除不必要的文件等。在没有100%确定的情况下-我认为这种方法减少了层数,避免了在单个
RUN $ c $中使用多个命令c>
- entrypoint.sh :容器的入口点。允许我们在容器启动时执行任务(例如解析环境变量)并打印调试信息
- build.sh: This is were you should invoke
script_that_creates_magic_file.sh
. Other common tasks involve downloading required files or temporarily copying ssh keys from the host. Finally, this script will calldocker build .
- Dockerfile: As usual, but depending on the number of commands we need to run we might have an
install.sh
- install.sh: This is copied and run inside the container, installs packages, removes unnecessary files, etc. Without being 100% sure - I think such an approach reduces the number of layers avoiding multiple commands in a single
RUN
- entrypoint.sh: Container's entrypoint. Allows us to perform tasks when the container starts (like parse environment variables) and print debugging info
我发现上述结构既方便又自-文档化,因为团队中的每个人都可以构建任何图像(无特殊说明/步骤)。自述文件在那里解释图像的作用...但是我不会对你说谎...它通常是空的...(或具有 h1
供gitlab显示):)
I find the above structure convenient and self-documented since everyone in the team can build any image (no special instructions/steps). The README is there to explain what the image is doing... but I won't lie to you... it is usually empty... (or has an h1
for the gitlab to display) :)
这篇关于在Docker构建期间在主机上执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!