Learning materials about Docker - 04 - Some basic operations of docker build




After the Dockerfile is created, you can use the docker build command to build an image based on the Dockerfile. In the previous section, we executed the docker build -t myimage . command in the folder where the Dockerfile is located, and the image was built. Now let's talk about this command in detail. The command format of the docker build is as follows:
quote
# docker build[OPTIONS]context path|URL

Among them,
"docker build: command keywords for building images with Dockerfile
" [OPTIONS]: command options, commonly used instructions include -t to specify the name of the image, -f to display the specified Dockerfile, if -f is not used, the context path will be defaulted to The file named Dockerfile below is considered the "Dockerfile" for building the image. The specific command can be viewed using docker build -help.
"Context Path|URL: Specifies the path of the context in which the image is built. During the process of building the image, any file in the context can be referenced only.
"
Now let's look at docker build -t myimage . This command, in this In the command, use -t to specify the image name myimage. Since the -f command is not used, the file named Dockerfile in the context path is used by default and is considered to be the "Dockerfile" for building the image. Finally, specify the context path. In this command, the context path is.

After executing docker build, all files in the context directory will be packaged first, and then passed to the Docker daemon, so that after the Docker daemon receives the context package, it will expand. Get all the files needed to build the image.

As shown in the figure below, after executing docker build, it will first send build context to Deckor daemon, that is, package all files in the context directory and send them to Docker daemon. Therefore, when building an image when using a Dockerfile file, it is generally placed in an empty folder to prevent other redundant files from being passed out. Then execute the instructions of the Dockerfile in sequence. If the instruction is executed correctly, continue to execute the next one until all the instructions are executed correctly and the image construction is completed; if the instruction is executed incorrectly, the image construction is terminated.

quote
1. [root@localhost newdir]# docker build-t myimage .
2. Sending build context toDocker daemon 2.048 kB
3. Step1/2: FROM ubuntu
4. --->14f60031763d
5. Step2/2: RUN mkdir dir1
6. --->Runningin c5117d908931
7. ---> cb0193727724
8. Removing intermediate container c5117d908931
9. Successfully built cb0193727724


In addition to building from local, docker build also supports building from URL.

$ docker build github.com/creack/docker-firefox


The build command will clone the GitHub repository project and use the cloned repository as the docker repository context, and the build command will look for the Dockerfile at the root of the context. You can specify any Git repository using git:// or git@scheme.
Note, if it is a build URL, you do not need to specify the context path, choose one of the two.
docker build -f ctx/Dockerfile http://server/ctx.tar.gz

The build statement above indicates that the repository context of docker is downloaded from the URL: http://server/ctx.tar.gz. And specify the ctx/Dockerfile in the path of the compressed package as the Dockerfile of this building. The following is the output:
quote
$ docker build -f ctx/Dockerfile http://server/ctx.tar.gz

Downloading context: http://server/ctx.tar.gz [===================>]    240 B/240 B
Step 1/3 : FROM busybox
---> 8c2e06607696
Step 2/3 : ADD ctx/container.cfg /
---> e7829950cee3
Removing intermediate container b35224abf821
Step 3/3 : CMD /bin/ls
---> Running in fbc63d321d73
---> 3286931702ad
Removing intermediate container fbc63d321d73
Successfully built 377c409b35e4


In addition to the above two builds (local and via URL), docker can actually build through STDIN, see Mina's build command:
$ docker build - < Dockerfile
This command indicates that docker uses standard input (STDIN) to Build an image, but this build has no repository context path, so because of the lack of context, nothing related to the local directory is sent to the docker daemon. Since there is no content, the ADD command used by the build dockerfile can only add remote URLs.
$ docker build - < context.tar.gz
The build command above also uses the standard input (STDIN) to build an image, but the standard input source is a compressed package. docker build supports compressed packages in the following formats: bizp2, gzip and xz.

Next, learn some common commands of docker build:

1. -t command
$ docker build -t vieux/apache:2.0 .

This build command adds the -t command, which means that the specified image name is vieux/apache, and the specified version tag is 2.0.
You can provide multiple tags to an image during a build process, for example, the following build, Indicates that an image of whenry/fedora-jboss is built, but version 2.1 and latest version are tagged at the same time
$ docker build -t whenry/fedora-jboss:latest -t whenry/fedora-jboss:v2.1 .


2. -f command
$ docker build -f Dockerfile.debug .

To specify a Dockerfile, use the -f command, which specifies a specific Dockerfile.debug in the current directory as the dockerfile to be used.
$ docker build -f dockerfiles/Dockerfile.debug -t myapp_debug .
$ docker build -f dockerfiles/Dockerfile.prod  -t myapp_prod .

The first command above looks for the Dockerfile.debug file in the current directory dockerfiles as the Dockerfile file, and builds an image called myapp_debug in the current directory.
The second line of the above command looks for the Dockerfile.prod file in the current directory dockerfiles as the Dockerfile file, and builds an image called myapp_prod in the current directory.

3. --add-host command
$ docker build --add-host=docker:10.180.0.1 .

The above build means to write docker:10.180.0.1 into the /etc/hosts file of the built image after the build. You can add multiple -add-hosts.

1. --target command
quote
FROM debian AS build-env
...

FROM alpine AS production-env
...

As we can see above, when a Dockerfile needs to build the contents of multiple stages, if the -target tag is specified as follows, the build will only build the contents of this build-env.
$ docker build -t mybuildimage --target build-env .


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326034608&siteId=291194637