In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to use dockerfile to build nginx images. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it.
Dockerfile introduction
Docker can automatically read the contents of the dockerfile build image,dockerfile is a text file that contains all the commands that need to be executed during the build process. It can also be understood that dockfile is a script interpreted by the docker program, which consists of one instruction, each instruction corresponding to a command under the linux system, and the docker program translates these dockerfile instructions into real linux commands. Dockerfile has its own writing format and supported commands, and the docker program solves the dependencies between these commands, similar to makefile.
The docker program will read the dockerfile and generate a custom image according to the instructions. An obvious script like dockerfile is more acceptable to users than a black box like image, which clearly shows how image is created. With dockerfile, when we need to customize our own additional requirements, we just need to add or modify instructions on dockerfile to regenerate image, saving us the trouble of typing commands.
Docker's method of building an image: commit, dockerfile
1. Use commit to build an image:
Commit is an image based on the original image. The purpose of using this method to build an image is to save some configuration information and modified information in the image. Equivalent to a mirrored snapshot.
2. Use dockerfile to build an image:
Dockerfile is the required (custom) image for a quick build.
Dockerfile's instructions:
From: specify 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, enter the command in the bash environment, a dockerfile allows you to use run no more than 127th layers, so use run once, use'\ 'line feeds, and use' & &'to execute the next command. This format is generally used
Exec format: run, which is like the format in a function call
Copy: copy the file. Its basic format:
Format 1:copy...
Format 2:copy [",."]
Add: a more advanced way to copy files. Some functions have been added to copy. If you copy a compressed package, it will be decompressed directly, without the need for run decompression.
Cmd: container startup command. Its basic format:
Shell format: cmd
Exec format: cmd ["executable", "parameter 1", "parameter 2".]
Parameter list format: cmd ["Parameter 1", "Parameter 2"...]. After specifying the entrypoint instruction, specify the specific parameters with cmd.
Entrypoint: entry point. Its basic format is divided into exec and shell.
The purpose of entrypoint, like cmd, is to start the program and parameters in the specified container. Entrypoint can be replaced in operation, but it is more tedious than cmd and needs to be specified by the parameter entrypoint of docker run. When entrypoint is specified, the meaning of cmd changes. Instead of running its command directly, it passes the contents of the cmd as an argument to the entrypoint instruction. When it is executed, it becomes: "
Env: sets the environment variable. (you can use the variables used here) its basic format:
Format 1:env
Format 2:env = =...
Arg: build parameters. The effect of build parameters is the same as that of env, which sets environment variables, except that the environment variables built by arg do not exist in the future container runtime. Its basic format:
Format 1: arg [=]
Format 2: this default value can be overridden with-- build-arg = in the build command docker build
Volume: defines anonymous volumes. Its basic format:
Format 1: volume ["", "...]
Format 2: volume
Expose: expose the port. The expose directive declares the port provided by the runtime container, and the port is not opened because of this declaration when the container is started. Its basic format:
Format 1: expose [...]
Workdir: specify the working directory. Its basic format:
Format 1: workdir
User: specifies the current user. User is to help you switch to the specified user. Its basic format:
Format 1: user
Healtcheck: health check to determine whether the container is in a normal state. Its basic format:
Format 1: healtcheck [options] cmd: set commands to check the health of containers
Format 2: healtcheck none: if the basic image has a health check instruction, use this format to block its health check instruction
Build a nginx image:
Create a directory and write dockerfile in that directory:
[root@docker ~] # mkdir mynginx [root@docker ~] # cd mynginx/ [root@docker mynginx] # pwd/root/mynginx [root@docker mynginx] #
Download the nginx source package to the directory you created (under the mynginx directory):
[root@docker] # wget-p / root/mynginx/ http://nginx.org/download/nginx-1.15.2.tar.gz
Write a dockerfile:
[root@docker mynginx] # vi dockerfile
Its contents are as follows:
From centosrun ping-c 1 www.baidu.comrun yum-y install gcc make pcre-devel zlib-devel tar zlibadd nginx-1.15.2.tar.gz / usr/src/run cd / usr/src/nginx-1.15.2\ & mkdir / usr/local/nginx\ & &. / configure-- prefix=/usr/local/nginx & & make & & make install\ & & ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/\ & & nginxrun Rm-rf / usr/src/nginx-1.15.2expose 80
Run the docker command to build the image:
[root@docker mynginx] # docker build-t nginx:v3. Sending build context to docker daemon 1.029mbstep 1 5182e96772bfstep 7: from centos-- > 5182e96772bfstep 2 www.baidu.com 7: run ping-c 1 www.baidu.com-- > using cache-- > 2f70f8abaf2astep 3 y install gcc make pcre-devel zlib-devel tar zlib- 7: run yum-y install gcc make pcre-devel zlib-devel tar zlib- > using cache-- > dbdda4b7ae6fstep 4 using cache 7: add nginx-1.15.2.tar.gz / usr/src/-- > using cache-- -> 18ace6285668step 5ln 7: run cd / usr/src/nginx-1.15.2 & & mkdir / usr/local/nginx & &. / configure-- prefix=/usr/local/nginx & & make & & make install & & ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ & & nginx- > using cache-- > 99629488ede9step 6 nginx- 7: run rm-rf / usr/src/nginx-1.15.2-- > using cache- -- > 869fbad71879step 7 869fbad71879step 7: expose 80-> using cache-> 384bed72ea6fsuccessfully built 384bed72ea6fsuccessfully tagged nginx:v3
The output of two successfully means that the construction is successful!
Start the custom image:
Use docker images to view the built image:
Start a custom image:
[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 dit 80-> 80/tcp nginx
Note: at this point, no matter how you start the container, it is still in the exited state.
After all kinds of solutions, we finally know where the problem lies. When the original container starts, it starts with a thread in the background. It has already started when it starts, but after it executes the command, it exits and is not running 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 dit 80-> 80/tcp nginx
However.
At this time, there is a problem, although it is up, but the nginx web web page interface can not be accessed, showing that the connection is rejected!
[root@docker ~] # curl 192.168.100.22curl: (7) failed connect to 192.168.100.22 failed connect to 80; refuse to connect [root@docker ~] # elinks-- dump 192.168.100.22elinks: reject a connection
Then, after asking Baidu and fq to see Google, I finally found out where the problem lies. It turns out that all you have to do is to 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: 35eme; 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.
These are all the contents of the article "how to use dockerfile to build nginx images". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.