Dockerfile

https://www.cnblogs.com/panwenbin-logs/p/8007348.html
github 文档 docker 官方文档

Dockerfile

用于制作docker镜像的设计稿

# 使用基础镜像,本地没有会从registry拉取,from必须是第一个指令
from perl 

# 维护者信息系,作者
maintainer ajanuw

# 设置环境变量,可以设置多条,运行时可以通过-e参数修改:docker run -e KEY=value2 image
env KEY=value

# 构建镜像时执行的命令
# 通常用来执行改变镜像的命令,安装软件,或者创建配置文件。
run echo "start..."

# add <local_path> <container_path> 添加本地文件和目录到镜像中, tar类型文件会自动解压,可以访问网络资源,类似wget
# 把要构建进容器的文件和目录放在Dockerfile所在的目录中
add ./init.txt /

# copy <local_path> <container_path> 类似 add 但是是不会自动解压文件,也不能访问网络资源
copy ./main.pl /src/

# 如果没有 entrypoint 启动容器的时候运行的命令就是cmd指定的命令
# 如果有entrypoint,则会运行entrypoint的命令,而cmd的值会被当作entrypoint的选项
# Dockerfile只能有一个cmd行
# 可以直接写成 cat /etc/redhat-release /etc/hosts 但这个命令会被shell(bash -c "<bach>")执行,而不是直接执行
# 运行时覆盖cmd命令,如:
# λ docker build -t test .
# λ docker run test  bash
# 在末尾添加参数,cmd就会被忽略,如果不想被忽略就用 entrypoint
cmd ["cat", "/etc/redhat-release", "/etc/hosts"]


# 使用docker run时要执行的命令
# --entrypoint="<bash>" 将会覆盖entrypoint的默认命令
entrypoint


# 添加标签,可设置多条,key和value随便设置,构建为镜像后 docker inspect -f="{{.Config.Labels}}" image可以查看到
# 标签重复会使用最后一个
# 标签用在别的docker命令使用--filter选项时使用:docker images --filter "label=a=1"
label a="1" b="2"

# 暴露出容器的80端口
# 其他容器可以使用暴露的接口
# 容器运行时加了 -P 参数,将所有暴露的端口随机分配到主机的端口上,docker port可以查看具体情况 
# 默认TCP协议,53/udp 更改协议
expose 80

同时设置cmd和entrypoint

cmd ["/etc/redhat-release", "/etc/hosts"]
entrypoint ["cat"]
λ docker run test  // 运行默认的两个参数
CentOS Linux release 7.6.1810 (Core)
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      0f4e36857387

λ docker run test /etc/issue // 覆盖了默认参数执行cat /etc/issue
S
Kernel 
 on an m

只设置 entrypoint

在结尾添加参数将不会被覆盖,而是追加

entrypoint ["cat","/etc/redhat-release", "/etc/hosts"]
λ docker run test1
CentOS Linux release 7.6.1810 (Core)
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      2afa66e264d2

λ docker run test1 /etc/issue
CentOS Linux release 7.6.1810 (Core)
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      506f2902e530
S
Kernel 
 on an m

nginx 示例

编写 Dockerfile

from nginx
expose 80

使用 Dockerfile 构建 Image

λ docker build -t test-nginx .
λ docker images

-t 设置 Image 标签

运行 Image 构建 Container

λ docker run -d -p 80:80 -v /var/www/yyy.com/web/:/usr/share/nginx/html/ --name=website test-nginx
λ docker ps -a
  • -d 后台运行
  • -p 设置端口 左边主机端口,右边容器端口
  • -v 设置卷 左边主机绝对路径,右边容器绝对路径
  • --name 命名容器

想在使用浏览器访问 本机ip 就可以看到页面,并且修改本机下的 "/var/www/yyy.com/web/index.html" 页面的内容也会修改