Build a Docker image using Dockerfile

Build a Docker image using Dockerfile

Today we will look at how to create a Dockerfile.

There are two ways to build a Docker image:

1. One is to use the docker commit command

2. Another one is based on docker build command and dockerfile file

Generally speaking, building an image with Dockerfile is more flexible than building an image with the Docker commit command, so the latter is more commonly used.

Build a Docker image using Dockerfile

1. What is Dockerfile?

A dockerfile is a file that is written using DSL syntax, and then the docker build command is used to build a new image based on the instructions in the dockerfile file.

Suppose our code is:

mkdir test

cd test

touch Dockerfile

It is not difficult to see that we created a test directory and entered the directory to create a dockerfile_test file. This directory is called our build environment. Docker calls this environment context or build context. Docker will upload the build context and the files and directories in the context to the Docker daemon when building the image, so that the Docker daemon can directly access any code, files or other data you store in the image.

Let's take a look at the contents of a Docker file:

# version: 0.0.1
FROM ubuntu:14.04
MAINTAINER Yeyz '[email protected]'
RUN apt-get update
RUN apt-get install -y nginx
RUN echo 'Hi, I am your container' > /root/test.html
EXPOSE 80

It is not difficult to see from the above file content that Dockerfile contains a series of commands, each command needs to be marked with an uppercase keyword. Content starting with # will be recognized as a comment.

Docker generally executes the commands in the Dockerfile in the following order:

1. Docker runs a container from a base image. The first command of each DockerFile should be From. From specifies a base image, and subsequent instructions are executed on this basis.

2. Execute an instruction to modify the container

3. Perform operations similar to docker commit to submit a new image layer

4. The Docker image runs a container based on the image just submitted

5. Execute the next instruction in the Dockerfile until all container instructions are executed

In the above process, if any step fails to execute, it means that the target image fails to be created. However, because the target image is the result of stacking one image after another, we actually get an image that can be run, but it has not yet reached the final image. This feature is very important for debugging images.

We explain the above image:

Version: represents a comment, indicating the version

From instruction, From specifies a base image ubuntu, indicating that our operation is performed on the base image ubuntu

Maintainer directive, which represents the author of the image and the author's email address

The RUN instruction will run the specified command in the current image. The three instructions are to update the apt repository, install the nginx package, and print a command to the specified file. Each command creates an image layer. If the command succeeds, the image layer is committed, and then the next instruction in the DockerFile is specified. By default, the RUN command uses /bin/bash -c in the shell to execute the following instructions.

The Expose instruction tells the application in the container to use the specified port of the container. Of course, you can use multiple EXPOSE to expose multiple ports to the outside world.

2. Execute a Dockerfile to build an image

We execute the Dockerfile just now and get the following results:

[root test]# docker build -t='yeyz:test0' .
Sending build context to Docker daemon 2.048 kB
Step 1/6 : FROM ubuntu:14.04
Trying to pull repository docker.io/library/ubuntu ...
 14.04: Pulling from docker.io/library/ubuntu
2e6e20c8e2e6: Downloading [> ] 539.1 kB/70.69 MB
95201152d9ff: Download complete
 5f63a3b65493: Download complete

The -t option is used to specify the image and warehouse name, yeyz is the warehouse name, and test0 is the image name. The . in the command represents the current directory. Docker searches for DockerFile in the local directory by default. It can also be replaced with a specified Git repository source address to specify the location of DockerFile. as follows:

docker build -t='yeyz/test0' xxxx.com:yeyz/test0

If you want to keep version information, you can specify a tag as follows:

docker build -t='yeyz/test0:v1' .

If we only want to upload some files under a certain directory, we can create a .dockerignore file, where each line is a file filter matching pattern. In this way, when generating the image, unnecessary files will be automatically filtered out.

The final return value is an image ID. After we get the image ID, we can use the docker run command to run the image.

3. Dockerfile build cache

When a problem occurs when we first build the Dockerfile, we need to build it again, and the same instructions will be executed this time. Suppose we modify the content of line 4 in the Dockerfile, then Docker will automatically load the first 3 lines by caching, which will save a lot of time.

If we don't want to use the Dockerfile cache, we can avoid using the Dockerfile cache by adding the --no-cache parameter.

4. View the image build history

Docker history can view the build history of the built image and each layer of the built image.

[root@VM-16-13-centos test]# docker images mysql
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql latest 4f1413420360 7 weeks ago 545 MB

[root@VM-16-13-centos test]# docker history 4f1413420360
IMAGE CREATED CREATED BY SIZE COMMENT
4f1413420360 7 weeks ago /bin/sh -c #(nop) CMD ["mysqld"] 0 B
                 <missing> 7 weeks ago /bin/sh -c #(nop) EXPOSE 3306 33060 0 B
                 <missing> 7 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["docker-ent... 0 B
                 <missing> 7 weeks ago /bin/sh -c ln -s usr/local/bin/docker-entr... 34 B
                <missing> 7 weeks ago /bin/sh -c #(nop) COPY file:f9202f6b715c0e... 13.1 kB
             <missing> 7 weeks ago /bin/sh -c #(nop) COPY dir:2e040acc386ebd2... 1.12 kB
             <missing> 7 weeks ago /bin/sh -c #(nop) VOLUME [/var/lib/mysql] 0 B
                 <missing> 7 weeks ago /bin/sh -c { echo mysql-community-server... 410 MB
              <missing> 7 weeks ago /bin/sh -c echo "deb http://repo.mysql.com... 55 B
                <missing> 7 weeks ago /bin/sh -c #(nop) ENV MYSQL_VERSION=8.0.2... 0 B
                 <missing> 7 weeks ago /bin/sh -c #(nop) ENV MYSQL_MAJOR=8.0 0 B
                 <missing> 7 weeks ago /bin/sh -c set -ex; key='A4A9406876FCBD3C... 2.61 kB
             <missing> 7 weeks ago /bin/sh -c apt-get update && apt-get insta... 52.2 MB
             <missing> 7 weeks ago /bin/sh -c mkdir /docker-entrypoint-initdb.d 0 B
                 <missing> 7 weeks ago /bin/sh -c set -eux; savedAptMark="$(apt-... 4.17 MB
             <missing> 7 weeks ago /bin/sh -c #(nop) ENV GOSU_VERSION=1.12 0 B
                 <missing> 7 weeks ago /bin/sh -c apt-get update && apt-get insta... 9.34 MB
             <missing> 7 weeks ago /bin/sh -c groupadd -r mysql && useradd -r... 329 kB
              <missing> 7 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0 B
                 <missing> 7 weeks ago /bin/sh -c #(nop) ADD file:d2abb0e4e7ac177... 69.2 MB

The above is the details of using Dockerfile to build docker images. For more information about using Dockerfile to build docker images, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Dockerfile file writing and image building command analysis
  • Implementation of building custom images with 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

<<:  Use personalized search engines to find the personalized information you need

>>:  How to achieve seamless token refresh

Recommend

Some experience sharing on enabling HTTPS

As the domestic network environment continues to ...

Detailed explanation of the problems and solutions caused by floating elements

1. Problem Multiple floating elements cannot expa...

How to use Docker-compose to build an ELK cluster

All the orchestration files and configuration fil...

In-depth understanding of Vue transition and animation

1. When inserting, updating, or removing DOM elem...

A brief discussion on how Tomcat breaks the parent delegation mechanism

Table of contents JVM Class Loader Tomcat class l...

An example of the difference between the id and name attributes in input

I have been making websites for a long time, but I...

MySQL 8.0.19 installation and configuration method graphic tutorial

This article records the installation and configu...

CSS setting div background image implementation code

Adding background image control to a component re...

JavaScript Regular Expressions Explained

Table of contents 1. Regular expression creation ...

Realize the CSS loading effect after clicking the button

Since there is a button in my company's produ...

Detailed explanation of fs module and Path module methods in Node.js

Overview: The filesystem module is a simple wrapp...

Realization of real-time file synchronization between Linux servers

Usage scenarios For existing servers A and B, if ...

Vue implements simple production of counter

This article example shares the simple implementa...