⑩. USER 指定运行容器时的用户名或UID
# 这是我的第一个dockerfile镜像 FROM alpine # 相当于给当前容器开一个用户,以后的命令可以用这个用户运行 有可能没有执行权限 # 容器中的ROOT虽然不是lunux宿主机的真实root,但是可以改掉这个镜像的所有 USER 1000:1000 # 不自动解压 # 以linux主机的用户为准,默认是root用户,如果我们不指定权限将复制失败 COPY --chown=1000:1000 *.txt /a.txt # RUN 指令上下并没有上下文关系 RUN ls -l
[root@i-id8g0yu9 ~]# docker build --no-cache -t dockerfileworkuser:v1 -f dockerfileUSER . Sending build context to Docker daemon 2.473MB Step 1/4 : FROM alpine ---> 6dbb9cc54074 Step 2/4 : USER 1000:1000 ---> Running in 7d4fd2b2a54c Removing intermediate container 7d4fd2b2a54c ---> 2561ad4ce2c2 Step 3/4 : COPY --chown=1000:1000 *.txt /a.txt ---> a21c8da0fb0c Step 4/4 : RUN ls -l ---> Running in c9cbef9eac65 total 12 -rw-r--r-- 1 1000 1000 5 Apr 18 13:46 a.txt drwxr-xr-x 2 root root 4096 Apr 14 10:25 bin drwxr-xr-x 5 root root 340 Apr 18 14:22 dev drwxr-xr-x 1 root root 66 Apr 18 14:22 etc drwxr-xr-x 2 root root 6 Apr 14 10:25 home drwxr-xr-x 7 root root 247 Apr 14 10:25 lib drwxr-xr-x 5 root root 44 Apr 14 10:25 media drwxr-xr-x 2 root root 6 Apr 14 10:25 mnt drwxr-xr-x 2 root root 6 Apr 14 10:25 opt dr-xr-xr-x 115 root root 0 Apr 18 14:22 proc drwx------ 2 root root 6 Apr 14 10:25 root drwxr-xr-x 2 root root 6 Apr 14 10:25 run drwxr-xr-x 2 root root 4096 Apr 14 10:25 sbin drwxr-xr-x 2 root root 6 Apr 14 10:25 srv dr-xr-xr-x 13 root root 0 Apr 18 12:58 sys drwxrwxrwt 2 root root 6 Apr 14 10:25 tmp drwxr-xr-x 7 root root 66 Apr 14 10:25 usr drwxr-xr-x 12 root root 137 Apr 14 10:25 var Removing intermediate container c9cbef9eac65 ---> db89e3d275a2 Successfully built db89e3d275a2 Successfully tagged dockerfileworkuser:v1 [root@i-id8g0yu9 ~]#
⑩①. multi-stage builds 多阶段构建
# 最终版本 FROM maven:3.5.2-jdk-8-slim AS buildapp WORKDIR /app COPY src . COPY pom.xml . RUN mvn clean package RUN cp target/*.jar app.jar RUN ls -l FROM openjdk:8u282-slim RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone LABEL maintainer="845195485@qq.com" COPY --from=buildapp /app/app.jar /app.jar # COPY demo-0.0.1-SNAPSHOT.jar /app.jar EXPOSE 8080 # CMD ["--server.prot=8080"] ENV JAVA_OPTS="" ENV PARAMS="" ENTRYPOINT [ "sh", "-c", "java -Djava.security.egd=file:/dev/./urandom $JAVA_OPTS -jar /app.jar $PARAMS" ]