|NO.Z.00111|——————————|CloudNative|——|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/                    

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(18)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示