How to create an image using a Dockerfile

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.

widget

Steps

Step 1: Create a directory where the user can build the image.

mkdir ~/my_directory
cd ~/my_directory
touch Dockerfile

mkdir creates the directory and cd changes the current directory to my_directory. The touch 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

Copyright ©2024 Educative, Inc. All rights reserved