Skip to content
Snippets Groups Projects
ONBUILD 4.72 KiB
● 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).

# ONBUILD
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
                   
# Build a Docker Image
docker build .

# OUTPUT
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

● It has created the Docker Image ID "65ce683d76a1" for our prerequisite image.
● Tag the newly created "prerequisite" image.

# COMMANDS
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

docker tag 65ce683d76a1 prerequisite

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

● Use the newly created image "prerequisite" as a base image in a new dockerfile.

# Dockerfile
FROM prerequisite:latest

# Build a Docker Image
docker build .

# OUTPUT
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

● 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".

# COMMANDS
docker images
REPOSITORY          TAG        IMAGE_ID        CREATED          SIZE
<none>              <none>     dc5012637937    a minute ago     200 MB 
prerequisite        latest     65ce683d76a1    2 minutes ago    200 MB
docker.io/centos    latest     49f7960eb7e4    3 weeks ago      200 MB

docker tag dc5012637937 deployall

docker images
REPOSITORY          TAG        IMAGE_ID        CREATED          SIZE
deployall           latest     dc5012637937    a minute ago     200 MB 
prerequisite        latest     65ce683d76a1    2 minutes ago    200 MB
docker.io/centos    latest     49f7960eb7e4    3 weeks ago      200 MB

● Run the container using the "deployall" image and check the logs to ensure the script has run or not.
docker run -d -it --name container1 deployall

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

● Above output confirms that our scripts has successfully executed which was specified along with ONBUILD instruction in the previous image "prerequisite".
● 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".

# Links:- https://www.learnitguide.net/2018/06/docker-onbuild-command-with-examples.html