Docker ONBUILD Command Explained with Examples

This tutorial post will help you to understand about Docker ONBUILD Command instruction with examples. This is also one of the instruction available in dockerfile. To know more about other dockerfile instructions, refer this link Dockerfile Instructions Explained with Examples.

Docker ONBUILD Command Explained with Examples

Syntax:

ONBUILD command arguments

Examples:

ONBUILD ADD . /usr/src/app
ONBUILD RUN yum install httpd

What is Docker ONBUILD command?

Docker ONBUILD Command Explained with Examples

Docker ONBUILD command instruction is used to specify the commands that runs when the new docker image is used as a base image for another image (child image).

We can use this ONBUILD instruction where you need a static base image with a dynamic config value that changes in a new image or we can use in a situation where a new image is depend on previous image.

Confused?
Assume that we create a dockerfile with various instruction and specifying ONBUILD instructions at the end of the dockerfile. When we build a image with current dockerfile using “docker build .” command, this will create a new docker image but ONBUILD instruction will not applied into the current docker image.

This will be applied only When we use the newly created image as a base image in another dockerfile.

Lets take an example that we want to create a new image which has all prerequisite ready for running multiple applications or scripts. Finally we will use the “prerequisite” image as a base image to run our applications or scripts.

For this example, We have a created a sample dockerfile which creates a directory and place some script file as a “prerequisite” image. Also we have used ONBUILD instructions to run the scripts, but this ONBUILD instructions will be executed only into the new image only when we use “prerequisite” as a base image.

You can use any command instructions along with ONBUILD, but we have used some scripts to show the examples.

[[email protected] ~]# cat dockerfile
# Starts from here
FROM docker.io/centos
MAINTAINER Devops Engineer
RUN mkdir -p /data/myscript
WORKDIR /data/myscript
RUN echo “ping google.com -c 3” > testping.sh
&& echo “uptime” > uptime.sh
&& echo “date” > date.sh

# Till this line, instructions will be applied to current image.
#
# This ONBUILD instructions will be applied to new image when we use the current image as a base image
ONBUILD ENTRYPOINT sh /data/myscript/testping.sh
&& sh /data/myscript/uptime.sh
&& sh /data/myscript/date.sh

[[email protected] ~]#

Build a “prerequisite” docker image.

[[email protected] ~]# docker build .
Sending build context to Docker daemon 134.6 MB
Step 1/6 : FROM docker.io/centos
—> 49f7960eb7e4
Step 2/6 : MAINTAINER Devops Engineer
—> Running in 9af56ccc058a
—> e2aac5c7bb6b
Removing intermediate container 9af56ccc058a
Step 3/6 : RUN mkdir -p /data/myscript
—> Running in b5aefa2952b8
—> 082fbf60ddb8
Removing intermediate container b5aefa2952b8
Step 4/6 : WORKDIR /data/myscript
—> 760a30286f2f
Removing intermediate container 2ae6a33beaca
Step 5/6 : RUN echo “ping google.com -c 3” > testping.sh     && echo “uptime” > uptime.sh     && echo “date” > date.sh
—> Running in dd38c5436a77
—> 77d02215183f
Removing intermediate container dd38c5436a77
Step 6/6 : ONBUILD entrypoint sh /data/myscript/testping.sh                    && sh /data/myscript/uptime.sh                    && sh /data/myscript/date.sh
—> Running in abfe4555943e
—> 65ce683d76a1
Removing intermediate container abfe4555943e
Successfully built 65ce683d76a1
[[email protected] ~]#

Above output shows that it has created the Docker Image ID “65ce683d76a1” for our prerequisite image.

Tag the newly created “prerequisite” image.

[[email protected] ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
<none>                      <none>              65ce683d76a1        2 minutes ago       200 MB
docker.io/centos            latest              49f7960eb7e4        3 weeks ago         200 MB
[[email protected] ~]# docker tag 65ce683d76a1 prerequisite
[[email protected] ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
prerequisite                latest              65ce683d76a1        2 minutes ago       200 MB
docker.io/centos            latest              49f7960eb7e4        3 weeks ago         200 MB
[[email protected] ~]#

Use the newly created image “prerequisite” as a base image in a new dockerfile.

[[email protected] ~]# cat dockerfile
FROM prerequisite:latest
[[email protected] ~]#

Build our second image.

[[email protected] ~]# docker build .
Sending build context to Docker daemon 134.6 MB
Step 1/1 : FROM prerequisite:latest
# Executing 1 build trigger…
Step 1/1 : ENTRYPOINT sh /data/myscript/testping.sh                    && sh /data/myscript/uptime.sh                    && sh /data/myscript/date.sh
—> Running in 08b2406e1755
—> dc5012637937

Removing intermediate container 08b2406e1755
Successfully built dc5012637937
[[email protected] ~]#

Above output shows that it has built the image ID “dc5012637937” executed 1 build trigger from base image “prerequisite:latest”.

Let’s tag the newly created image as “deployall”.

[[email protected] ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
<none>                      <none>              dc5012637937        About a minute ago   200 MB
prerequisite                latest              65ce683d76a1        2 minutes ago       200 MB
docker.io/centos            latest              49f7960eb7e4        3 weeks ago         200 MB
[[email protected] ~]# docker tag dc5012637937 deployall
[[email protected] ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
deployall                   latest              dc5012637937        About a minute ago   200 MB
prerequisite                latest              65ce683d76a1        2 minutes ago       200 MB
docker.io/centos            latest              49f7960eb7e4        3 weeks ago         200 MB
[[email protected] ~]#

Run the container using the “deployall” image and check the logs to ensure the script has run or not.

[[email protected] ~]# docker run -d -it –name container1 deployall
2ef67eff8a704d09637ab238d569d245e54ccf17fc3c415f47fec5cf789b5605
[[email protected] ~]# docker logs container1
[[email protected] ~]# docker logs container1
PING google.com (172.217.163.110) 56(84) bytes of data.
64 bytes from maa05s03-in-f14.1e100.net (172.217.163.110): icmp_seq=1 ttl=127 time=33.8 ms
64 bytes from maa05s03-in-f14.1e100.net (172.217.163.110): icmp_seq=2 ttl=127 time=31.5 ms
64 bytes from maa05s03-in-f14.1e100.net (172.217.163.110): icmp_seq=3 ttl=127 time=29.6 ms
— google.com ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 29.614/31.669/33.829/1.734 ms

 11:29:05 up  1:37,  0 users,  load average: 0.07, 0.09, 0.09
Fri Jun 29 11:29:05 UTC 2018
[[email protected] ~]#

Above output confirms that our scripts has successfully executed which was specified along with ONBUILD instruction in the previous image “prerequisite”.

As explained before, when we have used “prerequisite” as a base image in a new dockerfile to build another image “deployall”. ONBUILD instruction is executed the trigger into the new image “deployall”. Similarly, we can you any docker instructions.

So the conclusion is,
Docker ONBUILD command instruction is used to specify the commands that runs when the new docker image is used as a base image for another image (child image).

Hope you have got an idea about Docker ONBUILD command instruction. Going forward, we will play more with docker tool.

Keep practicing and have fun. Leave your comments if any.

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.