Dockerfile Detailed instructions please visit: https://www.cnblogs.com/cloudfloating/p/11737447.html
Use Alpine Linux as the base image
Alpine is a very lightweight Linux mirror, he was only about the size of 5MB, it builds on the mirror, you can greatly reduce the volume mirroring.
Alpine's Docker Hub page: https://hub.docker.com/_/alpine
docker pull alpine
Alpine use the apk
command to install the package, support package list can be viewed at the official website: https://pkgs.alpinelinux.org/packages
Here to install Nginx, for example, learning to build image. In addition Nginx itself has an official mirror, pull can.
Construction of Nginx Mirror
Write Dockerfile
FROM alpine
RUN apk update \
# 安装 nginx
apk add --no-cache nginx \
mkdir /run/nginx && \
# 清除缓存
rm -rf /tmp/* /var/cache/apk/*
# 添加容器启动命令,启动 nginx,以前台方式运行
CMD [ "nginx", "-g", "daemon off;" ]
There is a pit, you must create /run/nginx
a directory, or will be error.
Construction of Mirror
Use docker build
the command building:
docker build -t nginx-alpine .
Implementation of the above command in Dockerfile directory can be constructed image. -t
Parameter specifies the name of the mirror for the nginx-alpine
last .
shows the construction of the context ( .
the current directory).
Using the COPY
instruction to copy files, instruction source path is constructed with respect to the context (if context is designated /home
, then the front of the source corresponding to all of the paths are added /home/
).
If your Dockerfile file name is not "Dockerfile", you can use -f
parameters to specify.
Never Dockerfile placed in the root directory of the build, if you will Dockerfile in a large number of video storage directory, and build context for the current directory, then the image will be very large (video are packed into it) . Best practice is to Dockerfile and need to use the files in a single directory.
Run container
Mirror operation using containers constructed:
docker run --name my-nginx -p 80:80 -d nginx-apline
--name
Specifies the name of the container can be omitted (subsequent containers can be operated by id);-p
Port mapping, the host port -> container port;-d
Background process.
After running the visit http://localhost/
, there will be a 404 page nginx, explained that it had run successfully, because there is no default installed Nginx and page /etc/nginx/conf.d/default.conf
content:
# This is a default site configuration which will simply return 404, preventing
# chance access to any other virtualhost.
server {
listen 80 default_server;
listen [::]:80 default_server;
# Everything is a 404
location / {
return 404;
}
}
Nginx built using a mirror to run a static page
Nginx configuration file is created in an empty directory:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www;
location / {
index index.html;
}
}
Write a static page:
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Hello, Docker!</h1>
</body>
</html>
Construction of a new image using a mirror built before:
FROM nginx-alpine
# 拷贝配置文件,覆盖默认的
COPY default.conf /etc/nginx/conf.d/
# 拷贝静态页面
COPY index.html /var/www
Construction of the mirror, the container run:
docker build -t site .
docker run --name my-site -p 80:80 -d site
Now visit http://localhost/
, you can see Hello, Docker!