Using Dockerfile to build an nginx image

Source: Internet
Author: User
Tags curl docker ps docker run
Using Dockerfile to build an nginx image

How Docker constructs images: Commit, Dockerfile

1. Use commit to build the image:

Commit is a mirror built on the basis of the original image, using this method to build the image: Save some configuration information and the modified information in the image. Corresponds to a snapshot of a mirror.

2. Use Dockerfile to build the image:

Dockerfile is a quick build of the desired (custom) image.

Dockerfile's Instructions:

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

Run: Used to execute command-line commands. Its basic format:

Shell format: Run < command >, enter commands in the bash environment, a dockerfile allows the use of run no more than 127 layers, so use a run, use ' \ ' to wrap, use ' && ' to execute the next command. This format is generally used;

EXEC format: RUN < "executable", "Parameter 1", "Parameter 2", this method is like the format in a function call;

Copy: Copies the file. Its basic format:

Format 1:copy < source path >...< destination path >

Format 2:copy ["< Source path 1>",..... "< target path >"]

Add: More advanced copy of the file, on the basis of a copy of the addition of some features, if the copy is a compressed package, will be extracted directly, and do not need to use run decompression;

CMD: Container Start command. Its basic format:

Shell format: CMD < command >

EXEC format: CMD ["Executable file", "Parameter 1", "Parameter 2" ...]

Parameter list format: cmd ["Parameter 1", "Parameter 2" ...], specify the specific parameters with cmd after specifying the entrypoint instruction

EntryPoint: Entry point. The basic format is composed of exec and Shell,

The purpose of entrypoint is the same as the CMD, which starts the program and parameters in the specified container. EntryPoint can be replaced in operation, but more cumbersome than CMD, it needs to be specified by the parameter--entrypoint of the Docker run. When EntryPoint is specified, the meaning of CMD is changed, not by running its command directly, but by passing the contents of CMD as a parameter to the entrypoint instruction. When it executes, it becomes: <ENTRYPOINT> "<CMD>"

ENV: Set environment variables. (You can use the variables used here) in its basic format:

Format 1:env <key> <value>

Format 2:env <key1>=<value1> <key2>=<value>, ....

ARG: Build Parameters. The build parameters are the same as Env's, and all are set environment variables, but the environment variables that ARG constructs do not exist in the future when the container is running. Its basic format:

Format 1:arg < parameter name > [=< default value;]

Format 2: This default value can be overridden in the build command Docker build with--build-arg < parameter name >=< value >

VOLUME: Defines an anonymous volume. Its basic format:

Format 1:volume ["< path 1>", "< path 2>" ...]

Format 2:volume < paths >

EXPOSE: Exposed port. The expose directive is a port that is provided by the runtime container, and is not opened because of this declaration when the container is started. Its basic format:

Format 1:expose < port 1> [< port 2> ...]

Workdir: Specifies the working directory. Its basic format:

Format 1:workdir < working directory path >

User: Specifies the current user. User is to help you switch to the specified user. Its basic format:

Format 1:user < user name >

Healtcheck: Health Check to determine if the status of the container is normal. Its basic format:

Format 1:healtcheck [options] CMD < command >: Set the command to check the health of the container

Format 2:healtcheck NONE: Use this format to mask Health Check instructions if the underlying image has a health check instruction

To build an nginx image:

Create a directory in which to write Dockerfile:

[Root@docker ~]# mkdir mynginx[root@docker ~]# cd mynginx/[root@docker mynginx]# pwd/root/mynginx[root@docker mynginx]#

Download Nginx source package to the created directory (under the Mynginx directory):

[Root@docker ~]# wget-p/root/mynginx/  http://nginx.org/download/nginx-1.15.2.tar.gz

Write Dockerfile:

[Root@docker mynginx]# VI Dockerfile

The 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/u Sr/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

To 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/7: From CentOS---> 5182e96772bfstep 2/7: RUN ping-c 1  www.baidu.com---;  Using cache---;  2f70f8abaf2astep 3 /7:run yum-y Install gcc make pcre-devel zlib-devel tar  zlib---;  Using cache---;  dbdda4b7ae6f Step 4/7: ADD nginx-1.15.2.tar.gz/usr/src/---;  Using cache---;  18ace6285668step 5/7: RUN cd/usr/sr c/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---;  Usi NG cache---;  99629488ede9step 6/7: RUN rm-rf/usr/src/nginx-1.15.2---;  Using cache---;  869 Fbad71879step 7/7: EXPOSE---;  Using cache---;  384bed72ea6fsuccessfully built 384BED72EA6FSUCCESSF ully tagged nginx:v3              

Output of two successfully is the build success!

To start a custom image:

To view a built image using Docker images:

To start a custom image:

[Root@docker ~]# Docker run-d-P 80:80--name Nginx: V34ac935e955b1c3ac49eed68f3372f3e96a8934fd8ccf4614afa3d7c29eb96c08[root@docker ~]# Docker PS-ACONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               names4ac935e955b1        nginx:v3            "/bin/bash"         3 seconds ago       Exited (0) 2 seconds ago                       nginx 

Note: At this point, regardless of how you start the container, it is still in the exited state.

After a variety of solutions, finally, finally know where the problem. Originally when the container started, it was in the background corresponding to a thread started, it started at the start, but it executes the command, it exits, and does not run in the background, so use the-dit parameter to let it run in the background.

[Root@docker ~]# Docker run-dit-p 80:80--name 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 is a problem, it is up, but Nginx Web page interface can not access, display refused to connect!!!!

[Root@docker ~]# Curl 192.168.100.22Curl: (7) Failed connect to 192.168.100.22:80; deny connection [Root@docker ~]# elinks- -dump 192.168.100.22elinks: Deny connection  

Then, after asking Baidu, Fq look at Google, finally found the problem lies. As long as the use of exec into the container to start the nginx can be.

[Root@docker ~]# Docker exec-it nginx bash[root@ecaafe119044/]# nginx[root@ecaafe119044/]# exitexit
      
[Root@docker ~]# Curl 192.168.100.22<! DOCTYPE html>    body {        width:35em;        margin:0 Auto;        Font-family:tahoma, Verdana, Arial, sans-serif;    } </style>page, the Nginx Web server is successfully installed andworking. Further configuration is required.</p><p> for online documentation and support pleaserefer To<a href= " http://nginx.org/">nginx.org</a>.<br/>commercial support is available at<a href="/http/ nginx.com/">nginx.com</a>.</p><p><em>thank you for using nginx.</em></p> </body>     

Such Nginx image is built successfully!!!!

New Road!!! Please support!!!!!! a lot

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.