Dockerfile 介绍

发布时间: 更新时间: 总字数:767 阅读时间:2m 作者: 分享 复制网址

Dockerfile 是一个文本文件,它包含了生成镜像所需的所有命令,用来制作Docker的镜像。

介绍

构建镜像目的:

  1. 保存对容器的修改,并再次使用
  2. 自定义镜像的能力
  3. 以软件的形式打包并分发服务以及其运行环境

什么是 Dockerfile

通过Dockerfile构建镜像,Dockerfile 是一个文本文件,它包含了生成镜像所需的所有命令,用来制作Docker的镜像。

HEALTHCHECK

  • HEALTHCHECK 用来配置健康检查来监控容器的运行状态,当检查失败时,容器将被标记为 unhealthy 状态(通过 docker ps 可以查看)
FROM nginx:latest

HEALTHCHECK --interval=5s --timeout=3s CMD curl -f http://localhost/ || exit 1
# HEALTHCHECK &{["CMD-SHELL" "\"/opt/appsmith/healthcheck.sh\""] "15s" "15s"

...

CMD

  • CMD 设置默认命令,该命令仅在运行容器且不指定命令的时候运行,有三种形式:
# Exec 形式, 推荐
CMD ["executable", "param1", "param2"]

# Exec 形式为 ENTRYPOINT 设置默认参数
CMD ["param1", "param2"]

# Shell 形式
CMD command param1 param2

ENTRYPOINT

  • ENTRYPOINT 配置运行的容器命令(可执行文件)
# Exec 形式
ENTRYPOINT ["/bin/echo", "Hello"]
CMD ["world"]

# Shell 形式
ENTRYPOINT command param1 param2

构建

通过容器构建

docker commit 提交运行中的容器为镜像

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
 -a, --author="" Author
                             e.g,"xiexianbin me@xiexianbin.cn"
 -m, --message="" commit message
 -p,--pause=true Pause container during commit
docker run -it -p 80 --name commit_test ubuntu /bin/bash
apt-get install nginx
exit
docker commit -a "xiexianbin" -m "xiexianbin" commit_test xiexianbin:nginx:commit
docker images
docker run -d --name nginx_web -p 80 xiexianbin/nginx:commit nginx -g "daemo off;"
docker ps
curl http://127.0.0.1:4980
docker push xiexianbin/nginx:commit

通过 Dockerfile 文件构建

  • 创建 Dockerfile 文件
# first Docker
From ubuntu:14.04
MAINTATIER xiexianbin "me@xiexianbin.cn"
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80
  • build args
docker build [OPTIONS] PATH | URL | -
 --force-rm=false
 --no-cache=false
 --pull=false
 -q,--quiet=false
 --rm=true
 -t,--tag=""
 --build-arg "HTTP_PROXY=http://proxy.xiexianbin.cn/" # build 参数,代理设置
 --build-arg "HTTPS_PROXY=http://proxy.xiexianbin.cn/"
  • build
docker build -t='xiexianbin/nginx' .

构建技巧

每条 instruction 的耗时

# 1. 开启
export DOCKER_BUILDKIT=1

# 2. 参数 --progress plain 获得输出结果
docker build -t <image-name> -f Dockerfile --progress plain .

Build Cache

以 Nodejs 项目构建为例:

FROM node:8
WORKDIR /usr/src/app
# package.json 不经常变动,可以缓存起来
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "..." ]

build 时引用宿主机的环境变量

  • Dockerfile
$ cat Dockerfile
FROM alpine

ARG VAR1=${VAR1:-""}
ENV VAR2=${VAR1}

RUN echo "VAR1 is ${VAR1}, VAR2 is ${VAR2}"

CMD echo "VAR1 is ${VAR1}, VAR2 is ${VAR2}"
  • 制作镜像
$ docker build -f Dockerfile --build-arg VAR1="<password>" . -t test-env:1
...
 => [2/2] RUN echo "VAR1 is <password>, VAR2 is <password>"    0.3s
...
  • 测试
$ docker run test-env:1
VAR1 is , VAR2 is <password>

说明:

  • 不设置 ENV 时,可以用于传递密码等信息

默认shell说明

Dockerfile 默认使用的 shell 是 sh,采用如下方式切换到 bash

FROM xxx

SHELL ["/bin/bash", "-c"]
RUN echo I am now using bash!

/etc/profile 不生效问题

在 Docker 容器启动时,若要运行 sh 脚本,需要放在 /root/.bashrc 中,放到 /etc/profile 不生效

docker runtime to dockerfile

  • whaler
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数