apk太慢

  1. sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories

构建自己的镜像

  1. # --file指定Dockerfile名字
  2. # <REPOSITORY>通常在Docker Hub上注册的用户名
  3. # <TAG>是描述镜像的唯一值 类似镜像名
  4. # . Dockerfile文件所处路径
  5. docker image build --file <path_to_Dockerfile> --tag <REPOSITORY>:<TAG> .
  1. # 构建镜像
  2. docker image build --tag local:dockerfile-example .
  3. # 查看可用镜像
  4. docker image ls
  5. # 运行容器 -d 后台运行 --name dockerfile-example 运行容器名 local:dockerfile-example镜像名
  6. docker container run -d --name dockerfile-example -p 8080:80 local:dockerfile-example
  7. # nginx -v 覆盖Dockerfile CMD
  8. docker container run --name nginx-version local:dockerfile-example -v
  9. # 查看嵌入的标签
  10. docker image inspect -f {{.Config.Labels}} local:dockerfile-example
  11. # 停止并删除容器(非镜像 需要先停止再删除
  12. docker container stop dockerfile-example
  13. docker container rm dockerfile-example nginx-version

使用现有容器

  1. # 下载作为基础的镜像 alpine Linux
  2. docker image pull alpine:latest
  3. # 与容器进行交互
  4. docker container run -it --name alpine-test alpine /bin/sh
  5. # 以下命令安装 NGINX
  6. apk update
  7. apk upgrade
  8. apk add --update nginx
  9. rm -rf /var/cache/apk/*
  10. mkdir -p /tmp/nginx/
  11. exit
  12. # 将我们停止的容器保存为镜像
  13. docker container commit <container_name> <REPOSITORY>:<TAG>
  14. docker container commit alpine-test local:broken-container
  15. # 保存图像文件
  16. docker image save -o <name_of_file.tar> <REPOSITORY>:<TAG>
  17. docker image save -o broken-container.tar local:broken-container
  18. tar -xvf broken-container.tar

构建docker镜像 - 图1

使用 Dockerfile,您可以准确地看到创建映像所执行的操作,但使用该种方法,对该镜像的可见性为零。

另一个原因是你很难建立一套好的默认值。例如,如果您要以这种方式构建图像,那么您将无法真正利用ENTRYPOINT和CMD等功能,甚至无法利用最基本的指令,例如EXPOSE. 相反,用户必须在运行docker container run命令时定义所需的一切。

这是docker早期的一种做法

  1. # 编写Dockerfile scratch为docker hub中默认创建的空文件
  2. # https://www.alpinelinux.org/downloads/
  3. FROM scratch
  4. ADD files/alpine-minirootfs-3.11.3-x86_64.tar.gz /
  5. CMD [“/bin/sh”]
  6. # 建立镜像
  7. docker image build --tag local:fromscratch .
  8. # 运行镜像
  9. docker container run -it --name alpine-test local:fromscratch /bin/sh

使用 ENV

  1. ENV <key> <value>
  2. ENV username admin
  3. # 也可以在键和值之间放置一个等号
  4. ENV <key>=<value>
  5. ENV username=admin
  6. # 每行只能设置一个ENV 同一行设置多个环境变量
  7. ENV username=admin database=wordpress tableprefix=wp
  8. # 查看在镜像上设置的ENV
  9. docker image inspect <IMAGE_ID>

使用 Alpine Linux,我们将执行以下操作:

设置一个 ENV 来定义我们想要安装的 PHP 版本。

安装 Apache2 和我们选择的 PHP 版本。

设置镜像以便 Apache2 启动时不会出现问题。

删除默认index.html文件并添加一个index.php显示phpinfo命令结果的文件。

暴露容器上的端口 80。

设置 Apache 使其成为默认进程

  1. ROM alpine:3.8
  2. LABEL maintainer=”Russ McKendrick <russ@mckendrick.io>”
  3. LABEL description=”This example Dockerfile installs Apache & PHP.”
  4. ENV PHPVERSION 7
  5. RUN apk add --update apache2 php${PHPVERSION}-apache2 php${PHPVERSION} && \
  6. rm -rf /var/cache/apk/* && \
  7. mkdir /run/apache2/ && \
  8. rm -rf /var/www/localhost/htdocs/index.html && \
  9. echo “<?php phpinfo(); ?>” > /var/www/localhost/htdocs/index.php && \
  10. chmod 755 /var/www/localhost/htdocs/index.php
  11. EXPOSE 80/tcp
  12. ENTRYPOINT [“httpd”]
  13. CMD [“-D”, “FOREGROUND”]
  1. docker build --tag local/apache-php:7 .
  2. docker container run -d -p 8080:80 --name apache-php7 local/apache-php:7
  3. # 更改Dockerfile中的ENV PHPVERSION 5
  4. docker image build --tag local/apache-php:5 .
  5. docker container run -d -p 9090:80 --name apache-php5 local/apache-php:5
  6. docker image ls

构建一个更复杂的容器

  1. # installs and configures Consul by HashiCorp
  2. # Dockerfile
  3. FROM alpine:latest
  4. LABEL maintainer=”Russ McKendrick <russ@mckendrick.io>”
  5. LABEL description=”An image with the latest version on Consul.”
  6. ENV CONSUL_VERSION 1.7.1
  7. ENV CONSUL_SHA256 09f3583c6cd7b1f748c0c012ce9b3d96de95a6c0d2334327b74f7d72b1fa5054
  8. RUN apk add --update ca-certificates wget && \
  9. wget -O consul.zip https://releases.hashicorp.com/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_amd64.zip && \
  10. echo $CONSUL_SHA256 *consul.zip | sha256sum -c - && \
  11. unzip consul.zip && \
  12. mv consul /bin/ && \
  13. rm -rf consul.zip && \
  14. rm -rf /tmp/* /var/cache/apk/*
  15. EXPOSE 8300 8301 8301/udp 8302 8302/udp 8400 8500 8600 8600/udp
  16. VOLUME [ “/data” ]
  17. ENTRYPOINT [ “/bin/consul” ]
  18. CMD [ “agent”, “-data-dir”, “/data”, “-server”, “-bootstrap-expect”, “1”, “-client=0.0.0.0”]

多阶段(multi-stage)构建容器

下载build环境容器镜像并启动build容器。

将源代码复制到build容器中。

在build容器上编译源代码。

将编译后的二进制文件复制到build容器外。

移除build容器。

使用预先编写的 Dockerfile 构建镜像并将二进制文件复制到其中。

  1. # 之前都是通过包管理器(例如Alpine Linux的APK)直接将二进制文件添加到我们的镜像中
  2. # 即下载预编译的二进制文件
  3. # 将官方go容器重命名为builder
  4. FROM golang:latest as builder
  5. WORKDIR /go-http-hello-world/
  6. # go代理
  7. # RUN go env -w GO111MODULE=on
  8. # RUN go env -w GOPROXY=https://goproxy.cn,direct
  9. RUN go get -d -v golang.org/x/net/html
  10. # RUN go env -w GO111MODULE=off
  11. ADD https://raw.githubusercontent.com/geetarista/go-http-hello-world/master/hello_world/hello_world.go ./hello_world.go
  12. RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
  13. FROM scratch
  14. COPY --from=builder /go-http-hello-world/app .
  15. CMD [“./app”]
  16. docker image build --tag local:go-hello-world .
  17. docker container run -d -p 8000:80 --name go-hello-world local:go-hello-world
  18. # docker container stop go-hello-world && docker container rm go-hello-world
  19. # docker container prune 停止所有容器

nvidia Docker

https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#installation-guide