Help Center > > User Guide> Docker Basics> Building a Docker Image

Building a Docker Image

Updated at: Jun 21, 2019 GMT+08:00

This section walks you through the steps of using a Dockerfile to build a Docker image for a simple web application. Dockerfile is a text file that contains all the instructions a user could call on the command line to build an image. A Docker image is a stack consisting of multiple layers. Each instruction creates a layer.


When using a browser to access a containerized application built from an Nginx image, you will see the default Nginx welcome page. In this section, you build a new image based on the Nginx image to change the welcome message into Hello, SWR!


Docker has been installed on the Docker client that will build the new image. For details about how to install Docker, see Installing Docker.


  1. Log in to the Docker client as the root user.
  2. Run the following commands to create an empty file named Dockerfile:

    mkdir mynginx

    cd mynginx

    touch Dockerfile

  3. Edit Dockerfile.

    vim Dockerfile

    Instructions to be added to the Dockerfile:

    FROM nginx
    RUN echo '<h1>Hello,SWR!</h1>' > /usr/share/nginx/html/index.html

    In the preceding instructions:

    • FROM: creates a layer from the base image. A valid Dockerfile must start with a FROM instruction. In this example, the Nginx image is used as the base image.
    • RUN: executes commands to create a new layer. One of its syntax forms is RUN <command>. In this example, the echo command is executed to display Hello, SWR!

    Save the changes and exit.

  4. Run docker build [option] < context path > to build an image.

    docker build -t nginx:v3 .

    • -t nginx:v3: specifies the image name and tag.
    • .: indicates the path where the Dockerfile is located. All contents in this path are packed and sent to the Docker engine to build an image.

  5. Run the following command to list images. From the printout, you can find the newly created nginx image with a tag of v3.

    docker images

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?

Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel