Dockerfile Guide: Easily Create Your Own Docker Image!

Linux | Red Hat Certified | IT Technology | Operations Engineer

👇 Join the technical exchange QQ group with 1000 members, remark 【Official Account】 for faster approval

Dockerfile Guide: Easily Create Your Own Docker Image!

1. Basic Structure and Common Instructions of Dockerfile

FROM – Specify Base Image

FROM ubuntu:20.04

This instruction sets Ubuntu 20.04 as the base image.

RUN – Execute Command

RUN apt-get update && apt-get install -y nginx

Run commands to update the package manager’s index and install Nginx.

CMD – Provide Default Command When Starting Container

CMD ["nginx", "-g", "daemon off;"]

Set Nginx to run by default when the container starts.

EXPOSE – Declare Ports

EXPOSE 80

Inform Docker that the image intends to expose port 80 at runtime.

ENV – Set Environment Variables

ENV NGINX_VERSION 1.14

Set the environment variable NGINX_VERSION.

COPY and ADD – Copy Files/Directories

COPY . /app

Copy all files from the current directory to the /app directory in the image.

WORKDIR – Set Working Directory

WORKDIR /app

Set the working directory for subsequent RUN, CMD, and ENTRYPOINT commands.

ENTRYPOINT – Set Command to Execute When Starting Container

ENTRYPOINT ["python3", "-m", "http.server"]

Set the container to run a Python3 HTTP server when it starts.

USER – Set Running User

USER myuser

Specify the user under which the subsequent RUN, CMD, and ENTRYPOINT commands will run.

ARG – Define Build Arguments

ARG version

Define a parameter named version that can be provided at build time.

VOLUME – Define Anonymous Volumes

VOLUME /var/log/nginx

Create a point /var/log/nginx in the container that can be used to store data.

2. Build and Run Docker Image

1. Write Dockerfile: Create a Dockerfile containing the above instructions as needed. Assume the file content is as follows:

FROM ubuntu:20.04RUN apt-get update && apt-get install -y nginxCOPY . /var/www/htmlEXPOSE 80CMD ["nginx", "-g", "daemon off;"]

The following is a detailed explanation of the Dockerfile:

FROM ubuntu:20.04: Specifies the base image as Ubuntu 20.04. This means the built Docker image will be based on the Ubuntu 20.04 image.

RUN apt-get update && apt-get install -y nginx: Executes the command to update the package list and install Nginx. Here the && symbol indicates that Nginx will be installed after updating the package list. The -y parameter indicates to automatically answer yes during the installation process.

COPY . /var/www/html: Copies all files from the current directory to the container’s /var/www/html directory. Here the . represents the current directory, and /var/www/html is the default website directory for Nginx.

EXPOSE 80: Declares that the container will listen on port 80. Here, port 80 is the default port for Nginx.

CMD [“nginx”, “-g”, “daemon off;”]: Specifies the command to be executed when the container starts. Here the command starts Nginx and uses the daemon off; parameter to run in the foreground.

This Dockerfile builds a simple Nginx server based on Ubuntu 20.04, installs Nginx, and configures the server to serve the website content you provide. When built and run, this image will start Nginx and enable it to serve static content, listening on port 80 of the container. This configuration is ideal for hosting static websites or serving as a frontend server for web applications.

2. Build the Image: Execute the following command in the directory where the Dockerfile is located:

docker build -t my-nginx-image .

This will build a new image and tag it as my-nginx-image.

Explanation of Command Parameters:

docker: Name of the Docker command-line tool. build: Command to build a Docker image. -t: Specify the tag (tag) for the image. Here the tag is my-nginx-image, indicating that the built Docker image is named my-nginx-image. .: Specify the path where the Dockerfile is located. Here the path is ., indicating that the Dockerfile is in the current directory.

After executing the docker build -t my-nginx-image . command, Docker will look for the Dockerfile in the current directory and build the Docker image according to the instructions in the Dockerfile.Once the build is complete, the image will be stored locally, and you can use the docker images command to view it.

3. Run the Container:

docker run -p 8080:80 my-nginx-image

This will start a container instance using the image and map the local port 8080 to the container’s port 80.

Explanation of Command Parameters:

docker: Name of the Docker command-line tool. run: Command to run a Docker container. -p: Specify port mapping. Here, the port mapping is to map the container's port 80 to the host's port 8080. This means that when accessing the host's port 8080, it will be forwarded to the container's port 80. my-nginx-image: Specify the Docker image to run. Here the image name is my-nginx-image, which is the previously built Docker image.

By following these steps, you can create almost any type of Docker image to meet various application scenarios and needs.

3. Base Image Selection

A base image is the starting point for building a Docker image, and all Docker images are built from a base image. A base image contains a minimal installation of an operating system or a runtime environment for a specific application, from which more layers can be added to create the final image.

Common Base Images

Ubuntu

Description: Ubuntu is an open-source operating system based on Debian, widely used in cloud and server environments. Advantages: Extensive community support, rich package repository, frequent updates. Use Cases: Suitable for applications that require a stable, multifunctional, and easy-to-use Linux environment.

Alpine Linux

Description: Alpine Linux is a security-oriented lightweight Linux distribution that uses musl libc and busybox. Advantages: Very small image size (typically around 5MB), improving security and resource efficiency. Use Cases: Very suitable for creating small, secure containerized applications.

CentOS

Description: CentOS is an open-source operating system based on Red Hat Enterprise Linux, known for its enterprise-level stability and security. Advantages: Long-term support, a good choice for enterprise deployments. Use Cases: Suitable for applications that require enterprise-level support and wide compatibility.

Debian

Description: Debian is an extremely stable operating system that serves as the basis for Ubuntu and many other Linux distributions. Advantages: Strong stability, long support cycle, mature package management system. Use Cases: Suitable for servers and applications that require long-term stable support.

Scratch

Description: In the context of Docker, scratch is a blank image that contains no files or content. Advantages: Allows building images from scratch, providing complete control over image content. Use Cases: Suitable for extremely lightweight dedicated containers or building applications based on C/C++ that do not rely on operating system distributions.

BusyBox

Description: BusyBox combines multiple UNIX utilities into a single small executable file, often used in embedded environments. Advantages: Very compact, suitable for embedded systems and resource-constrained environments. Use Cases: Suitable for simple containers that require minimal Linux systems.

How to Choose a Base Image

When choosing a base image, consider the following factors:

Image Size: Choosing a smaller base image can reduce build time and improve container startup speed. Security: Some base images like Alpine Linux focus on providing minimal security risks. Compatibility: Certain applications may depend on specific system libraries or environments, so choosing a base image compatible with these requirements is crucial. Community and Support: Widely used base images typically have larger communities and better support. Updates and Maintenance: Choose actively maintained and regularly updated base images to ensure security and recency.

By considering these factors, you can select the most suitable base image for your project needs.Using a base image in a Dockerfile is very simple; just specify it at the top of the file using the FROM instruction.

Dockerfile Guide: Easily Create Your Own Docker Image!

For course inquiries, add: HCIE666CCIE

↑ Or scan the QR code above ↑

What technical points and content do you want to see?

You can leave a message below to let us know!

Leave a Comment