从Docker镜像仓库获取命令的格式是:
docker pull [选项] [Docker Registry 地址[:端口号]] 仓库名[:标签]
具体的选项可以通过docker pull --help查看,例子:
docker pull ubuntu:18.04 #默认地址为Docker Hub,仓库名(<用户名>/<软件名>格式)中默认用户名libray
列出已经下载的镜像:
docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 568c4670fa80 3 weeks ago 109MB ubuntu 18.04 93fd78260bd1 4 weeks ago 86.2MB
显示包括中间层镜像的所有镜像:
docker image ls -a
列出部分镜像:
docker image ls ubuntu
使用过滤参数:
docker image ls -f since=ubuntu:16.04
仅列出ID:
docker image ls -q
这里的size并非镜像的实际所占的内存空间,要查看镜像、容器、数据卷所占用空间:
docker system df TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 2 0 195.3MB 195.3MB (100%) Containers 0 0 0B 0B Local Volumes 0 0 0B 0B Build Cache 0 0 0B 0B
docker image rm [选项] <镜像1> [<镜像2......>]
其中镜像1,镜像2可以是镜像的ID,名称,摘要
举例:
docker image rm $(docker image ls -q redis) docker image rm $(docker image ls -q since=ubuntu:18.04)
Dockerfile 是一个文本文件,其内包含了一条条的指令(Instruction),每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。
1.使用FROM指定基础镜像
FROM指令是必备指令,且必须是第一条指令。例子
FROM ubuntu:18.04
#使用空白镜像为基础镜像
FROM scratch
2.使用RUN执行命令
RUN指令用来执行命令,其格式有两种
1)shell格式:RUN <命令> 例如:
RUN echo ‘<h1>Hello, Docker!</h1>‘ > /usr/share/nginx/html/index.html
2)exec格式:RUN ["可执行文件","参数1","参数2"]
Dockerfile中的每一个指令都会建立一层,RUN也不例外,错误写法:
FROM debian:stretch RUN apt-get update RUN apt-get install -y gcc libc6-dev make wget RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" RUN mkdir -p /usr/src/redis
正确写法:
RUN buildDeps=‘gcc libc6-dev make wget‘ && apt-get update && apt-get install -y $buildDeps && wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" && mkdir -p /usr/src/redis && tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1 && make -C /usr/src/redis && make -C /usr/src/redis install && rm -rf /var/lib/apt/lists/* && rm redis.tar.gz && rm -r /usr/src/redis && apt-get purge -y --auto-remove $buildDeps #清理命令
3.构建镜像
构建命令需要在Dockerfile文件所在的目录执行,命令格式为:
docker build [选项] <上下文路径/URL/>
例如
docker build -t nginx:v3 .
4.理解镜像构建上下文(Context)
当构建的时候,用户会指定构建镜像上下文的路径,docker build
命令得知这个路径后,会将路径下的所有内容打包,然后上传给 Docker 引擎。
5.其他docker build用法
1)直接用git repo构建
docker build https://github.com/twang2218/gitlab-ce-zh.git#:11.1 #其中构建目录为/11.1/
2)用tar包构建
docker build http://server/context.tar.gz
3)从标准输入中读取
docker build - < Dockerfile
cat Dockerfile | docker build -
1.COPY
该命令用于复制文件,其格式为:
COPY [--chown=<user>:<group>] <源路径>... <目标路径> COPY [--chown=<user>:<group>] ["<源路径1>",... "<目标路径>"]
注意:使用该指令复制的文件,各种元数据都会被保留。例子:
COPY hom* /mydir/ COPY hom?.txt /mydir/ COPY --chown=55:mygroup files* /mydir/ COPY --chown=bin files* /mydir/ COPY --chown=1 files* /mydir/ COPY --chown=10:11 files* /mydir/
<目标路径>
可以是容器内的绝对路径,也可以是相对于工作目录的相对路径
2.CMD容器启动命令
用于指定默认容器主进程的启动命令。
CMD命令格式:
shell 格式:CMD <命令> exec 格式:CMD ["可执行文件", "参数1", "参数2"...] 参数列表格式:CMD ["参数1", "参数2"...]。在指定了 ENTRYPOINT 指令后,用 CMD 指定具体的参数。
例如:
CMD echo $HOME
3.ENTRYPOINT入口点
https://yeasy.gitbooks.io/docker_practice/content/image/dockerfile/entrypoint.html
4.ENV设置环境变量
5.ARG构建参数
6.VOLUME定义匿名卷
7.EXPOSE暴露端口
8.WORKDIR指定工作目录
FROM golang:1.9-alpine as builder RUN apk --no-cache add git WORKDIR /go/src/github.com/go/helloworld/ RUN go get -d -v github.com/go-sql-driver/mysql COPY app.go . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . FROM alpine:latest as prod RUN apk --no-cache add ca-certificates WORKDIR /root/ COPY --from=0 /go/src/github.com/go/helloworld/app . CMD ["./app"]
构建镜像
docker build -t go/helloworld:3 .
对比三个镜像的大小:
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE go/helloworld 3 d6911ed9c846 7 seconds ago 6.47MB go/helloworld 2 f7cf3465432c 22 seconds ago 6.47MB go/helloworld 1 f55d3e16affc 2 minutes ago 295MB
我们可以使用 as
来为某一阶段命名:
FROM golang:1.9-alpine as builder
例如当我们只想构建 builder
阶段的镜像时,增加 --target=builder
参数即可
docker build --target builder -t username/imagename:tag .
构建时从其他镜像复制文件
上面例子中我们使用 COPY --from=0 /go/src/github.com/go/helloworld/app .
从上一阶段的镜像中复制文件,我们也可以复制任意镜像中的文件。
COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf
原文:https://www.cnblogs.com/Shadowplay/p/10171864.html