使用Dockerfile创建镜像
作者:互联网
1、基本结构
Dockerfile由一行行命令语句组成,并且支持以#开头的注释行。
一般而言,Dockerfile主体内容分为四部分:基础镜像镜像、维护者信息、镜像操作指令和容器启动时执行指令
2、指令说明
Dockerfile中指令的一般格式为:INSTRUCTION arguments,包括配置指令(配置镜像信息)和操作指令(具体执行操作)
分类 | 指令 | 说明 |
---|---|---|
配置指令 | ARG | 定义创建镜像过程中使用的变量 |
FROM | 指定所创建镜像的基础镜像 | |
LABEL | 为生成的镜像添加元数据标签信息 | |
EXPOSE | 声明镜像内服务监听的端口 | |
ENV | 指定环境变量 | |
ENTRYPOINT | 指定镜像的默认入口命令 | |
VOLUME | 创建一个数据卷挂载点 | |
USER | 指定运行容器时的用户名或UID | |
WORKDIR | 配置工作目录 | |
ONBUILD | 创建子镜像时指定自动执行的操作指令 | |
STOPSIGNAL | 指定退出的信号值 | |
HEALTHCHECK | 配置所启动容器如何进行健康检查 | |
SHELL | 指定默认shell类型 | |
操作指令 | RUN | 运行指定命令 |
CMD | 启动容器时指定默认执行的命令 | |
ADD | 添加内容到镜像 | |
COPY | 复制内容到镜像 |
示例:
FROM node
MAINTAINER Taolei
RUN git clone -q https://github.com/docker-in-practice/todo.git
WORKDIR todo
RUN npm install > /dev/null
EXPOSE 8000
CMD ["npm","start"]
构建过程如下:
[root@aliyun docker]# pwd
/home/docker
#构建一个镜像
[root@aliyun docker]# docker build .
Sending build context to Docker daemon 2.048kB
Step 1/7 : FROM node
latest: Pulling from library/node
0e29546d541c: Pull complete
9b829c73b52b: Pull complete
cb5b7ae36172: Pull complete
6494e4811622: Pull complete
6f9f74896dfa: Pull complete
f2930ff7fb60: Pull complete
c1d26179dd86: Pull complete
1fa56dd41537: Pull complete
321141c774e9: Pull complete
Digest: sha256:36aca218a5eb57cb23bc790a030591382c7664c15a384e2ddc2075761ac7e701
Status: Downloaded newer image for node:latest
---> a283f62cb84b
Step 2/7 : MAINTAINER Taolei
---> Running in c367d3410334
Removing intermediate container c367d3410334
---> e7567eaaf35c
Step 3/7 : RUN git clone -q https://github.com/docker-in-practice/todo.git
---> Running in 9191275f23b1
Removing intermediate container 9191275f23b1
---> 5d80658a509b
Step 4/7 : WORKDIR todo
---> Running in f297028141cf
Removing intermediate container f297028141cf
---> a81464358a14
Step 5/7 : RUN npm install > /dev/null
---> Running in 19d637151604
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE package: 'minifyify@4.4.0',
npm WARN EBADENGINE required: { node: '0.10.x' },
npm WARN EBADENGINE current: { node: 'v17.3.0', npm: '8.3.0' }
npm WARN EBADENGINE }
npm WARN deprecated graceful-fs@2.0.3: please upgrade to graceful-fs 4 for compatibility with current and future versions of Node.js
npm WARN deprecated buffer-browserify@0.2.5: Package not maintained. Recent browserify uses https://github.com/feross/buffer
npm WARN deprecated mkdirp@0.3.5: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
npm WARN deprecated mkdirp@0.3.5: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
npm WARN deprecated minimatch@0.3.0: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated minimatch@0.3.0: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated react-tools@0.11.2: react-tools is deprecated. For more information, visit https://fb.me/react-tools-deprecated
npm notice
npm notice New minor version of npm available! 8.3.0 -> 8.6.0
npm notice Changelog: <https://github.com/npm/cli/releases/tag/v8.6.0>
npm notice Run `npm install -g npm@8.6.0` to update!
npm notice
Removing intermediate container 19d637151604
---> d1b737e8b68f
Step 6/7 : EXPOSE 8000
---> Running in 27157638d7d6
Removing intermediate container 27157638d7d6
---> 6fafd82e1672
Step 7/7 : CMD ["npm","start"]
---> Running in fe6d70c5ec05
Removing intermediate container fe6d70c5ec05
---> eafa085c10fa
Successfully built eafa085c10fa
步骤说明:
- docker会上传docker build指定目录下的文件和目录
- 每个构建步骤从1开始按顺序编号,并与命令一起输出
- 每个命令会导致一个新镜像被创建出来,其镜像ID在此输出
- 为节省空间,在继续前每个中间容器会被移除
- 构建的调试信息在此输出
- 此次构建的最终镜像ID,可用于打标签
打标签
#打标签前
[root@aliyun docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> eafa085c10fa 7 minutes ago 1.1GB
busybox latest beae173ccac6 3 months ago 1.24MB
node latest a283f62cb84b 3 months ago 993MB
#打标签时
[root@aliyun docker]# docker tag eafa085c10fa todoapp
#打标签后
[root@aliyun docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
todoapp latest eafa085c10fa 8 minutes ago 1.1GB
busybox latest beae173ccac6 3 months ago 1.24MB
node latest a283f62cb84b 3 months ago 993MB
标签:npm,WARN,Pull,创建,---,镜像,docker,Dockerfile 来源: https://www.cnblogs.com/Torres-tao/p/16111020.html