Docker使用记录

news2024/10/6 10:37:15

文章目录

  • Docker基本使用
    • Docker配置
      • 查看状态
      • 卸载
      • 安装
        • 使用 apt 存储库安装
        • 在 Ubuntu 上安装 Docker 桌面(非必要)
    • Docker实例
      • 使用现有的镜像
        • 查找镜像
        • 拖取镜像
        • 列出镜像列表
        • 更新镜像
        • 导出镜像
        • 删除镜像
        • 导入镜像
        • 清理镜像
        • 查看容器
        • 导出容器
        • 导入容器-以镜像的方式
        • 创建容器
        • 重启容器
        • 进入容器
        • 停止&关闭容器
        • 删除容器
      • Dockerfile
      • Dockerfile切换国内源
      • buildx

Docker基本使用

参考文章:https://www.runoob.com/docker/docker-tutorial.html
参考文章:https://docs.docker.com/desktop/?_gl=11oj7pdo_gaMTY0NDQxMjgwNC4xNjgyMDY2MjM5_ga_XJWPQMJYHQ*MTY4NTQ5NjQyNi4yLjEuMTY4NTQ5NzA1Ny4zNi4wLjA.
参考文章:https://docs.docker.com/engine/
参考文章:https://cloud.tencent.com/developer/article/1885678
参考文章:https://docs.docker.com/get-started/overview/

Docker 使用客户端-服务器架构。Docker客户端与 Docker守护进程对话,后者负责构建、运行和分发 Docker 容器的繁重工作。Docker 客户端和守护进程可以 在同一系统上运行,或者您可以将 Docker 客户端连接到远程 Docker 守护进程。Docker 客户端和守护进程使用 REST API 通过 UNIX 套接字或网络接口进行通信。另一个 Docker 客户端是 Docker Compose,它允许您处理由一组容器组成的应用程序。
在这里插入图片描述
Docker中的镜像分层
Docker 支持通过扩展现有镜像,创建新的镜像。实际上,Docker Hub 中 99% 的镜像都是通过在 base 镜像中安装和配置需要的软件构建出来的。
在这里插入图片描述
从上图可以看到,新镜像是从 base 镜像一层一层叠加生成的。每安装一个软件,就在现有镜像的基础上增加一层。

Docker 镜像为什么分层
镜像分层最大的一个好处就是共享资源。
比如说有多个镜像都从相同的 base 镜像构建而来,那么 Docker Host 只需在磁盘上保存一份 base 镜像;同时内存中也只需加载一份 base 镜像,就可以为所有容器服务了。而且镜像的每一层都可以被共享。
如果多个容器共享一份基础镜像,当某个容器修改了基础镜像的内容,比如 /etc 下的文件,这时其他容器的 /etc 是不会被修改的,修改只会被限制在单个容器内。这就是容器 Copy-on-Write 特性。

可写的容器层
当容器启动时,一个新的可写层被加载到镜像的顶部。这一层通常被称作“容器层”,“容器层”之下的都叫“镜像层”。
在这里插入图片描述
所有对容器的改动 - 无论添加、删除、还是修改文件都只会发生在容器层中。只有容器层是可写的,容器层下面的所有镜像层都是只读的。

容器层的细节说明
镜像层数量可能会很多,所有镜像层会联合在一起组成一个统一的文件系统。如果不同层中有一个相同路径的文件,比如 /a,上层的 /a 会覆盖下层的 /a,也就是说用户只能访问到上层中的文件 /a。在容器层中,用户看到的是一个叠加之后的文件系统。

文件操作说明
添加文件在容器中创建文件时,新文件被添加到容器层中。
读取文件在容器中读取某个文件时,Docker 会从上往下依次在各镜像层中查找此文件。一旦找到,立即将其复制到容器层,然后打开并读入内存。
修改文件在容器中修改已存在的文件时,Docker 会从上往下依次在各镜像层中查找此文件。一旦找到,立即将其复制到容器层,然后修改之。
删除文件在容器中删除文件时,Docker 也是从上往下依次在镜像层中查找此文件。找到后,会在容器层中记录下此删除操作。(只是记录删除操作)

只有当需要修改时才复制一份数据,这种特性被称作 Copy-on-Write。可见,容器层保存的是镜像变化的部分,不会对镜像本身进行任何修改。

这样就解释了我们前面提出的问题:容器层记录对镜像的修改,所有镜像层都是只读的,不会被容器修改,所以镜像可以被多个容器共享。

Docker配置

系统版本:Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-146-generic x86_64)

查看状态

首先,可以先检查一下系统是否已经安装过docker,使用下面指令查看docker版本号。

root@qhdata-dev:~# docker -v
Docker version 20.10.17, build 100c701

root@qhdata-dev:~# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete 
Digest: sha256:fc6cf906cbfa013e80938cdf0bb199fbdbb86d6e3e013783e5a766f50f5dbce0
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

卸载

使用下列语句卸载已安装的docker

 sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

主机上的图像、容器、卷或自定义配置文件不会自动删除。删除所有镜像、容器和卷:

sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd

您必须手动删除任何已编辑的配置文件。

安装

  • Docker Engine 与Docker Desktop for Linux捆绑在一起 。这是最简单快捷的入门方法。
  • 从Docker 的apt存储库设置和安装 Docker 引擎 。
  • 手动安装并手动管理升级。
  • 使用方便的脚本。仅推荐用于测试和开发环境。

使用 apt 存储库安装

设置存储库
更新apt包索引并安装包以允许apt通过 HTTPS 使用存储库:

sudo apt-get update
sudo apt-get install ca-certificates curl gnupg

添加 Docker 的官方 GPG 密钥:

 sudo install -m 0755 -d /etc/apt/keyrings
 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
 sudo chmod a+r /etc/apt/keyrings/docker.gpg

使用以下命令设置存储库:

echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

安装 Docker 引擎
更新apt包索引:

 sudo apt-get update

要安装特定版本的 Docker Engine,首先在存储库中列出可用版本:

apt-cache madison docker-ce | awk '{ print $3 }'

选择所需版本并安装:

 VERSION_STRING=5:24.0.0-1~ubuntu.20.04~focal
 sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin

或者直接安装最新版本

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

通过运行hello-world映像验证 Docker 引擎安装是否成功

root@qhdata-dev:~# docker -v
Docker version 24.0.2, build cb74dfc

sudo docker run hello-world

在 Ubuntu 上安装 Docker 桌面(非必要)

参考文章:https://docs.docker.com/desktop/install/ubuntu/
先决条件
要成功安装 Docker Desktop,您必须:

  • 满足系统要求
  • 拥有 64 位版本的 Ubuntu Jammy Jellyfish 22.04 (LTS) 或 Ubuntu Impish Indri 21.10。x86_64Docker Desktop 在(或)架构上受支持amd64。
  • 对于非 Gnome 桌面环境,gnome-terminal必须安装:
sudo apt install gnome-terminal

安装 Docker 桌面
1.设置Docker 的包存储库。
2.下载最新的DEB 包。
3.使用 apt 安装软件包,如下所示:

sudo apt-get update
sudo apt-get install ./docker-desktop-<version>-<arch>.deb

安装后脚本:

  • 设置 Docker Desktop 二进制文件的功能以映射特权端口并设置资源限制。
  • 将 Kubernetes 的 DNS 名称添加到/etc/hosts.
  • 创建一个从/usr/bin/docker到/usr/local/bin/com.docker.cli的链接

启动 Docker 桌面

Docker实例

参考文章:https://www.quanxiaoha.com/docker/docker-look-image.html
参考文章:https://cloud.tencent.com/developer/article/1885678
参考文章:https://www.w3cschool.cn/docker/docker-image-usage.html
docker_hub地址:https://hub.docker.com/search?q=
docker构建一个基础镜像大概流程:

更新镜像代码
Dockerfile 文件
docker构建基础镜像
创建镜像
导入镜像
使用现有的镜像
更新镜像
创建容器
保存容器成为新的镜像
保存到hub上
保存到本地
构建新的镜像
docker build

使用现有的镜像

查找镜像

可以登陆Docker Hub来查找我们需要的的镜像。
在这里插入图片描述
在这里插入图片描述

拖取镜像

使用命令 docker pull 来下载镜像

root@qhdata-dev:/home/qhdata/docker# docker pull ubuntu:20.04
20.04: Pulling from library/ubuntu
ca1778b69356: Pull complete 
Digest: sha256:db8bf6f4fb351aa7a26e27ba2686cf35a6a409f65603e59d4c203e58387dc6b3
Status: Downloaded newer image for ubuntu:20.04
docker.io/library/ubuntu:20.04
root@qhdata-dev:/home/qhdata/docker# 

列出镜像列表

使用 docker images 来列出本地主机上的镜像。

root@qhdata-dev:/home/qhdata/docker# docker images
REPOSITORY              TAG           IMAGE ID       CREATED         SIZE
hello-world             latest        9c7a54a9a43c   3 weeks ago     13.3kB
ubuntu                  20.04         88bd68917189   6 weeks ago     72.8MB
...
alpine                  latest        e66264b98777   12 months ago   5.53MB
root@qhdata-dev:/home/qhdata/docker# 

更新镜像

更新镜像之前,我们需要使用镜像来创建一个容器。

root@qhdata-dev:/home/qhdata/docker# docker run -t -i ubuntu:20.04 /bin/bash
root@7ec56dec1d8e:/# 

在运行的容器内使用 apt-get update 命令进行更新。
在完成操作之后,输入 exit命令来退出这个容器。

root@7ec56dec1d8e:/# apt-get update
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]   
.....                                                                                                                                                                             
Get:18 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [2748 kB]                                                                                                                                                                                        
Fetched 26.8 MB in 15s (1767 kB/s)                                                                                                                                                                                                                                           
Reading package lists... Done
root@7ec56dec1d8e:/# exit
exit
root@qhdata-dev:/home/qhdata/docker# 

此时ID为7ec56dec1d8e的容器,是按我们的需求更改的容器。我们可以通过命令docker commit来提交容器副本。

root@qhdata-dev:/home/qhdata/docker# docker commit -m="has update" -a="qhdata" 7ec56dec1d8e qhdata/ubuntu:20.04
sha256:dd4c75bc58d43b6ae59f9afc5c528117ac317d936cfd3219fe1ade1e902c3965

各个参数说明:

  • -m:提交的描述信息
  • -a:指定镜像作者
  • 7ec56dec1d8e:容器ID
  • qhdata/ubuntu:20.04:指定要创建的目标镜像名

我们可以使用docker images命令来查看我们的新镜像 qhdata/ubuntu:20.04

root@qhdata-dev:/home/qhdata/docker# docker images
REPOSITORY              TAG           IMAGE ID       CREATED         SIZE
qhdata/ubuntu           20.04         dd4c75bc58d4   2 minutes ago   116MB
hello-world             latest        9c7a54a9a43c   3 weeks ago     13.3kB
ubuntu                  20.04         88bd68917189   6 weeks ago     72.8MB
...
alpine                  latest        e66264b98777   12 months ago   5.53MB
root@qhdata-dev:/home/qhdata/docker# 

导出镜像

参考文章:https://docs.docker.com/engine/reference/commandline/save/

root@qhdata-dev:/home/qhdata/docker# docker save -o qhdata_ubuntu_20_04.tar qhdata/ubuntu:20.04
root@qhdata-dev:/home/qhdata/docker# ls -r
qhdata_ubuntu_20_04.tar
root@qhdata-dev:/home/qhdata/docker# 

参数说明:

  • --output,-o:写入的文件,而不是 STDOUT

删除镜像

参考文章:https://docs.docker.com/engine/reference/commandline/rmi/
参考文章:https://docs.docker.com/engine/reference/commandline/image_rm/

docker rmi [image]
docker rmi [image id]
docker image rm [image]

参数说明:

  • --force , -f:强制删除图像
  • --no-prune:不要删除未标记的父级

eg:

root@qhdata-dev:/home/qhdata/docker# docker rmi qhdata/ubuntu:20.04
Untagged: qhdata/ubuntu:20.04
Deleted: sha256:dd4c75bc58d43b6ae59f9afc5c528117ac317d936cfd3219fe1ade1e902c3965
Deleted: sha256:307a3b9484906a84d1b16735fdb6c967692fd0b065985126f54fbeb16eefdf20

root@qhdata-dev:/home/qhdata/docker# docker images
REPOSITORY              TAG           IMAGE ID       CREATED         SIZE
hello-world             latest        9c7a54a9a43c   3 weeks ago     13.3kB
ubuntu                  20.04         88bd68917189   6 weeks ago     72.8MB
...
alpine                  latest        e66264b98777   12 months ago   5.53MB
root@qhdata-dev:/home/qhdata/docker# 

导入镜像

参考文章:https://docs.docker.com/engine/reference/commandline/load/
参考文章:https://docs.docker.com/engine/reference/commandline/image_load/
从 STDIN 加载图像

docker load < qhdata_ubuntu_20_04.tar
docker image load < qhdata_ubuntu_20_04.tar

从文件加载图像 (–input)

docker load -i qhdata_ubuntu_20_04.tar
docker image load -i qhdata_ubuntu_20_04.tar

参数说明:

  • --input,-i:从 tar 归档文件中读取,而不是从 STDIN 中读取
  • --quiet,-q抑制负载输出

eg:

root@qhdata-dev:/home/qhdata/docker# docker load -i qhdata_ubuntu_20_04.tar
f6ed57753c5f: Loading layer [==================================================>]  43.33MB/43.33MB
Loaded image: qhdata/ubuntu:20.04

root@qhdata-dev:/home/qhdata/docker# docker images
REPOSITORY              TAG           IMAGE ID       CREATED         SIZE
qhdata/ubuntu           20.04         dd4c75bc58d4   2 hours ago     116MB
hello-world             latest        9c7a54a9a43c   3 weeks ago     13.3kB
ubuntu                  20.04         88bd68917189   6 weeks ago     72.8MB
...
alpine                  latest        e66264b98777   12 months ago   5.53MB
root@qhdata-dev:/home/qhdata/docker# 

清理镜像

参考文章:https://docs.docker.com/engine/reference/commandline/image_prune/

root@qhdata-dev:/home/qhdata/docker# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
root@qhdata-dev:/home/qhdata/docker# 

参数说明:

  • --all,-a:删除所有未使用的图像,而不仅仅是悬挂的图像
  • --filter:提供过滤值(例如until=<timestamp>
  • --force,-f:不提示确认

过滤(--filter
过滤标志(–filter)格式为“key=value”。如果有多个过滤器,则传递多个标志(例如,–filter “foo=bar” --filter “bif=baz”)
目前支持的过滤器有:

  • until ( <timestamp>) - 仅删除在给定时间戳之前创建的图像
  • label ( label=<key>, label=<key>=<value>, label!=<key>, or label!=<key>=<value>) - 仅删除带有(或不带,如果label!=...使用)指定标签的图像。

查看容器

docker ps: 查看正在运行中的容器;
docker ps -a: 查看所有容器,包括运行中的、已经停止运行的容器。
docker container ls -a: 查看所有容器,包括运行中的、已经停止运行的容器。

root@qhdata-dev:/home/qhdata/docker# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS       PORTS                                       NAMES
7b651a8dfc5c   registry:latest   "/entrypoint.sh /etc…"   11 months ago   Up 7 hours   0.0.0.0:5001->5000/tcp, :::5001->5000/tcp   registry

root@qhdata-dev:/home/qhdata/docker# docker ps -a
CONTAINER ID   IMAGE                          COMMAND                  CREATED         STATUS                      PORTS                                                                            NAMES
7ec56dec1d8e   ubuntu:20.04                   "/bin/bash"              3 minutes ago   Exited (0) 2 minutes ago                                                                                     unruffled_ganguly
dea79bf486d4   hello-world                    "/hello"                 8 hours ago     Exited (0) 8 hours ago                                                                                       laughing_joliot
a67c7b72196a   owasp/modsecurity-crs:nginx    "/docker-entrypoint.…"   9 months ago    Exited (255) 5 months ago   0.0.0.0:8080->80/tcp, :::8080->80/tcp, 0.0.0.0:8443->443/tcp, :::8443->443/tcp   wafdemo-waf-1
d2945582db44   bkimminich/juice-shop:latest   "/nodejs/bin/node /j…"   9 months ago    Exited (255) 5 months ago   0.0.0.0:1003->3000/tcp, :::1003->3000/tcp                                        wafdemo-webapp-1
c7f605ab3ec9   nginx:latest                   "/docker-entrypoint.…"   9 months ago    Exited (255) 5 months ago   0.0.0.0:8081->80/tcp, :::8081->80/tcp                                            nginx-webapp-1
7b651a8dfc5c   registry:latest                "/entrypoint.sh /etc…"   11 months ago   Up 7 hours                  0.0.0.0:5001->5000/tcp, :::5001->5000/tcp                                        registry
root@qhdata-dev:/home/qhdata/docker# 

返回字段说明:

  • CONTAINER ID: 容器 ID;
  • IMAGE: 创建容器时使用的镜像;
  • COMMAND: 容器最后运行的命令;
  • CREATED: 容器创建时间;
  • STATUS: 容器状态;
  • PORTS: 端口信息;
  • NAMES: 容器名:和容器 ID 一样,可以标识容器的唯一性,同一台宿主机上不允许有同名容器存在,否则会冲突;

导出容器

参考文章:https://docs.docker.com/engine/reference/commandline/export/

docker export 7ec56dec1d8e > qhdata_ubuntu_20_04.tar
docker export --output="qhdata_ubuntu_20_04.tar" 7ec56dec1d8e

参数说明:

  • --output , -o:写入的文件,而不是 STDOUT

eg:

root@qhdata-dev:/home/qhdata/docker# docker export --output="qhdata_ubuntu_20_04.tar" 7ec56dec1d8e
root@qhdata-dev:/home/qhdata/docker# ls -r
qhdata_ubuntu_20_04.tar
root@qhdata-dev:/home/qhdata/docker# 

导入容器-以镜像的方式

参考文章:https://docs.docker.com/engine/reference/commandline/import/

// 从远程位置导入
docker import https://example.com/exampleimage.tgz  // 这将创建一个新的未标记图像。
// 从本地文件导入
cat exampleimage.tgz | docker import - exampleimagelocal:new  // 通过管道导入到 docker 和STDIN.
cat exampleimage.tgz | docker import --message "New image imported from tarball" - exampleimagelocal:new  // 使用提交消息导入。
docker import /path/to/exampleimage.tgz  // 从本地存档导入到 docker。
// 从本地目录导入
sudo tar -c . | docker import - exampleimagedir
// 使用新配置从本地目录导入
sudo tar -c . | docker import --change "ENV DEBUG=true" - exampleimagedir

参数说明:

  • --change,-c:将 Dockerfile 指令应用于创建的图像
  • --message,-m:为导入的图像设置提交信息
  • --platform:如果服务器支持多平台,则设置平台

eg:

root@qhdata-dev:/home/qhdata/docker# cat qhdata_ubuntu_20_04.tar | docker import - qhdata/ubuntu:20.04
sha256:db81f58964d10ecf857bbb61777ebe3aa1318eaa71ef3ba58fb15ac87d13aed7

root@qhdata-dev:/home/qhdata/docker# docker images
REPOSITORY              TAG           IMAGE ID       CREATED          SIZE
qhdata/ubuntu           20.04         db81f58964d1   14 seconds ago   116MB
hello-world             latest        9c7a54a9a43c   3 weeks ago      13.3kB
...
alpine                  latest        e66264b98777   12 months ago    5.53MB
root@qhdata-dev:/home/qhdata/docker# 

创建容器

参考文章:https://docs.docker.com/engine/reference/commandline/run/

root@qhdata-dev:/home/qhdata/docker# docker  run -v ./dm7:/dm7 -w /dm7 -i -t qhdata/ubuntu:20.04 /bin/bash
root@1867b8bbbbc8:/dm7# 

root@8b5dd52725e5:/dm7# ls -r
include  drivers  bin
root@8b5dd52725e5:/dm7# 

root@1867b8bbbbc8:/dm7# cd /
root@1867b8bbbbc8:/# ls -r
var  usr  tmp  sys  srv  sbin  run  root  proc  opt  mnt  media  libx32  lib64  lib32  lib  home  etc  dm7  dev  boot  bin
root@1867b8bbbbc8:/# exit

参数说明(部分):

  • --volume,-v:绑定挂载卷,从 Docker 引擎版本 23 开始,您可以在主机上使用相对路径。
  • --workdir,-w:容器内的工作目录
  • --tty,-t:分配伪 TTY
  • --interactive,-i:即使未附加,也要保持 STDIN 打开
  • --env,-e:设置环境变量
  • --platform: 如果服务器支持多平台,则设置平台
  • /bin/bash:在容器中创建交互式shell。
  • /bin/echo:echo后面接需要使用的命令。

重启容器

参考文章:https://docs.docker.com/engine/reference/commandline/restart/
参考文章:https://docs.docker.com/engine/reference/commandline/container_restart/

root@qhdata-dev:/home/qhdata/docker# docker restart 8b5dd52725e5
8b5dd52725e5
root@qhdata-dev:/home/qhdata/docker# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS          PORTS                                       NAMES
8b5dd52725e5   qhdata/ubuntu:20.04   "/bin/bash"              24 hours ago    Up 10 seconds                                               gracious_sanderson
7b651a8dfc5c   registry:latest       "/entrypoint.sh /etc…"   11 months ago   Up 2 days       0.0.0.0:5001->5000/tcp, :::5001->5000/tcp   registry
root@qhdata-dev:/home/qhdata/docker# 

参数说明:

  • --signal,-s: 发送到容器的信号
  • --time,-t: 杀死容器前等待的秒数

进入容器

参考文章:https://docs.docker.com/engine/reference/commandline/exec/

docker exec -it [container ID or NAMES] 
docker exec -it mycontainer sh  // 在容器上执行交互式shell
docker exec -it mycontainer pwd  // 在创建容器时运行命令

参数说明:

  • --detach,-d: 分离模式:在后台运行命令
  • --detach-keys: 覆盖用于分离容器的键序列
  • --env,-e: 设置环境变量
  • --env-file: 读入环境变量文件
  • --interactive,-i: 即使未附加,也要保持 STDIN 打开
  • --privileged: 赋予命令扩展权限
  • --tty,-t: 分配伪 TTY
  • --user,-u: 用户名或 UID(格式<name|uid>[:<group|gid>]:)
  • --workdir,-w: 容器内的工作目录

eg:

root@qhdata-dev:/home/qhdata/docker# docker exec -it 8b5dd52725e5 sh
# ls -r
include  drivers  bin
# cd /
# ls -r
var  usr  tmp  sys  srv  sbin  run  root  proc	opt  mnt  media  libx32  lib64	lib32  lib  home  etc  dm7  dev  boot  bin
# exit
root@qhdata-dev:/home/qhdata/docker# 

如果进入一个未启动的容器,会报错误。

root@qhdata-dev:/home/qhdata/docker# docker exec -it 1867b8bbbbc8 sh
Error response from daemon: Container 1867b8bbbbc83c348ea1f898d6cfc6dd7208d36d3b3d926a1abcc8f94eb8fa7b is not running

停止&关闭容器

参考文章:https://docs.docker.com/engine/reference/commandline/stop/
参考文章:https://docs.docker.com/engine/reference/commandline/container_stop/
参考文章:https://docs.docker.com/engine/reference/commandline/kill/
参考文章:https://docs.docker.com/engine/reference/commandline/container_kill/

// 优雅模式
docker container stop [container ID or NAMES]
# 简写模式(可省略关键字 container )
docker stop [container ID or NAMES]
// 强制模式
docker container kill [container ID or NAMES]
# 简写模式(可省略关键字 container )
docker kill [container ID or NAMES]

参数说明:

  • --signal,-s: 发送到容器的信号
  • --time,-t: 杀死容器前等待的秒数

eg:

root@qhdata-dev:/home/qhdata/docker# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS      PORTS                                       NAMES
8b5dd52725e5   qhdata/ubuntu:20.04   "/bin/bash"              3 days ago      Up 2 days                                               gracious_sanderson
7b651a8dfc5c   registry:latest       "/entrypoint.sh /etc…"   11 months ago   Up 5 days   0.0.0.0:5001->5000/tcp, :::5001->5000/tcp   registry
root@qhdata-dev:/home/qhdata/docker# docker container stop 8b5dd52725e5
8b5dd52725e5
root@qhdata-dev:/home/qhdata/docker# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED         STATUS      PORTS                                       NAMES
7b651a8dfc5c   registry:latest   "/entrypoint.sh /etc…"   11 months ago   Up 5 days   0.0.0.0:5001->5000/tcp, :::5001->5000/tcp   registry
root@qhdata-dev:/home/qhdata/docker# 

删除容器

参考文章:https://docs.docker.com/engine/reference/commandline/rm/
参考文章:https://docs.docker.com/engine/reference/commandline/container_rm/

docker container rm [container ID or NAMES]
# 简写模式(可省略关键字 container )
docker rm [container ID or NAMES]

参数说明:

  • --force,-f: 强制删除正在运行的容器(使用 SIGKILL)
  • --link,-l: 删除指定链接
  • --volumes,-v: 删除与容器关联的匿名卷

eg:

root@qhdata-dev:/home/qhdata/docker# docker rm 8b5dd52725e5
8b5dd52725e5
root@qhdata-dev:/home/qhdata/docker# docker ps -a
CONTAINER ID   IMAGE                          COMMAND                  CREATED         STATUS                      PORTS                                                                            NAMES
1867b8bbbbc8   qhdata/ubuntu:20.04            "/bin/bash"              3 days ago      Exited (0) 3 days ago                                                                                        intelligent_feynman
7ec56dec1d8e   ubuntu:20.04                   "/bin/bash"              4 days ago      Exited (0) 4 days ago                                                                                        unruffled_ganguly
dea79bf486d4   hello-world                    "/hello"                 5 days ago      Exited (0) 5 days ago                                                                                        laughing_joliot
...
7b651a8dfc5c   registry:latest                "/entrypoint.sh /etc…"   11 months ago   Up 5 days                   0.0.0.0:5001->5000/tcp, :::5001->5000/tcp                                        registry
root@qhdata-dev:/home/qhdata/docker# 

Dockerfile

参考文章:https://docs.docker.com/engine/reference/builder/
参考文章:https://www.quanxiaoha.com/docker/dockerfile-build-image.html
Dockerfile的构建

FROM --platform=linux/arm64 python:3.8.10
  
ADD . .

RUN apt-get update -y && \
apt-get install python-dev -y && \
apt-get install gcc* -y

ENV DM_HOME=/dm7 LD_LIBRARY_PATH=/dm7/bin:$LD_LIBRARY_PATH PATH=$PATH:$HOME/bin:/dm7/bin

RUN cd ./dm7/drivers/python/dmPython/ && python setup.py install  # 安装dmPython

RUN cd ./dm7/drivers/python/sqlalchemy1.4.6/ && python setup.py install  # sqlalchemy1.4.6

RUN pip install --upgrade scipy==1.7.3 -i https://pypi.tuna.tsinghua.edu.cn/simple && \
pip install --upgrade numpy==1.22.4 -i https://pypi.tuna.tsinghua.edu.cn/simple && \
pip install --upgrade pandas==1.2.4 -i https://pypi.tuna.tsinghua.edu.cn/simple && \
pip install --upgrade xlrd==1.2.0 -i https://pypi.tuna.tsinghua.edu.cn/simple && \
pip install --upgrade xlwt==0.7.2 -i https://pypi.tuna.tsinghua.edu.cn/simple && \
pip install --upgrade python_dateutil==2.8.2 -i https://pypi.tuna.tsinghua.edu.cn/simple && \
pip install --upgrade pymysql==0.9.3 -i https://pypi.tuna.tsinghua.edu.cn/simple && \
pip install --upgrade SQLAlchemy==1.4.18 -i https://pypi.tuna.tsinghua.edu.cn/simple && \
pip install --upgrade openpyxl==3.0.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

执行生成

root@qhdata-dev:/home/qhdata/docker# docker build -t dm_base/qhdata .

但是执行失败,报下面的错误。

ERROR: failed to solve: process "/bin/sh -c apt-get update -y" did not complete successfully: exit code: 1

估计是新版问题。我切换回5:20.10.23~3-0~ubuntu-focal就不再提示上述错误。

Sending build context to Docker daemon  3.164GB
Step 1/8 : FROM --platform=linux/arm64 python:3.8.10
 ---> a369814a9797
Step 2/8 : ADD . .
 ---> 8ce022ad8be9
Step 3/8 : RUN apt-get update -y
 ---> Running in 76f670c26669
Get:1 http://security.debian.org/debian-security buster/updates InRelease [34.8 kB]
Get:2 http://deb.debian.org/debian buster InRelease [122 kB]
Get:3 http://deb.debian.org/debian buster-updates InRelease [56.6 kB]
Get:4 http://security.debian.org/debian-security buster/updates/main amd64 Packages [508 kB]
Get:5 http://deb.debian.org/debian buster/main amd64 Packages [7909 kB]
Get:6 http://deb.debian.org/debian buster-updates/main amd64 Packages [8788 B]
Fetched 8640 kB in 15s (588 kB/s)
Reading package lists...
Removing intermediate container 76f670c26669
 ---> 7b2a2534e290
Step 4/8 : RUN apt-get install gcc -y
 ---> Running in 0befdb33c623
Reading package lists...
Building dependency tree...
Reading state information...
...
Removing intermediate container da5f1a91fce3
 ---> 0d450fba1928
Successfully built 0d450fba1928
Successfully tagged dm_base/qhdata:latest
root@qhdata-dev:/home/qhdata/docker# docker images
REPOSITORY              TAG           IMAGE ID       CREATED          SIZE
dm_base/qhdata          latest        0d450fba1928   21 seconds ago   4.42GB
<none>                  <none>        926744ce4933   9 minutes ago    4.08GB
...
python                  3.8.10        a369814a9797   23 months ago    883MB
root@qhdata-dev:/home/qhdata/docker#  docker save -o dm_base.tar dm_base/qhdata:latest

但是如果是这样的话,在跨平台(linux/amd64 -> linux/arm64)还是会出现问题。提示如下

WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested                               
exec /bin/bash: exec format error 

Dockerfile切换国内源

参考文章:https://blog.csdn.net/xiaowu_wu/article/details/124355694
参考文章:https://mirrors.tuna.tsinghua.edu.cn/help/debian/
参考文章:https://blog.csdn.net/karmacode/article/details/104902656
参考文章:https://blog.csdn.net/weixin_45067618/article/details/122234387
参考文章:https://stackoverflow.com/questions/70789307/how-to-fix-the-following-signatures-couldnt-be-verified-because-the-public-key
参考文章:https://blog.csdn.net/seaofbits/article/details/123647256
在切换前,需要进入你目标镜像中查看系统版本(cat /etc/os-release)。

root@qhdata-dev:/home/qhdata/docker# docker run -t -i python:3.9 /bin/bash
WARNING: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64) and no specific platform was requested
root@016143a4d2f4:/# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
root@016143a4d2f4:/# 

得到目标镜像实用的PRETTY_NAME。然后到清华大学开源软件镜像站查找对应实用版本镜像。
在这里插入图片描述
在这里插入图片描述
将里面的内容复制到dockerfile里面。

RUN mv /etc/apt/sources.list /etc/apt/sources_backup.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse                      " >> /etc/apt/sources.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse              " >> /etc/apt/sources.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse            " >> /etc/apt/sources.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse             " >> /etc/apt/sources.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free                                 " >> /etc/apt/sources.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free                         " >> /etc/apt/sources.list && \
echo "deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free                       " >> /etc/apt/sources.list && \
echo "deb https://security.debian.org/debian-security bullseye-security main contrib non-free                         " >> /etc/apt/sources.list 

RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32 && \
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 871920D1991BC93C

这时候运行的结果就如图瞎:
在这里插入图片描述

错误1:E: Failed to fetch https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/focal-updates/main/binary-arm64/Packages 404 Not Found [IP: 101.6.15.130 443]
这是由于框架是用arm的,但是网上给的教程都是amd的。所以只需要将镜像网址中的ubuntu改成ubuntu-ports即可。
错误2:The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 3B4FE6ACC0B21F32 NO_PUBKEY 871920D1991BC93C
这是镜像的key不存在,只要在dockerfile中将该key添加即可
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
错误3:Some packages could not be installed. This may mean that you have requested an impossible situation or if you are using the unstable distribution that some required packages have not yet been created or been moved out of Incoming.
这个错误无解,就是镜像缺少对应的包。

buildx

参考文章:https://docs.docker.com/build/building/multi-platform/
参考文章:https://docs.docker.com/build/building/multi-stage/
参考文章:https://docs.docker.com/engine/reference/commandline/buildx_build/
参考文章:https://blog.bwcxtech.com/posts/43dd6afb/
参考文章:https://waynerv.com/posts/building-multi-architecture-images-with-docker-buildx/
Docker 默认会使用不支持多 CPU 架构的构建器,我们需要手动切换。
查看现有的构建器

root@qhdata-dev:/home/qhdata/docker# docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS  BUILDKIT PLATFORMS
default * docker                           
  default default         running 20.10.23 linux/amd64, linux/386
root@qhdata-dev:/home/qhdata/docker# 

先创建一个新的构建器:

docker buildx create --use --name mybuilder
docker buildx use mybuilder

启动构建器:

docker buildx inspect mybuilder --bootstrap

如果发现还是像我一样不支持arm架构,则使用下面方法来安装支持。
Buildx 和 Dockerfiles 支持的三种不同策略构建多平台镜像:

  • 在内核中使用 QEMU 仿真支持
  • 使用相同的构建器实例在多个本机节点上构建
  • 使用 Dockerfile 中的阶段交叉编译到不同的体系结构

如果您的节点已经支持 QEMU(例如,如果您正在使用 Docker Desktop),QEMU 是最简单的入门方法。它不需要更改您的 Dockerfile,并且 BuildKit 会自动检测可用的二级架构。当 BuildKit 需要为不同的体系结构运行二进制文件时,它会通过在处理程序中注册的二进制文件自动加载它binfmt_misc 。
binfmt_misc对于在主机操作系统上注册的 QEMU 二进制文件要在容器内透明地工作,它们必须静态编译并使用fix_binary标志注册。这需要内核 >= 4.8 和 binfmt-support >= 2.1.7。您可以通过检查是否 F在/proc/sys/fs/binfmt_misc/qemu-*. 虽然 Docker Desktop 预配置了binfmt_misc对其他平台的支持,但对于其他安装,它可能需要使用 tonistiigi/binfmt 图像进行安装。

docker run --privileged --rm tonistiigi/binfmt --install all

这时候重新走启动构建器这一步后,检查构建器情况。

root@qhdata-dev:/home/qhdata# docker buildx ls
NAME/NODE    DRIVER/ENDPOINT             STATUS  BUILDKIT PLATFORMS
mybuilder *  docker-container                             
  mybuilder0 unix:///var/run/docker.sock running v0.11.6  linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/386, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/mips64le, linux/mips64
default      docker                                       
  default    default                     running 20.10.23 linux/amd64, linux/386, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/arm/v7, linux/arm/v6
  
root@qhdata-dev:~# docker buildx inspect --bootstrap
Name:          mybuilder
Driver:        docker-container
Last Activity: 2023-06-09 06:14:55 +0000 UTC

Nodes:
Name:      mybuilder0
Endpoint:  unix:///var/run/docker.sock
Status:    running
Buildkit:  v0.11.6
Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/386, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/mips64le, linux/mips64
root@qhdata-dev:~# 

这时候发现都可以支持arm架构了。
然后使用下面语句进行构建即可。

docker buildx build --platform linux/amd64,linux/arm64 --output type=tar,dest=./dm_base_python38.rar  -t dm_base/python38 .

部分参数说明:

  • --load::简写为–output=type=docker,会自动将单平台构建结果加载到docker images
  • --output,-o:输出目的地(格式type=local,dest=path:)
  • --platform:为构建设置目标平台
  • --add-host:添加自定义主机到 IP 映射(格式host:ip:)
  • --file,-f:Dockerfile 的名称(默认值PATH/Dockerfile:)

platform详细说明:
设置构建的目标平台。如果Dockerfile 中所有的FROM命令没有自己--platform标志则会使用这个参数所代表的platform作为最终platform。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/642358.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Springboot Apollo配置yml

1.背景&#xff1a; 项目都是配置的Apollo配置中心来进行配置的。新功能需要yml格式的数据&#xff08;层级结构更清晰&#xff09; 2.问题&#xff1a; 1&#xff09;Apollo是否支持yml格式的配置信息&#xff1f; 2&#xff09;配置好了以后读取不到Apollo配置的yml。 3…

平心而论,做电商数据分析还得这款大数据分析平台

各个业务系统上的数据能放一起分析了吗&#xff1f;根据物流周期做好库存计划了吗&#xff1f;广告投入分析评估报表出来吗&#xff1f;运营、物流、财务等部门环节间的信息脱节解决了吗&#xff1f;做电商数据分析不仅仅是做一两个销售分析报表&#xff0c;而是要综合各个部门…

vue3 - 内置组件Teleport的使用

<Teleport> 是一个内置组件&#xff0c;它可以将一个组件内部的一部分模板“传送”到该组件的 DOM 结构外层的位置去。 1&#xff0c;使用场景&#xff1a; 一个组件模板的一部分在逻辑上从属于该组件&#xff0c;但从整个应用视图的角度来看&#xff0c;它在 DOM 中应该…

Python网络爬虫使用教程

文章目录 一、URL资源抓取1.urllib2.requests3.requests-html二、正则表达式三、数据解析1.Beautiful Soup2.lxml3.selectolax四、自动化爬虫selenium五、爬虫框架1.Scrapy2.pyspider框架六、模拟登录与验证码识别七、autoscraper&#xff08;不需要编程基础&#xff09; 一、U…

小鱼深度产品测评之:阿里云新款通用算力型ECS云服务器Universal实例,实力与能力并存的一款产品。

ECS U实例评测 1、引言2、购买流程3、向导展示4、实例4.1 创建实例4.2 迁移上云4.3 查询功能4.3.1 下拉框选项4.3.2 查询结果保存 4.4 默认定位4.5 分组4.6 监控4.6.1 查看监控大盘4.6.2 自定义报警规则4.6.3 一键报警 4.7 列表操作4.7.1 资源变配4.7.2 远程链接4.7.3 续费 4.8…

深入探究测试用例设计的底层逻辑

测试用例是每位测试人员都绕不开的话题&#xff0c;也是大家习以为常的事情。几乎所有测试相关的公众号、博客、专栏&#xff0c;都会提及测试用例&#xff0c;由此可见它的重要性。但是&#xff0c;有许多从业者&#xff0c;对测试用例的设计仍然依靠经验积累&#xff0c;即使…

达梦8命令行方式安装创建数据库

在实际生产环境中&#xff0c;有很多linux服务器并没有安装桌面&#xff0c;无法调用图形化界面来安装、创建和配置数据库。下面讲解在linux操作系统中&#xff0c;以命令行的形式创建、安装、启动DM8数据库。 一、命令行安装数据库软件 1.用root账号将数据库安装包dm8_setup…

DJ4-3 动态分区分配算法

目录 一、基于顺序搜索的分配算法 1、最佳适应算法&#xff08;BF&#xff09; 2、最坏适应算法&#xff08;Worst fit&#xff0c;WF&#xff09; 3、首次适应算法&#xff08;First Fit&#xff0c;FF&#xff09; 4、下次适应算法&#xff08;Next fit&#xff0c;NF&a…

机器人技术在 PCB 制造中的关键优势

原创 | 文 BFT机器人 印刷电路板或 PCB 相当于神经系统的计算机。它是连接微型电子元件&#xff08;电阻器、微芯片、连接器和电容器&#xff09;的基础。它极其复杂&#xff0c;有些部分非常微小&#xff0c;只能在显微镜下才能看到。 机器人技术是 PCB 制造的核心。没有人能…

从事软件测试行业的你是“摆烂”还是“内卷”?

工作几年后&#xff0c;你还在持续地学习吗&#xff1f; 很多人可能在工作本身上已经耗掉了所有的精力&#xff0c;尤其是在软件行业&#xff0c;加班是常态&#xff0c;空余时间再想集中精力学习某方面的知识会很吃力。 如果像我一样&#xff0c;开始几年做的是基本的功能测…

API自动化测试:如何构建高效的测试流程

一、引言 在当前的软件开发环境中&#xff0c;API&#xff08;Application Programming Interface&#xff09;扮演了极为重要的角色&#xff0c;连接着应用的各个部分。对API进行自动化测试能够提高测试效率&#xff0c;降低错误&#xff0c;确保软件产品的质量。本文将通过实…

IT安全解决方案保护企业网络安全

全球每年报告数以万计的网络安全事件&#xff0c;云解决方案和远程工作的大规模采用意味着大多数组织的攻击面呈指数级增长。采用正确的网络安全解决方案是保护企业免受这些攻击的唯一方法。使用正确IT安全解决方案企业网络安全。 IT安全解决方案 ManageEngine IT安全解决方案…

5 月 Web3 游戏月报:增长有迹可循,但困局仍在

作者&#xff1a;lesleyfootprint.network 5 月 13 日&#xff0c;Line 旗下 NFT 子公司计划将于 2023 年发布五款 NFT 游戏。越来越多的游戏开发者和项目开始涌现&#xff0c;web3 游戏不再仅仅是投机的象征&#xff0c;而是真正有越来越多的项目方深耕与此。然而&#xff0c…

2023-6-13-IP配置知识补充学习

&#x1f37f;*★,*:.☆(&#xffe3;▽&#xffe3;)/$:*.★* &#x1f37f; &#x1f4a5;&#x1f4a5;&#x1f4a5;欢迎来到&#x1f91e;汤姆&#x1f91e;的csdn博文&#x1f4a5;&#x1f4a5;&#x1f4a5; &#x1f49f;&#x1f49f;喜欢的朋友可以关注一下&#xf…

大数据分析平台释疑专用帖第二弹

不管是想要快速了解BI大数据分析平台&#xff0c;还是想要了解BI和自己的需求匹配度&#xff0c;都可关注我们的释疑专用贴。 1、可以分析直播数据吗&#xff1f; 严格来说&#xff0c;只要能够提供数据&#xff0c;就可以做数据可视化分析&#xff0c;直播数据也同理。 如果…

活动新闻稿为什么有企业采访,权威专家表述更容易被央媒报道

传媒如春雨&#xff0c;润物细无声&#xff0c;大家好&#xff0c;我是51媒体网胡老师。 最近有很多新闻稿想让胡老师来同步一些央媒&#xff0c;或者地方头部媒体&#xff0c;因为媒体的权重都比较高&#xff0c;有些稿子写的比较平&#xff0c;缺乏亮点和新闻性&#xff0c;…

接口测试工具Postman学习之常用断言

目录 什么是断言&#xff1f; Postman常见断言方法介绍&#xff1a; 总结&#xff1a; 什么是断言&#xff1f; 断言——就是结果中的特定属性或值与预期做对比&#xff0c;如果一致&#xff0c;则用例通过&#xff0c;如果不一致&#xff0c;断言失败&#xff0c;用例失败。…

Java实训日志02

文章目录 八、项目开发实现步骤&#xff08;二&#xff09;创建项目1、创建Java项目2、创建目录&#xff0c;添加素材&#xff08;1&#xff09;创建help目录添加帮助文档&#xff08;2&#xff09;创建images目录添加图像素材&#xff08;3&#xff09;创建lib目录添加数据库驱…

直播预告 | 博睿学院:AIOps利器-混沌工程实践

混沌工程是通过应用一些经验探索的原则&#xff0c;来学习观察系统是如何反应的。应用混沌工程可以对系统抵抗扰动并保持正常运作的能力&#xff08;稳定性&#xff09;进行校验和评估&#xff0c;提前识别未知隐患并进行修复&#xff0c;进而保障系统更好地抵御生产环境中的失…

window Cmake开发环境搭建

背景 最近的项目需要从linux转战至Windows。因为之前的项目都是在linux环境下开发的&#xff0c;代码也是通过CMAKE编译。攻欲善其事&#xff0c;必先利其器。首先要考虑的是如何在Windows环境下使用cmake编译代码。 另外一个就是IDE的选择了&#xff0c;相比于动辄 好几个G的…