Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
643 views
in Technique[技术] by (71.8m points)

dockerfile - Execute command on host during docker build

Is it possible to create Dockerfile that executes a command on host when image is being build?

Now I'm doing:

./script_that_creates_magic_file.sh
docker build .

with Dockerfile:

FROM alpine
COPY magic_file

I want to be able to do:

docker build .

with Dockerfile:

FROM alpine
# invoke script_that_creates_magic_file.sh on the host
COPY magic_file

Of course, this script is in the same directory as Dockerfile.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(Just a suggestion)

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: 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 call docker 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

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) :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...