Dockerfile is a text document that contains all commands a user could call on the CLI to assemble an image. Using docker build command user can create an automated build that executes several command-line instructions in succession.
Dockerfile instructions:
FROM:
FROM instruction initializes a new build stage and sets the base image for subsequent instructions.As such a valid Dockerfile must start with a FROM instruction.
Dockerfile instructions:
FROM:
FROM instruction initializes a new build stage and sets the base image for subsequent instructions.As such a valid Dockerfile must start with a FROM instruction.
- ARG is the only instruction that may precede FROM in the Dockerfile.
- FROM can appear multiple times within a single Dockerfile to create multiple images or one build stage as a dependency for another.
ARG CODE_VERSION=1.0-SNAPSHOT
FROM helloworld:${CODE_VERSION}
RUN:
RUN instruction will execute any command in a new layer on top of current image and commit the results. The resulting committed image will be used for the next step in Dockerfile.
RUN yum install httpd -y
By default RUN will use sh shell. If you want to change the shell.
RUN ["/bin/bash","-c","echo HelloWorld"]
CMD:
CMD sets default commands for the build stage. it doesn't run the command during build time. If arguments are supplied at docker run, the CMD configuration here will get overridden.
There can be n number of CMD directives in a Dockerfile but only the last one will take effect.
CMD echo "Hello World" | wc -
CMD ["wc","--help"]
RUN vs CMD:
RUN actually runs a command and commits the result CMD doesn't execute anything in build phase, but specifies the intended command for the image (container run time only CMD will come to picture).
MAINTAINER:
MAINTAINER instruction helps us to provide author details in Dockerfile. In current docker releases this instruction is deprecated.
MAINTAINER stardomsolutions
LABEL:
LABEL instructions adds metadata to an image. as per latest docker documentation we need to use LABEL instead of MAINTAINER to define metadata for an docker image.
A LABEL is a key-value pair. To include spaces with in a LABEL we can use quotes.
LABEL AUTHOR="stardom solutions"
LABEL APPNAME="helloworld application"
LAB exercise-1:
In this exercise we will write a Dockerfile with all instructions we have discussed till now.
Create a Dockerfile with below instructions.
FROM centos
MAINTAINER stardomsolutions
RUN yum install httpd -y
CMD echo "Hello World"
CMD echo "Welcome World"
MAINTAINER stardomsolutions
RUN yum install httpd -y
CMD echo "Hello World"
CMD echo "Welcome World"
create docker image by running below command.
docker build -t example1 .
Once the build is success run docker run example1
It will print Welcome World as output and container will exit. As discussed earlier even we have 2 CMD instructions only last CMD instruction is executed.
No comments:
Post a Comment