A Dockerfile
is the text file that builds Docker images. This text file contains particular instructions about the container. To build a Dockerfile, follow the steps below.
Step 1: Create a directory where the user can build the image.
mkdir ~/my_directory
cd ~/my_directory
touch Dockerfile
mkdir
creates the directory andcd
changes the current directory tomy_directory
. Thetouch
command in Docker creates the Dockerfile.
Step 2: Add comments in case they are needed. Comments can be added using the #
sign.
# This is my comment
Step 3: The very first line has to begin at the keyword FROM
as it lets Docker know which base image the current image should be based on. In this case, the image is created from the Ubuntu image.
FROM ubuntu
Step 4: Add the command LABEL
to alert Docker to the person who is going to maintain the image. It adds metadata to an image.
# LABEL <key>=<value> <key>=<value> <key>=<value>
LABEL maintainer = "abc@gmail.com"="Abc person"
Step 5: Finally, use the RUN
and CMD/ENTRYPOINT
commands. The RUN
command runs the specific instructions against the image while the CMD
launches the software required in a container.
RUN apt-get update
RUN apt-get install –y nginx
CMD ["echo","Image is successfully created"]
In this case, the Ubuntu system is updated and the nginx server is installed onto our Ubuntu image.
Free Resources