docker清理命令prune

Docker使用中会产生很多冗余的镜像、容器和数据卷,那么怎么来清理这些冗余呢?

系统环境:

虚拟机:Parallels Desktop 16 for Mac  .Version 16.1.2(49151)
操作系统:CentOS Linux release 8.4.2105
Docker:20.10.7, build f0df350

CentOS8-neofetch

常用清理命令参考:

清理所有未使用的镜像、容器和数据卷(不懂该命令请慎用):

docker system prune

查看所有悬挂状态的镜像:

docker image ls -f dangling=true

清理所有悬挂状态的镜像:

docker image prune

OR

docker image rm $(docker image ls -f dangling=true -q)

删除所有停止状态的容器:

docker container prune

删除所有容器(你的所有容器会被清空,慎用)

docker rm -f $(docker ps -aq)

OR

docker container rm -f $(docker container ls -aq)

删除不再使用的数据卷

docker volume rm $(docker volume ls -q)

OR

docker volume prune

删除构建缓存

docker builder prune

Docker运行mysql设置密码问题

Docker运行mysql时,必须提供一个初始化密码,但是有的开发者是本地测试使用,所以不想加密码该如何操作呢?

系统环境:

虚拟机:Parallels Desktop 16 for Mac  .Version 16.1.2(49151)
操作系统:CentOS Linux release 8.4.2105
Docker:20.10.7, build f0df350
MySQL:5.7

CentOS8-neofetch

解决方案:

执行以下命令即可,连接mysql不需要填写密码便能连接上

docker run --name mysql57 \
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
-p 3306:3306 \
-d mysql:5.7

参数参考:

运行容器时设置root密码为:123456

docker run --name mysql57 \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3306:3306 \
-d mysql:5.7

运行容器时创建一个新的用户,并将root密码设置为随机,一般使用场景为单个项目数据库使用唯一的账号密码来管理:

docker run --name=mysqlBlog \
-e MYSQL_DATABASE=blog \
-e MYSQL_USER=blog \
-e MYSQL_PASSWORD=blog \
-e MYSQL_RANDOM_ROOT_PASSWORD=yes \
-p 3306:3306 \
-d mysql:5.7

Docker容器中如何使用vi或者vim编辑器

Docker容器没有安装vi或者vim编辑器,导致提示:command not found

系统环境:

虚拟机:Parallels Desktop 16 for Mac  .Version 16.1.2(49151)
操作系统:CentOS Linux release 8.4.2105
Docker:20.10.7, build f0df350

CentOS8-neofetch

解决方案:

依次执行以下命令即可

apt-get update
apt-get install vim -y

CentOS8安装Docker

在CentOS8系统上安装Docker环境步骤

系统环境:

虚拟机:Parallels Desktop 16 for Mac  .Version 16.1.2(49151)
操作系统:CentOS Linux release 8.4.2105
Docker:20.10.7, build f0df350

CentOS8-neofetch

安装过程

error.先发个错误:podman已经安装导致的报错,执行以下命令删除podman后继续执行后续安装步骤即可,如果你不知道podman是什么,建议直接先执行以下解决方案给出的命令

解决方案:
yum -y erase podman buildah
错误参考:(只要看到 installed package podman… 基本就是这个问题)
Error: 
 Problem 1: problem with installed package podman-3.0.1-7.module_el8.4.0+830+8027e1c4.x86_64
  - package podman-3.0.1-7.module_el8.4.0+830+8027e1c4.x86_64 requires runc >= 1.0.0-57, but none of the providers can be installed
  - package podman-3.0.1-6.module_el8.4.0+781+acf4c33b.x86_64 requires runc >= 1.0.0-57, but none of the providers can be installed
  - package containerd.io-1.4.6-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-73.rc93.module_el8.4.0+830+8027e1c4.x86_64
  - package containerd.io-1.4.6-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-73.rc93.module_el8.4.0+830+8027e1c4.x86_64
  - package containerd.io-1.4.6-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.4.0+673+eabfc99d.x86_64
  - package containerd.io-1.4.6-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.4.0+673+eabfc99d.x86_64
  - cannot install the best candidate for the job
  - package runc-1.0.0-64.rc10.module_el8.4.0+522+66908d0c.x86_64 is filtered out by modular filtering
  - package runc-1.0.0-65.rc10.module_el8.4.0+819+4afbd1d6.x86_64 is filtered out by modular filtering
  - package runc-1.0.0-70.rc92.module_el8.4.0+786+4668b267.x86_64 is filtered out by modular filtering
  - package runc-1.0.0-71.rc92.module_el8.4.0+833+9763146c.x86_64 is filtered out by modular filtering
 Problem 2: package podman-3.0.1-6.module_el8.4.0+781+acf4c33b.x86_64 requires runc >= 1.0.0-57, but none of the providers can be installed
  - package containerd.io-1.4.3-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.4.0+673+eabfc99d.x86_64
  - package containerd.io-1.4.3-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.4.0+673+eabfc99d.x86_64
  - package containerd.io-1.4.3-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-73.rc93.module_el8.4.0+830+8027e1c4.x86_64
  - package containerd.io-1.4.3-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-73.rc93.module_el8.4.0+830+8027e1c4.x86_64
  - package docker-ce-3:20.10.7-3.el8.x86_64 requires containerd.io >= 1.4.1, but none of the providers can be installed
  - package containerd.io-1.4.3-3.2.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.4.0+673+eabfc99d.x86_64
  - package containerd.io-1.4.3-3.2.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.4.0+673+eabfc99d.x86_64
  - package containerd.io-1.4.3-3.2.el8.x86_64 conflicts with runc provided by runc-1.0.0-73.rc93.module_el8.4.0+830+8027e1c4.x86_64
  - package containerd.io-1.4.3-3.2.el8.x86_64 obsoletes runc provided by runc-1.0.0-73.rc93.module_el8.4.0+830+8027e1c4.x86_64
  - package podman-catatonit-3.0.1-6.module_el8.4.0+781+acf4c33b.x86_64 requires podman = 3.0.1-6.module_el8.4.0+781+acf4c33b, but none of the providers can be installed
  - problem with installed package podman-catatonit-3.0.1-7.module_el8.4.0+830+8027e1c4.x86_64
  - package podman-catatonit-3.0.1-7.module_el8.4.0+830+8027e1c4.x86_64 requires podman = 3.0.1-7.module_el8.4.0+830+8027e1c4, but none of the providers can be installed
  - package podman-3.0.1-7.module_el8.4.0+830+8027e1c4.x86_64 requires runc >= 1.0.0-57, but none of the providers can be installed
  - package containerd.io-1.4.4-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-73.rc93.module_el8.4.0+830+8027e1c4.x86_64
  - package containerd.io-1.4.4-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-73.rc93.module_el8.4.0+830+8027e1c4.x86_64
  - package containerd.io-1.4.6-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-73.rc93.module_el8.4.0+830+8027e1c4.x86_64
  - package containerd.io-1.4.6-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-73.rc93.module_el8.4.0+830+8027e1c4.x86_64
  - cannot install the best candidate for the job
  - package runc-1.0.0-64.rc10.module_el8.4.0+522+66908d0c.x86_64 is filtered out by modular filtering
  - package runc-1.0.0-65.rc10.module_el8.4.0+819+4afbd1d6.x86_64 is filtered out by modular filtering
  - package runc-1.0.0-70.rc92.module_el8.4.0+786+4668b267.x86_64 is filtered out by modular filtering
  - package runc-1.0.0-71.rc92.module_el8.4.0+833+9763146c.x86_64 is filtered out by modular filtering
  - package containerd.io-1.4.4-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.4.0+673+eabfc99d.x86_64
  - package containerd.io-1.4.4-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.4.0+673+eabfc99d.x86_64
  - package containerd.io-1.4.6-3.1.el8.x86_64 conflicts with runc provided by runc-1.0.0-70.rc92.module_el8.4.0+673+eabfc99d.x86_64
  - package containerd.io-1.4.6-3.1.el8.x86_64 obsoletes runc provided by runc-1.0.0-70.rc92.module_el8.4.0+673+eabfc99d.x86_64
(try to add '--allowerasing' to command line to replace conflicting packages or '--skip-broken' to skip uninstallable packages or '--nobest' to use not only best candidate packages)

1.安装yum-utils

sudo yum install -y yum-utils

2.添加仓库

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

3.安装 docker-ce docker-ce-cli

sudo yum -y  install docker-ce  docker-ce-cli containerd.io

4.启动 docker 服务

systemctl start docker

5.查看 docker 相关信息

① 查看 docker 版本号:
  docker -v
② 查看 docker 详细信息:
  docker version

6.设置 docker 服务为开机启动

systemctl enable --now docker

Docker命令参考

1. 查看本地已有镜像列表:
  docker images
2. 搜索镜像
  docker search openresty
3. 拉取镜像:
  docker pull openresty/openresty 
4. 删除镜像:
  docker rmi openresty/openresty

5. 查看已有容器列表:
  docker ps
6. 运行容器:
  docker run -d -p 10080:80 --name openresty openresty/openresty
  -d:加了这个参数不会进入容器,想要进入容器需要使用指令 docker exec
7. 停止容器:
  docker stop openresty
8. 重启容器:
  docker restart openresty
9. 进入容器:
  docker exec it openresty /bin/bash
  exit退出容器后不会导致容器停止运行。(推荐该命令连接)
  docker attach openresty
  exit退出容器后会使容器停止运行。
10. 容器和本地之间交换文件:
  docker cp fromPath(被拷贝文件) toPath(要放置被拷贝文件的地方)
  容器外部拷贝文件到容器内部操作
    docker cp ~/Program/hugo/blog/public openresty:openresty:/usr/local/openresty/nginx/html/blog
  容器内部部拷贝文件到容器外部操作
    docker cp openresty:openresty:/usr/local/openresty/nginx/html/blog ~/Program/hugo/blog/public
11. 删除容器:
  docker rm -f openresty
12. 清理所有处于终止状态的容器:
  docker container prune

系统提供的命令

  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes