CC00111.CloudKubernetes——|KuberNetes&Docker操作.V09|——|dockerfile.v04|workdir&user&volume|

一、Dockerfile:WORKDIR参数
### --- 创建dockerfile

[root@k8s-master01 dockerfiles]# vim Dockerfile 
FROM centos:8

LABEL maintainer="test dockerfile"
LABEL test=dockerfile

ENV test_env1 env1
ENV test_env2 env2
ENV env1=test1 env2=test2

RUN useradd yanqi
RUN mkdir /opt/yanqitest

#ENTRYPOINT ["echo"]

ADD ./index.html.tar.gz /opt/
COPY ./index.html.tar.gz /opt/yanqitest/
WORKDIR /opt/yanqi
CMD pwd ; ls
#RUN useradd yanqi && /opt/yanqi
### --- 构建镜像

[root@k8s-master01 dockerfiles]# docker build -t centos:workdir .
Step 11/12 : WORKDIR /opt/yanqitest
 ---> Running in 5fe980f90f6d
Removing intermediate container 5fe980f90f6d
 ---> 3fa9782e2702
Step 12/12 : CMD pwd ; ls
 ---> Running in f679f7ffa9ad
Removing intermediate container f679f7ffa9ad
 ---> d91a6af60fe4
Successfully built d91a6af60fe4
Successfully tagged centos:workdir
### --- 运行容器
~~~     注:WORKDIR会经常应用到的。

[root@k8s-master01 dockerfiles]# docker run -ti --rm centos:workdir
/opt/yanqi                              // 已切换到/opt/yanqi目录下
index.html.tar.gz                       // 并且ls查看到的文件信息
二、dockerfile:USER参数:使用容器执行的用户
### --- 运行容器并查看用户
~~~     # 运行容器

[root@k8s-master01 dockerfiles]# docker run -ti --rm centos:workdir bash
~~~     # 查看用户

[root@0044a0be125a yanqitest]# whoami
root                //当前是root用户
[root@0044a0be125a ~]# cat /etc/passwd
yanqi:x:1000:1000::/home/yanqi:/bin/bash    // 可以使用yanqi用户
### --- 创建dockerfile

[root@k8s-master01 dockerfiles]# vim Dockerfile
FROM centos:8

LABEL maintainer="test dockerfile"
LABEL test=dockerfile

ENV test_env1 env1
ENV test_env2 env2
ENV env1=test1 env2=test2

RUN useradd yanqi
RUN mkdir /opt/yanqi

#ENTRYPOINT ["echo"]

ADD ./index.html.tar.gz /opt/
COPY ./index.html.tar.gz /opt/yanqitest/
WORKDIR /opt/yanqitest
USER 1000
CMD pwd ; ls
#RUN useradd yanqi && /opt/yanqi
### --- 构建镜像

[root@k8s-master01 dockerfiles]# docker build -t centos:workdir .
Step 12/13 : USER 1000
 ---> Running in 4fc17065dfa3
Removing intermediate container 4fc17065dfa3
 ---> e3646ef7236c
Step 13/13 : CMD pwd ; ls
 ---> Running in bd9911d6f0d8
Removing intermediate container bd9911d6f0d8
 ---> 81a90d428efe
Successfully built 81a90d428efe
Successfully tagged centos:workdir
### --- 运行容器并查看镜像层
~~~     # 运行容器

[root@k8s-master01 dockerfiles]# docker run -ti --rm centos:workdir bash
[yanqi@28fd5f7e8e30 yanqitest]$ whoami
yanqi                                       // 可以查看到当前用户已经切换为yanqi
~~~     # 推送到hub仓库
~~~     推送到镜像仓库,默认是推送到官方的地址,若是需要更改地址,需要前面加入域名

[root@k8s-master01 dockerfiles]# docker build -t xxx.com/centos:wordir .
~~~     # 查看镜像层
~~~     mone镜像,可能是镜像之前的层,有可能会用到这些层。这些层可以清理掉,不清理也没有关系

[root@k8s-master01 dockerfiles]# docker images
xxx.com/centos                                                    wordir    81a90d428efe   2 minutes ago    210MB
三、dockerfile:VOLUME参数
### --- 创建dockerfile

[root@k8s-master01 dockerfiles]# vim Dockerfile 
FROM centos:8

LABEL maintainer="test dockerfile"
LABEL test=dockerfile

ENV test_env1 env1
ENV test_env2 env2
ENV env1=test1 env2=test2

RUN useradd yanqi
RUN mkdir /opt/yanqi

#ENTRYPOINT ["echo"]

ADD ./index.html.tar.gz /opt/
COPY ./index.html.tar.gz /opt/yanqitest/
WORKDIR /opt/yanqitest
#USER 1000
VOLUME /data
CMD pwd ; ls
#RUN useradd yanqi && /opt/yanqi
### --- 构建镜像

[root@k8s-master01 dockerfiles]# docker build -t xxx.com/centos:wordir .
Step 12/13 : VOLUME /data
 ---> Running in 1af4e46a9d4a
Removing intermediate container 1af4e46a9d4a
 ---> 569977d829a1
Step 13/13 : CMD pwd ; ls
 ---> Running in b6de07554ab4
Removing intermediate container b6de07554ab4
 ---> f5fe200aa73c
Successfully built f5fe200aa73c
Successfully tagged xxx.com/centos:wordir
### --- 运行容器并查看目录

[root@k8s-master01 ~]# docker run -ti xxx.com/centos:wordir bash
~~~     # 可以查看到目录已经创建好了。
~~~     注:用法:开发写法习惯,不是基于容器开发的,而是把数据存储目录放在本地。
~~~     注:解决方案一:使用mkdir去创建这个目录。
~~~     注:解决方案二:使用VOLUME去创建挂载目的目录。

[root@1f40a3762b1d ~]# ls /data/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yanqi_vip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值