标签:ebe 下载 exec port || off 法规 读写 方式
原文:Docker镜像构建mkdir /opt/demo -p
cd /opt/demo/
FROM centos:latest #指定基础镜像为centos LABEL Author=“wd” #指明作者 RUN yum install -y yum epel-release && yum install -y nginx && echo "${HOSTNAME}-nginx server" > /usr/share/nginx/html/index.html #运行命令安装Nginx CMD [ "/usr/sbin/nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf”] #启动容器运行的命令
[root@app51 demo]# docker build -t nginx:v1 ./ Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos:latest ---> 1e1148e4cc2c Step 2/4 : LABEL Author="wd" ---> Using cache ---> 8eb3ffcb8ba3 Step 3/4 : RUN yum install -y yum epel-release && yum install -y nginx && echo "${HOSTNAME}-nginx server" > /usr/share/nginx/html/index.html ---> Using cache ---> ac91999a716e Step 4/4 : CMD [ "/usr/sbin/nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"] ---> Running in 323afd4ac89d Removing intermediate container 323afd4ac89d ---> 6403c553fd04 Successfully built 6403c553fd04 Successfully tagged nginx:v1
[root@app51 demo]# docker run -d --name nginx-demo-c1 -p 8088:80 nginx:v1 08812b7def62c9ad7879dfa4182bc28a20f524e2dbc5eb6e4fe63d2b67be3cc9 [root@app51 demo]# curl http://127.0.0.1:8088 60e5de135132-nginx server #访问成功 [root@app51 demo]#
FROM <repository> FROM <repository>[:<tag>] FROM <repository>@<digest>
FROM centos:latest
LABEL <key>=<value> <key>=<value> <key>=<value> ...
示例:
LABEL "com.example.vendor"="ACME Incorporated” LABEL maintainer="SvenDowideit@home.org.au"
MAINTAINER <message>
MAINTAINER "wd <xxx@163.com>"
用于主机中的文件或者复制到镜像中
语法:
COPY [--chown=<user>:<group>] <src>... <dest> COPY [--chown=<user>:<group>] ["<src>",... "<dest>"]
COPY hom* /mydir/ #拷贝以hom开头的的所有文件 COPY hom?.txt /mydir/ #?代表占位符,可以拷贝
ADD指令类似于COPY,但是ADD比COPY更强大,支持TAR文件和URL路径
语法:
ADD [--chown=<user>:<group>] <src>... <dest> ADD [--chown=<user>:<group>] ["<src>",... "<dest>"]
ADD hom* /mydir/
ADD hom?.txt /mydir/
WORKDIR dirpath
示例:
WORKDIR /usr/local
用于为镜像定义所需的环境变量,并可被Dockfile中位于其以后的指令所调用,如ADD、COPY、RUN等调用格式为$variable_name或者${variable_name},此外在启动容器时候这些变量也是存在的。
语法:
ENV <key> <value>
ENV <key>=<value> ...
示例:
ENV myName="John Doe” myDog=Rex myCat=fluffy ENV myCat fluffy
用于在build过程中运行的程序,可以是任何指令,可以指定多个RUN
语法:
RUN <command> #shell 格式默认linux采用/bin/sh -c,windows采用cmd /S /C RUN ["executable", "param1", "param2”] #可执行程序格式
示例:
RUN yum install -y nginx RUN ["/bin/bash", "-c", "echo hello"]
用于为容器暴露端口到外部,用于实现通讯,类似于docker run的-p选项
语法:
EXPOSE <port> [<port>/<protocol>...]
EXPOSE 8080
EXPOSE 8080/udp 8088/tcp
用于在image中创建一个挂载目录,以挂载宿主机上的目录
语法:
VOLUME <path> VOLUME ["path"]
VOLUME ["/var/log/“] VOLUME /myvol
CMD command param1 param2 #shell格式,含有shell环境 CMD ["executable","param1","param2”] #可执行程序格式 CMD ["param1","param2”] #第三种用于为ENTRYPOINT提供默认参数
注意:
ENTRYPOINT <command> ENTRYPOINT ["<executable>", "<param1>", "<param2>"]
示例:
[“nginx”,"-g","daemon off"]
USER <user>[:<group>]
USER <UID>[:<GID>]
USER nginx
语法:
STOPSIGNAL signal
示例:
STOPSIGNAL SIGKILL
HEALTHCHECK [OPTIONS] CMD command
其中OPTIONS有如下选项:
示例:
HEALTHCHECK --interval=5m --timeout=3s CMD curl -f http://localhost/ || exit 1
将可执行程序运行为shell环境,默认以/bin/sh -c运行
语法:
SHELL ["executable", "parameters"]
示例:
SHELL ["echo", “hello"] #等价于 RUN echo hello
ARG <name>[=<default value>]
FROM nginx ARG CONF="/tmp/nginx.conf" LABEL Author=wd RUN touch "${CONF}"
构建镜像:
[root@app51 ~]# docker build --build-arg CONF=‘/etc/test.conf‘ -t nginx:v15.2 ./ Sending build context to Docker daemon 225.6MB Step 1/4 : FROM nginx ---> f09fe80eb0e7 Step 2/4 : ARG CONF="/tmp/nginx.conf" ---> Using cache ---> ac081589c644 Step 3/4 : LABEL Author=wd ---> Using cache ---> 53b9b0ba4460 Step 4/4 : RUN touch "${CONF}" ---> Running in 50debe96f876 Removing intermediate container 50debe96f876 ---> d8680a2433bc Successfully built d8680a2433bc Successfully tagged nginx:v15.2
运行容器查看:
[root@app51 ~]# docker run --rm nginx:v15.2 ls /etc/test.conf -l -rw-r--r-- 1 root root 0 Feb 27 11:18 /etc/test.conf
用于在Dockerfile中定义一个触发器,当制作出来的镜像被别人用于基础镜像时候自动触发。
语法:
ONBUILD [INSTRUCTION]
示例:
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src
FROM golang:1.7.3 WORKDIR /go/src/github.com/alexellis/href-counter/ RUN go get -d -v golang.org/x/net/html COPY app.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . FROM alpine:latest RUN apk --no-cache add ca-certificates WORKDIR /root/ COPY --from=0 /go/src/github.com/alexellis/href-counter/app . CMD ["./app"]
默认情况下,stage未命名,可以通过整数来引用它们,第一个stage表示0,第二个表1以此类推。 但是,当有多个stage时候,这样会显得麻烦,Docker提供AS 语法可以为stage命名:
FROM golang:1.7.3 as builder
然后在另一个stage中使用:
COPY --from=builder /go/src/github.com/alexellis/href-counter/app .
除了可以使用Dockerfile中的stage外,构建镜像时候还可以直接使用本地已存在的环境和产物,例如:
COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf
标签:ebe 下载 exec port || off 法规 读写 方式
原文地址:https://www.cnblogs.com/lonelyxmas/p/10470357.html