0
点赞
收藏
分享

微信扫一扫

docker file

彪悍的鼹鼠 2021-09-24 阅读 77
PaaS

docker file简介

Dockerfile 是由一个个的指令组成,是用于表示创建一个镜像文件的过程。

docker file 详解

环境变量env

echo ${NAME:-tom}  //没值显示默认值

NAME=test

echo ${NAME:-tom} //有值显示设置的值

echo ${NAME:+tom}  //只要有值则显示tom

NAME=test

unset NAME

echo ${NAME:+tom} //没值显示空

Dockerfile 指令

FROM命令用法说明

MAINTAINER命令用法说明

RUN命令用法说明

CMD命令用法说明

EXPOSE命令用法说明

ENV命令用法说明

ADD命令用法说明

COPY命令用法说明

ENTRYPOINT命令用法说明

VOLUME命令用法说明

USER命令用法说明

WORKDIR命令用法说明

docker file基于tomcat例子

镜像构建实践

提供文件index.html

编写Dockerfile

每一条指令都会生成一个镜像层

执行构建命令

docker build -h

docker build -t testhttpd:v1 ./

docker images

docker run --name testhttpd --rm testhttpd:v1 cat /data/web/html/index.html

使用COPY指令

cp -r /etc/yum.repos.d/ .

vi Dockerfile  //在原有文件后加一行

docker build -t testhttpd:v2 ./    //重新构建

docker run --name testhttpd --rm testhttpd:v2 ls /etc/yum.repos.d/

对比一下:v1版本镜像无yum.repos.d目录

docker run --name testhttpd --rm testhttpd:v1 ls /etc/

将最后一行改一下目录名:不存在的目录将新建目录,并将目录中的文件放入新创建的目录中

docker build -t testhttpd:v3 ./

docker run --name testhttpd --rm testhttpd:v3 ls /etc/

将最后一行改成已存在的目录名:将目录中的文件放入已存在的目录中

docker build -t testhttpd:v4 ./

docker run --name testhttpd --rm testhttpd:v4 ls /etc/

docker run --name testhttpd --rm testhttpd:v4 ls -al /etc/network

使用ADD指令

docker build -t testhttpd:v5 ./

docker run --name testhttpd --rm testhttpd:v5 ls -al /usr/local/src

手工下载nginx压缩包

wget http://nginx.org/download/nginx-1.15.2.tar.gz

vi Dockerfile  //改一下最后一行,从本地拷贝

docker build -t testhttpd:v6 ./

docker run --name testhttpd --rm testhttpd:v6 ls -al /usr/local/src

使用WORKDIR

vi Dockerfile

docker build -t testhttpd:v7 ./

docker run --name testhttpd --rm testhttpd:v7 ls -al /usr/local/src

vi Dockerfile

docker build -t testhttpd:v7 ./

docker run --name testhttpd --rm testhttpd:v7 ls -al /usr/local/src

docker exec -it testhttpd sh  //直接进入容器的话,就是设置的当前工作目录

使用VOLUME指令

docker build -t testhttpd:v8 ./

docker run --name testhttpd --rm testhttpd:v8 mount

可以直接使用grep mysql过滤出来

docker run --name testhttpd --rm testhttpd:v8 mount|grep mysql

docker run --name testhttpd --rm testhttpd:v8 sleep 3600

docker inspect testhttpd

使用EXPOSE指令

动态绑定宿主机的端口

docker build -t testhttpd:v9 ./

docker run --name testhttpd --rm testhttpd:v9 /bin/httpd -f -h /data/web/html

curl 172.18.0.2

docker port testhttpd //没有暴露端口

加-P暴露任何端口

docker run --name testhttpd --rm -P testhttpd:v9 /bin/httpd -f -h /data/web/html

docker port testhttpd

curl localhost:32768

使用ENV指令

vi Dockerfile   //只配置一个ENV,用空格分隔

docker build -t testhttpd:v10 ./

vi Dockerfile  // //配置多个ENV,用=分隔;多行用\分隔

docker build -t testhttpd:v10 ./

docker run --name testhttpd --rm -P testhttpd:v10 ls /usr/local/src

docker run --name testhttpd --rm -P testhttpd:v10 ls /data/web/html

docker run --name testhttpd --rm -P testhttpd:v10 printenv

docker run 参数设置

docker run --help

docker run --name testhttpd --rm -P -e DOC_SERVER="tomcat" testhttpd:v10 printenv

使用RUN指令

vi Dockerfile

docker build -t testhttpd:v11 ./

docker run --name testhttpd --rm -P -e DOC_SERVER="tomcat" -it testhttpd:v11 ls /usr/local/src

vi Dockerfile

docker build -t testhttpd:v12 ./

docker run --name testhttpd --rm -P -e DOC_SERVER="webserver" -it testhttpd:v12 ls /usr/local/src

使用CMD指令

vi Dockerfile

docker build -t testhttpd:v12 ./

docker image inspect testhttpd:v12

docker run --name testhttpd --rm -P  -it testhttpd:v12 

docker exec -it testhttpd sh

vi Dockerfile

docker build -t testhttpd:v13 ./

docker image inspect testhttpd:v13

docker run --name testhttpd --rm -P  -it testhttpd:v13

vi Dockerfile

docker build -t testhttpd:v13 ./

docker run --name testhttpd --rm -P -it testhttpd:v13

举报

相关推荐

0 条评论