How to use dockerfile to build nginx image

12-22-2023

Introduction to dockerfile

Docker can automatically build image by reading the contents of dockerfile, which is a text file containing all commands that need to be executed during the build process. It can also be understood that docker file is a script interpreted by docker program, which consists of instructions one by one, and each instruction corresponds to a command under linux system, and Docker program translates these dockerfile instructions into real linux commands. Dockerfile has its own writing format and supported commands, and docker program solves the dependencies between these commands, similar to makefile.

Docker program will read dockerfile and generate customized image according to the instruction. Compared with the black box like image, dockerfile, an obvious script, is more acceptable to users, which clearly shows how image is generated. With dockerfile, when we need to customize our extra requirements, we only need to add or modify instructions on dockerfile and regenerate the image, which saves the trouble of typing commands.

Docker's methods of building images: commit and dockerfile.

1. Use commit to build a mirror:

Commit is a mirror image based on the original mirror image. The purpose of using this method to build a mirror image is to save some configuration information and modified information in the mirror image. Equivalent to a mirror snapshot.

2. use dockerfile to build the image:

Dockerfile is a required (custom) image for rapid construction.

Dockerfile's instructions:

From: Specifies the base image (from is a required instruction and must be the first instruction).

Run: used to execute command line commands. Its basic format:

Shell format: run, just enter the command in bash environment, and a dockerfile is allowed to use run no more than 127 layers. Therefore, use run once, use'' to wrap lines, and use'&&'to execute the next command. This format is generally used;

Exec format: run, which is like the format in function call;

Copy: copy the file. Its basic format:

Format 1: copy ...

Format 2: copy

Start a custom mirror:

[root@docker ~]# docker run -dit -p 80:80 --name nginx nginx:v3ecaafe1190447878b98dfb0198e92439db60ff7dab57a1674e0e9e7282a9c858[root@docker ~]# docker ps -acontainer id image command created status ports namesecaafe119044 nginx:v3 "/bin/bash" 3 seconds ago up 2 seconds 0.0.0.0:80->80/tcp nginx

Note: at this time, no matter how you start this container, it is still in the existing state.

After all kinds of solutions, finally, I finally know where the problem lies. Originally, when the container was started, it was started in the background corresponding to a thread. It was already started when it started, but after it executed the command, it quit and did not run in the background, so use the -dit parameter to make it run in the background.

[root@docker ~]# docker run -dit -p 80:80 --name nginx nginx:v3ecaafe1190447878b98dfb0198e92439db60ff7dab57a1674e0e9e7282a9c858[root@docker ~]# docker ps -acontainer id image command created status ports namesecaafe119044 nginx:v3 "/bin/bash" 3 seconds ago up 2 seconds 0.0.0.0:80->80/tcp nginx

however .......

At this time, there was another problem. Although it got up, nginx's web interface could not be accessed, and the connection was refused! ! ! !

[root@docker ~]# curl 192.168.100.22curl: (7) failed connect to 192.168.100.22:80; Connection denied [root @ docker ~] # elinks-dump 192.168.100.22 elinks: Connection denied.

Then, after asking Baidu, fq looked at Google and finally found the problem. It turns out that just use exec to enter the container and start nginx.

[root@docker ~]# docker exec -it nginx bash[root@ecaafe119044 /]# nginx[root@ecaafe119044 /]# exitexit

[root@docker ~]# curl 192.168.100.22welcome to nginx! body { width: 35em; margin: 0 auto; font-family: tahoma, verdana, arial, sans-serif; }welcome to nginx! if you see this page, the nginx web server is successfully installed andworking. further configuration is required.

for online documentation and support please refer tonginx.org.commercial support is available atnginx.com .

thank you for using nginx.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us