本帖最后由 赵美京₁₉⁸⁸⁸ 于 2024-4-30 16:50 编辑
【树莓派5 测评】 + 15.Docker功能测试(zmj)
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows操作系统的机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。 1. Docker安装
Docker安装有多种方式,此处仅举例两种,且推荐第二种。
1.1 安装方式1常规安装: - //------安装指令
- # 更新本地软件包列表
- sudo apt update
- # 更新已安装的包
- sudo apt upgrade -y
- # 安装依赖包
- sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
- # 添加 Docker 官方 GPG 密钥
- sudo install -m 0755 -d /etc/apt/keyrings
- sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
- sudo chmod a+r /etc/apt/keyrings/docker.asc
- # 将存储库添加到 Apt 源
- echo \
- "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
- $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
- sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- # 再次更新本地软件包列表,更新完成后可以使用 apt-cache madison docker-ce | awk '{ print $3 }' 列出存储库中可用版本
- sudo apt update
- # 安装 Docker
- sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
- # 验证安装
- docker --version
- docker compose version
- # 启动 docker 守护进程,同时设置开机启动(这部分操作在虚拟机中无法使用,需要手动启动 docker)
- sudo systemctl start docker
- sudo systemctl enable docker
- 或者
- sudo systemctl enable --now docker
复制代码 1.2 安装方式2便捷安装,安装的是最新版。 - //------安装指令
- # 便捷安装
- curl -fsSL https://get.docker.com/ | sh
- # 验证安装
- docker --version
- docker compose version
- # 启动 docker 守护进程,同时设置开机启动(这部分操作在虚拟机中无法使用,需要手动启动 docker)
- sudo systemctl start docker
- sudo systemctl enable docker
- 或者
- sudo systemctl enable --now docker
- //------Log信息
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ ll
- total 8
- drwxr-xr-x 2 zhaomeijing zhaomeijing 4096 Apr 28 15:05 ./
- drwxr-xr-x 16 zhaomeijing zhaomeijing 4096 Apr 28 15:05 ../
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ curl -fsSL https://get.docker.com/ | sh
- # Executing docker install script, commit: e5543d473431b782227f8908005543bb4389b8de
- + sudo -E sh -c apt-get update -qq >/dev/null
- + sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq apt-transport-https ca-certificates curl >/dev/null
- + sudo -E sh -c install -m 0755 -d /etc/apt/keyrings
- + sudo -E sh -c curl -fsSL "https://download.docker.com/linux/debian/gpg" | gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg
- + sudo -E sh -c chmod a+r /etc/apt/keyrings/docker.gpg
- + sudo -E sh -c echo "deb [arch=arm64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian bookworm stable" > /etc/apt/sources.list.d/docker.list
- + sudo -E sh -c apt-get update -qq >/dev/null
- + sudo -E sh -c DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-ce-rootless-extras docker-buildx-plugin >/dev/null
- + sudo -E sh -c docker version
- Client: Docker Engine - Community
- Version: 26.1.0
- API version: 1.45
- Go version: go1.21.9
- Git commit: 9714adc
- Built: Mon Apr 22 17:06:59 2024
- OS/Arch: linux/arm64
- Context: default
- Server: Docker Engine - Community
- Engine:
- Version: 26.1.0
- API version: 1.45 (minimum version 1.24)
- Go version: go1.21.9
- Git commit: c8af8eb
- Built: Mon Apr 22 17:06:59 2024
- OS/Arch: linux/arm64
- Experimental: false
- containerd:
- Version: 1.6.31
- GitCommit: e377cd56a71523140ca6ae87e30244719194a521
- runc:
- Version: 1.1.12
- GitCommit: v1.1.12-0-g51d5e94
- docker-init:
- Version: 0.19.0
- GitCommit: de40ad0
- ================================================================================
- To run Docker as a non-privileged user, consider setting up the
- Docker daemon in rootless mode for your user:
- dockerd-rootless-setuptool.sh install
- Visit https://docs.docker.com/go/rootless/ to learn about rootless mode.
- To run the Docker daemon as a fully privileged service, but granting non-root
- users access, refer to https://docs.docker.com/go/daemon-access/
- WARNING: Access to the remote API on a privileged Docker daemon is equivalent
- to root access on the host. Refer to the 'Docker daemon attack surface'
- documentation for details: https://docs.docker.com/go/attack-surface/
- ================================================================================
- zhaomeijing@raspberrypi5:~/workspace/15_docker$
复制代码//------Docker便捷安装: 2. Docker测试Docker测试流程: 首先查看Docker版本,然后运行“hello-world”测试,最后删除Docker镜像。
测试指令如下: - //------测试指令
- //---版本查看
- docker --version
- docker compose version
- //---运行状态(无镜像)
- sudo docker images
- sudo docker ps -a
- //---测试运行hello-world
- sudo docker run hello-world
- //---运行状态(有镜像)
- sudo docker images
- //---先删除Docker容器,再删除Docker镜像
- sudo docker ps -a
- sudo docker rm ff5d40552233
- sudo docker images
- sudo docker rmi ee301c921b8a
- //------Log信息
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ docker --version
- Docker version 26.1.0, build 9714adc
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ docker compose version
- Docker Compose version v2.26.1
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ sudo docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ sudo docker ps -a
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ sudo docker run hello-world
- Unable to find image 'hello-world:latest' locally
- latest: Pulling from library/hello-world
- 478afc919002: Pull complete
- Digest: sha256:a26bff933ddc26d5cdf7faa98b4ae1e3ec20c4985e6f87ac0973052224d24302
- 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.
- (arm64v8)
- 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/
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ sudo docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- hello-world latest ee301c921b8a 12 months ago 9.14kB
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ sudo docker ps -a
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- ff5d40552233 hello-world "/hello" 15 seconds ago Exited (0) 13 seconds ago compassionate_noyce
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ sudo docker rmi ee301c921b8a
- Error response from daemon: conflict: unable to delete ee301c921b8a (must be forced) - image is being used by stopped container ff5d40552233
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ sudo docker ps -a
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- ff5d40552233 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago compassionate_noyce
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ sudo docker rm ff5d40552233
- ff5d40552233
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ sudo docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- hello-world latest ee301c921b8a 12 months ago 9.14kB
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ sudo docker rmi ee301c921b8a
- Untagged: hello-world:latest
- Untagged: hello-world@sha256:a26bff933ddc26d5cdf7faa98b4ae1e3ec20c4985e6f87ac0973052224d24302
- Deleted: sha256:ee301c921b8aadc002973b2e0c3da17d701dcd994b606769a7e6eaa100b81d44
- Deleted: sha256:12660636fe55438cc3ae7424da7ac56e845cdb52493ff9cf949c47a7f57f8b43
- zhaomeijing@raspberrypi5:~/workspace/15_docker$ sudo docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- zhaomeijing@raspberrypi5:~/workspace/15_docker$
复制代码//------Docker测试结果: 3. 其他Docker指令下面列出一些相对常用的Docker指令,更多指令请参考Docker帮助指令“docker -h”。 - //------其他Docker指令
- //---基础配置
- # 开机自启动docker
- sudo systemctl enable docker
- # 启动docker
- sudo systemctl start docker
- # 查看docker运行状态
- sudo systemctl status docker
- //---建立Docker用户组
- 默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。
- # 建立 docker 组:
- sudo groupadd docker
- # 将当前用户加入 docker 组:
- sudo usermod -aG docker $USER
- # 重启docker
- sudo service docker restart
- # 切换当前会话到新 group
- newgrp - docker
- //---镜像与容器操作
- # 列出镜像
- docker image ls
- # 删除本地镜像
- docker image rm [选项] <镜像1> [<镜像2> ...]
- # 列出正在运行的容器
- docker ps
- docker container ls
- # 列出所有容器
- docker ps -a
- docker container ls -a
- # 删除容器
- docker container rm [选项] <容器1> [<容器2> ...]
- # 清理掉所有处于终止状态的容器
- docker container prune
- # 终止容器
- docker container stop [选项] <容器1> [<容器2> ...]
- # 启动容器
- docker container start [选项] <容器1> [<容器2> ...]
- # 重启容器
- docker container restart [选项] <容器1> [<容器2> ...]
- # 容器与本地文件传输
- docker cp 本地文件路径 容器ID:容器路径
- docker cp 容器ID:容器文件路径 本地路径
复制代码
//------end
|