Building a Docker Image
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.
- Log in to the Docker client as the root user.
- Run the following commands to create an empty file named Dockerfile:
- Edit 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.
- 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.
- Run the following command to list images. From the printout, you can find the newly created nginx image with a tag of v3.