Overview
Cast: Nginx
container, one who would say hello world
the html
file.
Script: A Dockerfile
file named , which records the entire process of modification, installation, construction, and operation. We will use this script to build and customize the image.
Scene: docker
environment.
list of actors
The number one actor index.html
came out and said something to everyoneHello World
<h1>Hello World</h1>
The second actor nginx:alpine
, he is a ready-made docker image. We are familiar with nginx:latest
his little brother. He is better than his brother in three places:
The latest version of nginx mirror is used, and the function is
nginx:latest
exactly the same.alpine
The mirror image uses theAlpine Linux
kernel, which is much smaller than theubuntu
kernel.nginx:alpine
Supported by defaulthttp2
.
script
The main reason for the success of a movie is to have a good story. Let's take a look at the script.
Creating a Dockerfile file is generally divided into four parts: basic image information, maintainer information, image operation instructions and instructions to be executed when the container is started. The content is as follows
# 基础镜像信息
FROM nginx:alpine
# 维护者信息
MAINTAINER docker_user [email protected]
# 镜像操作指令
COPY . /usr/share/nginx/html
RUN echo hello
FROM
nginx:alpine
Create a layer from the Docker image.MAINTAINER
Mark the information of the mirror maintainer.Mirror operation instructions:
COPY
Copy the files in the current directory to/usr/share/nginx/html
, as to why this directory is because the official website mirror uses this directory as the static website deployment directory.Format
RUN
orRUN ["executable", "param1", "param2"]
; the former will run the command in the shell terminal, that is/bin/sh -c
, the latter will use theexec
execution, which is recommended.
Executing instructions when the container starts:
CMD
Command, the usage is theRUN
same, it is used to specify the operation command when running the container, because we are based on the nginx image to make our image, which is equivalent to adding a shell, where the nginx image has been written by ourselves Startup method, so we don't have to write it again.
Filming
After the script is written, execute the command: in the folder path where the Dockerfile is located docker build --tag name:tag .
, and you can build a mirror as described. name is the name of the mirror, tag is the version or tag number of the mirror, if not written, it means last. Note that there is a space and dot after it.
# 用法
docker build [选项] 路径
Mirroring
$ docker build -t webserver-image:v1 .
Sending build context to Docker daemon 3.072kB
Step 1/4 : FROM nginx:alpine
---> bfba26ca350c
Step 2/4 : MAINTAINER docker_user [email protected]
---> Using cache
---> a8b84ca6d5a1
Step 3/4 : COPY . /usr/share/nginx/html
---> b25123c06e56
Step 4/4 : RUN echo hello
---> Running in 7c86a7bca405
hello
Removing intermediate container 7c86a7bca405
---> 55bcba5b3ab9
Successfully built 55bcba5b3ab9
Successfully tagged webserver-image:v1
Use docker images
command to view mirror
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
webserver-image v1 55bcba5b3ab9 44 seconds ago 20.5MB
nginx alpine bfba26ca350c 2 weeks ago 20.5MB
Specify the webserver-image:v1
mirror, -d
run the container in the background, and map the mirrored port 80 to the local port 80.
$ docker run -d -p 80:80 webserver-image:v1
fe5dd3369f7766cf078c4049cad1c99fc444cac56752d2a1393b64fd46e43c4d
Check the running effect
$ curl 0.0.0.0:80
<h1>Hello World</h1>
other
.gitignore
Anyone who has learned git knows that files can be ignored for tracking, and docker also has similar files named .dockerignore
, for example, write the following ignored files and folder contents in this file
.git/
logs