Implementation of building custom images with Dockerfile

Implementation of building custom images with Dockerfile

Preface

In the previous article, the images used to run containers with docker were pulled remotely from dockerhub, so can we build our own images? The answer is yes. You can build your own image through Dockerfile. Dockerfile itself is not difficult, it is just a bunch of commands. What makes me feel difficult is how to use these commands of Dockerfile to better build your own image from a higher level perspective. Let's take a look.

Introduction to Dockerfile

Dockerfile is actually a file. The name of the file is Dockerfile. Of course, you can also use this name and specify the name when building the image. However, it is commonly known as using Dockerfile. The main function of Dockerfile is to help us build a custom image, so Dockerfile is also called an image build file or an image description file.

Some friends may ask, there are many official images provided on Dockerhub that have basically met all services, so why do we need to customize the image? By using Dockerfile to build your own image, you can package your own application into an image, and running the image can directly run services with some of our customized functions.

For example, when downloading the centos7 image from dockerhub, the official image does not have the vim function. Therefore, we can build our own image based on the official centos image to automatically install vim when running the centos container.

Dockerfile builds the image process

Before introducing the Dockerfile image building process, you need to know an important concept-context directory. The directory where the Dockerfile is located is called the context directory.

Docker is a CS architecture. The command used to build an image using Dockerfile is docker build. When the command is executed in the client's operating system, the docker engine will package all the data in the directory where the Dockerfile is located and send it to the docker server. Therefore, the directory where the Dockerfile is located should only contain the files required to build the current image. When the server runs a line of commands in the Dockerfile, a temporary image will be generated in the docker cache (if you do not want to generate a temporary image, add the --no_cache parameter after docker build). When it runs to the last line, a final image will be obtained. As shown in the following figure:

Dockerfile usage

First, let's talk about the command for Dockerfile to build the image:

# Command format: docker build -t image name: tag path where Dockerfile is located docker build -t mycentos:01 .

Here are some commonly used commands in Dockerfile. Note that all commands in Dockerfile are capitalized.

FROM image name: tag: The first command in the Dockerfile, indicating which image to build the image based on. Although a custom image is built, this custom image is also built on the official base image.

# Dockerfile
FROM centos:7 # indicates that the image is built based on centos7

RUN: The command that needs to be run when building the image, which can be followed by a shell command.

# The first syntax format RUN yum install -y vim

# The second syntax format RUN ["yum", "install", "-y", "vim"]

EXPOSE: Ports exposed to the outside world. Only when the ports are exposed in the image can the -p parameter be specified when executing the docker run command.

EXPOSE 9000 # indicates exposing port 9000

WORKDIR: Specifies the path when entering the container. You can write multiple paths. If there is no specified path in the container, the path will be created. You can write multiple paths. The next path is a relative path based on the previous path.

WORKDIR /data
WORKDIR /a

COPY: Copies the specified file in the context directory to the specified directory in the image.

# Command format: COPY original path (can be absolute path or relative path) target path in container# Syntax format 1 COPY /root/app/aa.txt /data 

# Syntax format 2 COPY ["<src>", ..., "<dest>"] --- Syntax format 2

ADD: Copies the specified file in the context directory to the specified directory of the mirror. It can identify the URL and automatically download the compressed package corresponding to the URL. It can also copy the local compressed package to the specified directory of the mirror and then automatically decompress it.

#Copy the specified file in the context directory to the specified directory of the mirror ADD bb.txt /data

# Automatically identify the URL and download it to the specified directory in the container ADD https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz /data

# The compressed package will be automatically decompressed to the specified directory ADD xxx.tar /data

ENTRYPOINT: Used to specify the command to be executed when the container is started. Similar to CMD, it is often used to set the first command after the container is started. Multiple commands can be written; they can be overwritten.

# ENTRYPOINT shell script or ENTRYPOINT ["tail", "-f"...]
ENTRYPOINT tail -f bb.txt
# Overwrite docker run --entrypoint=cat mycentos:08 /data/bb.txt

CMD: The last CMD is used as the standard; can be overwritten; can pass parameters to ENTRYPOINT and can be used in conjunction with ENTRYPOINT (can only be used in the form of a json array)

# Only run the last CMD
CMD ls $BASEDIR
CMD cat bb.txt

# Can be overwritten, the command after docker run will directly overwrite the command after CMD in Dockerfile docker run mycentos:09 ls /data

# Used in conjunction with ENTRYPOINT to pass parameters to ENTRYPOINT. Parameters can be passed dynamically. For example, the project path remains unchanged but the project name changes. Please note that when the two are used in conjunction, the json array format must be used.
ENTRYPOINT ["ls", "/data"]
CMD [/data/bb"]

or

docker run mycentos:10 /data/bb/aa

This is the end of this article about the implementation of building a custom image with Dockerfile. For more information about building an image with Dockerfile, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Dockerfile file writing and image building command analysis
  • Build a Docker image using Dockerfile
  • Steps to build a Docker image using Dockerfile
  • How to build a tomcat image based on Dockerfile
  • How to use Dockerfile to build images in Docker
  • Example of using Dockerfile to build an nginx image
  • How to use Dockerfile to build images
  • Sample code for building a docker image using the dockerfile instruction

<<:  How to automatically back up the mysql database regularly

>>:  CSS uses Alibaba vector library to quickly add good-looking icon effects to the corresponding positions (example code)

Recommend

Detailed explanation of using pt-heartbeat to monitor MySQL replication delay

pt-heartbeat When the database is replicated betw...

Public free STUN servers

Public free STUN servers When the SIP terminal us...

Analysis of Nginx Rewrite usage scenarios and configuration methods

Nginx Rewrite usage scenarios 1. URL address jump...

Rendering Function & JSX Details

Table of contents 1. Basics 2. Nodes, trees, and ...

How to modify the port mapping of a running Docker container

Preface When docker run creates and runs a contai...

MySQL 8.0.20 installation and configuration detailed tutorial

This article shares with you a detailed tutorial ...

Detailed explanation of JS ES6 coding standards

Table of contents 1. Block scope 1.1. let replace...

HTML code example: detailed explanation of hyperlinks

Hyperlinks are the most frequently used HTML elem...

How to expand the disk space of Linux server

Table of contents Preface step Preface Today I fo...

MySQL server 5.7.20 installation and configuration method graphic tutorial

This article records the installation and configu...

Example analysis of mysql variable usage [system variables, user variables]

This article uses examples to illustrate the usag...