apk太慢
sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
构建自己的镜像
# --file指定Dockerfile名字
# <REPOSITORY>通常在Docker Hub上注册的用户名
# <TAG>是描述镜像的唯一值 类似镜像名
# . Dockerfile文件所处路径
docker image build --file <path_to_Dockerfile> --tag <REPOSITORY>:<TAG> .
# 构建镜像
docker image build --tag local:dockerfile-example .
# 查看可用镜像
docker image ls
# 运行容器 -d 后台运行 --name dockerfile-example 运行容器名 local:dockerfile-example镜像名
docker container run -d --name dockerfile-example -p 8080:80 local:dockerfile-example
# nginx -v 覆盖Dockerfile CMD
docker container run --name nginx-version local:dockerfile-example -v
# 查看嵌入的标签
docker image inspect -f {{.Config.Labels}} local:dockerfile-example
# 停止并删除容器(非镜像 需要先停止再删除
docker container stop dockerfile-example
docker container rm dockerfile-example nginx-version
使用现有容器
# 下载作为基础的镜像 alpine Linux
docker image pull alpine:latest
# 与容器进行交互
docker container run -it --name alpine-test alpine /bin/sh
# 以下命令安装 NGINX
apk update
apk upgrade
apk add --update nginx
rm -rf /var/cache/apk/*
mkdir -p /tmp/nginx/
exit
# 将我们停止的容器保存为镜像
docker container commit <container_name> <REPOSITORY>:<TAG>
docker container commit alpine-test local:broken-container
# 保存图像文件
docker image save -o <name_of_file.tar> <REPOSITORY>:<TAG>
docker image save -o broken-container.tar local:broken-container
tar -xvf broken-container.tar
使用 Dockerfile,您可以准确地看到创建映像所执行的操作,但使用该种方法,对该镜像的可见性为零。
另一个原因是你很难建立一套好的默认值。例如,如果您要以这种方式构建图像,那么您将无法真正利用ENTRYPOINT和CMD等功能,甚至无法利用最基本的指令,例如EXPOSE. 相反,用户必须在运行docker container run命令时定义所需的一切。
这是docker早期的一种做法
# 编写Dockerfile scratch为docker hub中默认创建的空文件
# https://www.alpinelinux.org/downloads/
FROM scratch
ADD files/alpine-minirootfs-3.11.3-x86_64.tar.gz /
CMD [“/bin/sh”]
# 建立镜像
docker image build --tag local:fromscratch .
# 运行镜像
docker container run -it --name alpine-test local:fromscratch /bin/sh
使用 ENV
ENV <key> <value>
ENV username admin
# 也可以在键和值之间放置一个等号
ENV <key>=<value>
ENV username=admin
# 每行只能设置一个ENV 同一行设置多个环境变量
ENV username=admin database=wordpress tableprefix=wp
# 查看在镜像上设置的ENV
docker image inspect <IMAGE_ID>
使用 Alpine Linux,我们将执行以下操作:
设置一个 ENV 来定义我们想要安装的 PHP 版本。
安装 Apache2 和我们选择的 PHP 版本。
设置镜像以便 Apache2 启动时不会出现问题。
删除默认index.html文件并添加一个index.php显示phpinfo命令结果的文件。
暴露容器上的端口 80。
设置 Apache 使其成为默认进程
ROM alpine:3.8
LABEL maintainer=”Russ McKendrick <russ@mckendrick.io>”
LABEL description=”This example Dockerfile installs Apache & PHP.”
ENV PHPVERSION 7
RUN apk add --update apache2 php${PHPVERSION}-apache2 php${PHPVERSION} && \
rm -rf /var/cache/apk/* && \
mkdir /run/apache2/ && \
rm -rf /var/www/localhost/htdocs/index.html && \
echo “<?php phpinfo(); ?>” > /var/www/localhost/htdocs/index.php && \
chmod 755 /var/www/localhost/htdocs/index.php
EXPOSE 80/tcp
ENTRYPOINT [“httpd”]
CMD [“-D”, “FOREGROUND”]
docker build --tag local/apache-php:7 .
docker container run -d -p 8080:80 --name apache-php7 local/apache-php:7
# 更改Dockerfile中的ENV PHPVERSION 5
docker image build --tag local/apache-php:5 .
docker container run -d -p 9090:80 --name apache-php5 local/apache-php:5
docker image ls
构建一个更复杂的容器
# installs and configures Consul by HashiCorp
# Dockerfile
FROM alpine:latest
LABEL maintainer=”Russ McKendrick <russ@mckendrick.io>”
LABEL description=”An image with the latest version on Consul.”
ENV CONSUL_VERSION 1.7.1
ENV CONSUL_SHA256 09f3583c6cd7b1f748c0c012ce9b3d96de95a6c0d2334327b74f7d72b1fa5054
RUN apk add --update ca-certificates wget && \
wget -O consul.zip https://releases.hashicorp.com/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_amd64.zip && \
echo “$CONSUL_SHA256 *consul.zip” | sha256sum -c - && \
unzip consul.zip && \
mv consul /bin/ && \
rm -rf consul.zip && \
rm -rf /tmp/* /var/cache/apk/*
EXPOSE 8300 8301 8301/udp 8302 8302/udp 8400 8500 8600 8600/udp
VOLUME [ “/data” ]
ENTRYPOINT [ “/bin/consul” ]
CMD [ “agent”, “-data-dir”, “/data”, “-server”, “-bootstrap-expect”, “1”, “-client=0.0.0.0”]
多阶段(multi-stage)构建容器
下载build环境容器镜像并启动build容器。
将源代码复制到build容器中。
在build容器上编译源代码。
将编译后的二进制文件复制到build容器外。
移除build容器。
使用预先编写的 Dockerfile 构建镜像并将二进制文件复制到其中。
# 之前都是通过包管理器(例如Alpine Linux的APK)直接将二进制文件添加到我们的镜像中
# 即下载预编译的二进制文件
# 将官方go容器重命名为builder
FROM golang:latest as builder
WORKDIR /go-http-hello-world/
# go代理
# RUN go env -w GO111MODULE=on
# RUN go env -w GOPROXY=https://goproxy.cn,direct
RUN go get -d -v golang.org/x/net/html
# RUN go env -w GO111MODULE=off
ADD https://raw.githubusercontent.com/geetarista/go-http-hello-world/master/hello_world/hello_world.go ./hello_world.go
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM scratch
COPY --from=builder /go-http-hello-world/app .
CMD [“./app”]
docker image build --tag local:go-hello-world .
docker container run -d -p 8000:80 --name go-hello-world local:go-hello-world
# docker container stop go-hello-world && docker container rm go-hello-world
# docker container prune 停止所有容器