Can I give an advance instructions to run later, during the next build stage in docker?

0 votes
Is it possible in docker to give an instruction in advance that is supposed to be executed in the next build stage? How can this be possible?
Jul 2 in Docker by Namik
• 1,210 points
62 views

1 answer to this question.

0 votes

You can definitely give a trigger instruction for an image to be executed at a later time, when the image is used as the base for another build. The trigger will be executed in the context of the downstream build, as if it had been inserted immediately after the FROM instruction in the downstream Dockerfile.

The ONBUILD instruction adds to the image a trigger instruction

ONBUILD [INSTRUCTION]

Any build instruction can be registered as a trigger.

This is useful if you are building an image which will be used as a base to build other images, for example an application build environment or a daemon which may be customized with user-specific configuration.

You can use ONBUILD to register advance instructions to run later, during the next build stage.

Here’s how it works:

  1. When it encounters an ONBUILD instruction, the builder adds a trigger to the metadata of the image being built. The instruction does not otherwise affect the current build.
  2. At the end of the build, a list of all triggers is stored in the image manifest, under the key OnBuild. They can be inspected with the docker inspect command.
  3. Later the image may be used as a base for a new build, using the FROM instruction. As part of processing the FROM instruction, the downstream builder looks for ONBUILD triggers, and executes them in the same order they were registered. If any of the triggers fail, the FROM instruction is aborted which in turn causes the build to fail. If all triggers succeed, the FROM instruction completes and the build continues as usual.
  4. Triggers are cleared from the final image after being executed. In other words they are not inherited by “grand-children” builds.

For example you might add something like this:

[...]
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
[...]
answered Jul 2 by Sirajul
• 36,220 points

Related Questions In Docker

0 votes
2 answers

How can I inspect the file system of a failed `docker build

Failed command is a build that has ...READ MORE

answered Aug 6, 2018 in Docker by Nilesh
• 6,880 points
98 views
0 votes
1 answer

How to run a docker command from inside the container?

You must have come across the /var/run/docker.sock file, ...READ MORE

answered Jun 28, 2018 in Docker by Sophie may
• 9,850 points
398 views
0 votes
1 answer
+1 vote
11 answers

How can I run a ‘docker exec’ command inside a docker container?

Use bash with -c docker exec -it CONTAINER_ID ...READ MORE

answered Dec 10, 2018 in Docker by Kiyara
119,434 views
0 votes
1 answer
0 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 56,900 points
217 views
0 votes
1 answer

Can I run docker in docker?

If you want to use Docker in Docker, that ...READ MORE

answered Jul 5 in Docker by Sirajul
• 36,220 points
128 views