您的位置 首页 > 教育学习

wwwhbeeeducn,干货看这篇!Docker入门篇

一、简介

1、介绍

docker底层是一个平台,通过容器化技术,向上承载了很多镜像和容器

2004年:Solaris系统就提供了容器化技术

2008年:Linux系统也提出容器化支持,代号LXC1.0,Docker技术的前身

2013年:Docker的母公司dotCloud的创始人Solomon Hykes在内部开始使用了,此时Docker已经诞生了

同年3月Docker进行了开源,释放了第一个开源的版本

2016年:Docker有了重大的变更,由统一的一个版本拆分成CE和EE两个子版本

至今:社区比较活跃

小知识:“Linux”国际音标 ['li:nэks] ,但Linus Torvalds本人是芬兰人,地方口音读['li:niks],所以这两种读法都没问题

2、特点

  • 开源的应用容器引擎,基于Go语言开发并遵从 A 协议开源;
  • 容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器开销极低,换句话说系统资源利用率很高
  • Docker就是容器化技术的代名词;容器化不止Docker一种但Docker是市场上使用人数最多,最受业界追捧的一款产品
  • Docker也具备一定虚拟化智能;利用docker不仅能创建容器而且还能向本地物理机申请资源,这样更加方便程序的迁移与扩展

3、版本

Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了

docker-ce:社区版

docker-ee:企业版(收费)

4、官网

Docker 官网:

Docker 源码:

5、架构

Docker 包括三个基本概念:

  • 镜像(Image):Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 其中包含了完整的一套 Ubun 最小系统的 root 文件系统。
  • 容器(Container):镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
  • 仓库(Repository):仓库可看成一个代码控制中心,用来保存镜像。

Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。

Docker 容器通过 Docker 镜像来创建。

容器与镜像的关系类似于面向对象编程中的对象与类。

概念

说明

Docker 镜像(Images)

Docker 镜像是用于创建 Docker 容器的模板,比如 Ubuntu 系统。

Docker 容器(Container)

容器是独立运行的一个或一组应用,是镜像运行时的实体。

Docker 客户端(Client)

Docker 客户端通过命令行或者其他工具使用 Docker SDK (/develop/sdk/) 与 Docker 守护进程通信。

Docker 主机(Host)

一个物理或者虚拟的机器用于执行 Docker 守护进程和容器。

Docker Registry

Docker 仓库用来保存镜像,可以理解为代码控制中的代码仓库。Docker Hub(https://) 提供了庞大的镜像集合供使用。一个 Docker Registry 中可以包含多个仓库(Repository);每个仓库可以包含多个标签(Tag);每个标签对应一个镜像。通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本。我们可以通过 <仓库名>:<标签> 的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以 latest 作为默认标签。

Docker Machine

Docker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker,比如VirtualBox、 Digital Ocean、Microsoft Azure。

二、自动安装Docker CE

1、官方安装脚本

curl -fsSL | bash -s docker --mirror Aliyun

2、国内 daocloud 一键安装命令

curl -sSL | sh

三、手动安装Docker CE

1、环境要求

centos来说,必须是centos 7+ 版本

windows来说,必须是windows10x64版本(借助的Hypervisor实现虚拟化)

Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看CentOS的内核版本。

[root@xxx ~]# uname -a Linux xxx 3.10.0-1160.11.1.el7.x86_64 #1 SMP Fri Dec 18 16:34:56 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

2、卸载旧版本

较旧的 Docker 版本称为 docker 或 docker-engine 。如果已安装这些程序,请卸载它们以及相关的依赖项。

sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine \ docker-selinux

3、使用 Docker 仓库进行安装

在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库。之后,您可以从仓库安装和更新 Docker。

1、安装所需的软件包

# yum-utils:yum工具集,device-mapper-persistent-data和lvm2:数据存储驱动包,docker必须的,-y 代表自动确认安装 sudo yum install -y yum-utils device-mapper-persistent-data lvm2

2、设置仓库

#这个是官方的(比较慢) sudo yum-config-manager --add-repo # 阿里云 sudo yum-config-manager --add-repo # 清华大学源 sudo yum-config-manager --add-repo

3、将服务器上的软件包信息先在本地缓存,以提高搜索、安装软件的速度,Centos 8及其之上的版本,去掉fast。

sudo yum makecache fast

4、安装最新版本

#安装最新版本的docker ce sudo yum -y install docker-ce docker-ce-cli con

4、安装特定版本

1)列出并排序您存储库中可用的版本。此示例按版本号(从高到低)对结果进行排序。

[root@xxx ~]# yum list docker-ce --showduplicates | sort -r docker-ce.x86_64 3:20.10.9-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.8-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.7-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.6-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.5-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.4-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.3-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.2-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.1-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.12-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.11-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.10-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.0-3.el7 docker-ce-stable

2)通过其完整的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。例如:docker-ce-20.10.9。

sudo yum -y install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> con

5、启动服务

#启动Docker服务 sudo systemctl start docker #设置开机自动启动 sudo systemctl enable docker

4、验证

#查看状态 [root@xxx ~]# sudo systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system; disabled; vendor preset: disabled) Active: active (running) since 三 2022-01-19 15:03:52 CST; 37s ago Docs: Main PID: 2591 (dockerd) Tasks: 7 Memory: 26.2M CGroup: /system.slice └─2591 /usr/bin/dockerd -H fd:// --containerd=/run/containerd #查看版本 [root@xxx ~]# docker version Client: Docker Engine - Community Version: 20.10.12 API version: 1.41 Go version: go1.16.12 Git commit: e91ed57 Built: Mon Dec 13 11:45:41 2021 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.12 API version: 1.41 (minimum version 1.12) Go version: go1.16.12 Git commit: 459d0df Built: Mon Dec 13 11:44:05 2021 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.4.12 GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d runc: Version: 1.0.2 GitCommit: v1.0.2-0-g52b36a2 docker-init: Version: 0.19.0 GitCommit: de40ad0 #通过运行 hello-world 映像来验证是否正确安装了 Docker Engine-Community 。 [root@xxx ~]# sudo docker run hello-world Unable to find image 'hello-world:latest' locally Trying to pull repository docker.io/library/hello-world ... latest: Pulling from docker.io/library/hello-world 2db29710123e: Pull complete Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f Status: Downloaded newer image for docker.io/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: For more examples and ideas, visit: /get-started/

5、卸载 docker

#删除安装包: yum remove docker-ce # 删除镜像、容器、配置文件等内容: rm -rf /var/lib/docker

四、配置阿里云镜像仓库

 Docker的默认官方远程仓库是,由于网络原因,下载一个Docker官方镜像可能会需要很长的时间,甚至下载失败。为此,阿里云容器镜像服务ACR提供了官方的镜像站点(可以理解为CDN代理),从而加速官方镜像的下载。下面介绍如何使用阿里云镜像仓库。

1、登录阿里云控制台

需要有账号,搜索“容器镜像服务”,

2、获取加速地址

3、配置自定义镜像仓库地址

# 创建目录 sudo mkdir -p /etc/docker # 配置docker自定义镜像仓库地址 sudo tee /etc/docker <<-'EOF' { "registry-mirrors": [";] } EOF #重新加载配置 sudo systemctl daemon-reload #重启docker sudo systemctl restart docker #验证,在展示的信息中找到Registry Mirrors属性,如果是刚才配置的地址,那么说明设置成功 docker info

五、Docker 命令

docker 客户端非常简单 ,可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项。

[root@xxx docker]# docker Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/root/.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/root/.docker;) --tlscert string Path to TLS certificate file (default "/root/.docker;) --tlskey string Path to TLS key file (default "/root/.docker;) --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: app* Docker App (Docker Inc., v0.9.1-beta3) builder Manage builds buildx* Docker Buildx (Docker Inc., v0.7.1-docker) config Manage Docker configs container Manage containers context Manage contexts image Manage images manifest Manage Docker image manifests and manifest lists network Manage networks node Manage Swarm nodes plugin Manage plugins scan* Docker Scan (Docker Inc., v0.12.0) secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes Commands: 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 Run 'docker COMMAND --help' for more information on a command. To get more help with docker, check out our guides at /go/guides/

1、镜像

  • 构建镜像我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。
  • 设置镜像标签#先查
    [root@xxx docker]# docker images -a
    REPOSITORY TAG IMAGE ID CREATED SIZE
    mysql latest 3218b38490ce 4 weeks ago 516MB
    #设置镜像tag
    [root@xxx docker]# docker tag 3218b38490ce mysql:v7
    #再查
    [root@xxx docker]# docker images -a
    REPOSITORY TAG IMAGE ID CREATED SIZE
    mysql latest 3218b38490ce 4 weeks ago 516MB
    mysql v7 3218b38490ce 4 weeks ago 516MB
  • 列出本地主机上的镜像[root@xxx docker]# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    ubuntu 15.10 9b9cb95443b5 5 years ago 137MB各个选项说明:REPOSITORY:表示镜像的仓库源TAG:镜像的标签IMAGE ID:镜像IDCREATED:镜像创建时间SIZE:镜像大小同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如 ubuntu 仓库源里,有 15.10、14.04 等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。
  • 当我们在本地主机上使用一个不存在的镜像时 Docker 就会自动下载这个镜像,如果要使用版本为15.10的ubuntu系统镜像来运行容器时,命令如下:[root@xxx docker]# docker run -it ubuntu:15.10 /bin/bash
    Unable to find image 'ubuntu:15.10' locally
    15.10: Pulling from library/ubuntu
    7dcf5a444392: Pull complete
    759aa75f3cee: Pull complete
    3fa871dc8a2b: Pull complete
    224c42ae46e7: Pull complete
    Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3
    Status: Downloaded newer image for ubuntu:15.10参数说明:-i: 交互式操作。-t: 终端。ubuntu:15.10: 这是指用 ubuntu 15.10 版本镜像为基础来启动容器。/bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。如果不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像。
  • 查找镜像可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: 也可以使用 docker search 命令来搜索镜像。比如需要一个 jdk的镜像,可以通过 docker search 命令搜索 jdk 来寻找适合的镜像。[root@xxx docker]# docker search jdk
    NAME DESCRIPTION STARS OFFICIAL AUTOMATED
    java DEPRECATED; use "openjdk" (or other JDK impl… 1976 [OK]
    primetoninc/jdk Oracle jdk 1.8, 1.7 25 [OK]
    ascdc/jdk8 jdk8 17 [OK]
    codenvy/jdk8_maven3_tomcat8 JDK 8, Maven 3, Tomcat 8 10 [OK]
    ringcentral/jdk an alpine Linux image included glibc and JDK 9
    kdvolder/jdk8 8
    codenvy/jdk7_tomcat7 Dockerfile … 8
    openkbs/jdk-mvn-py3 Java 8/11 OpenJDK + Maven + Python 3+ pip +… 6 [OK]
    gmaslowski/jdk Oracle JDK8 6
    jdkato/vale The official Docker image for Vale. 4
    codenvy/jdk7_vnc Dockerfile … 3
    openkbs/jdk11-mvn-py3 Java JDK 11 + Maven 3 + Python 3 + Gradle 6 3 [OK]
    codenvy/jdk7 Dockerfile … 3
    codenvy/jdk7_jboss7 Dockerfile … 2
    andonescu/jdk-8-node-docker-gcp jdk8 node docker gcp 1
    openkbs/jdk-mvn-py Java JDK 8 + Maven 3 + Python 2 1 [OK]
    payneteasy/jdk-8 Java 8 for using with gitlab-ci 1 [OK]
    uquote Apache Tomee Plume with Oracle JDK 8 on Cent… 0 参数说明:NAME: 镜像仓库源的名称DESCRIPTION: 镜像的描述OFFICIAL: 是否 docker 官方发布stars: 类似 Github 里面的 star,表示点赞、喜欢的意思。AUTOMATED: 自动构建。
  • 获取一个新的镜像如果我们想预先下载这个镜像,我们可以使用 docker pull 命令来下载它。[root@xxx docker]# docker pull openjdk:11
    11: Pulling from library/openjdk
    0e29546d541c: Pull complete
    9b829c73b52b: Pull complete
    cb5b7ae36172: Pull complete
    6494e4811622: Pull complete
    668f6fcc5fa5: Pull complete
    dc120c3e0290: Pull complete
    8f7c0eebb7b1: Pull complete
    Digest: sha256:33c346c637ebb17823cbeda4d4e5601c66262752dadcae559a03b090f6505a47
    Status: Downloaded newer image for openjdk:11
    docker.io/library/openjdk:11下载完成后,我们可以直接使用这个镜像来运行容器。
  • 镜像的保存[root@xxx docker]# docker images -a
    REPOSITORY TAG IMAGE ID CREATED SIZE
    mysql latest 3218b38490ce 4 weeks ago 516MB
    ubuntu latest ba6acccedd29 3 months ago 72.8MB
    #保存mysql这个镜像
    [root@xxx docker]# docker save 3218b >my
    #查看一下,已经导出成功
    [root@xxx docker]# ls -lh
    总用量 497M
    -rw-r--r-- 1 root root 67 1月 19 15:08 daemon.json
    -rw-r--r-- 1 root root 67 1月 18 17:00 daemon.j
    -rw------- 1 root root 244 9月 14 2018 key.json
    -rw-r--r-- 1 root root 497M 1月 19 17:20 my
  • 镜像的导入#把原来已经有的删掉
    [root@xxx docker]# docker rmi 3218b38490ce
    Error response from daemon: conflict: unable to delete 3218b38490ce (must be forced) - image is being used by stopped container a70e7c12f042
    [root@xxx docker]# docker rmi -f 3218b38490ce
    Untagged: mysql:latest
    Untagged: mysql@sha256:e9027fe4d91c01656806cc784e914937271037f7738bd5b8e7709
    Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
    [root@xxx docker]# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    ubuntu latest ba6acccedd29 3 months ago 72.8MB
    #导入镜像
    [root@xxx docker]# docker load < my
    Loaded image ID: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
    [root@xxx docker]# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    <none> <none> 3218b38490ce 4 weeks ago 516MB
    ubuntu latest ba6acccedd29 3 months ago 72.8MB
    [root@xxx docker]# docker tag 3218b38490ce mysql:8.0
    [root@xxx docker]# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    mysql 8.0 3218b38490ce 4 weeks ago 516MB
    ubuntu latest ba6acccedd29 3 months ago 72.8MB
  • 删除镜像[root@xxx docker]# docker rmi 5505a9a39df1
    Untagged: openjdk:11
    Untagged: openjdk@sha256:33c346c637ebb17823cbeda4d4e5601c66262752dadcae559a03b090f6505a47
    Deleted: sha256:5505a9a39df1c99d27b47ce81bee2693807f8915989f359f8648002e5d918d50
    Deleted: sha256:ea82d4efcdfa1c039d722a5a9613c18d3c3a84fbba8efae5e7f13cb3b4ec379f
    Deleted: sha256:79a6c362c6b1a580d2d8d33f6d860d45c530f34ff7c0441d36b61aceefdfd656
    Deleted: sha256:1788a74c5c86e769f61cd615269eba11c3d7648eac4a85a1ffd2840427820a2f
    Deleted: sha256:cbce712ed17923285239f9d9c0528984aef065b7413d68a0290e2c8eecc98f4a
    Deleted: sha256:aa56d037ee5925ebf11127c3e1f617874c4ce8bae6b6af7d132b7f7a4a606e6f
    Deleted: sha256:97e5f44efb543d466c5847602654a8cb22c9466b61d04988d47ec44b197ea874
    Deleted: sha256:11936051f93baf5a4fb090a8fa0999309b8173556f7826598e235e8a82127bce

2、容器

docker run :创建一个新的容器并运行一个命令

# 语法 docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

OPTIONS说明:

  • -a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
  • -d: 后台运行容器,并返回容器ID;
  • -i: 以交互模式运行容器,通常与 -t 同时使用;
  • -P: 随机端口映射,容器内部端口随机映射到主机的端口
  • -p: 指定端口映射,格式为:主机(宿主)端口:容器端口
  • -t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
  • --name="nginx-lb": 为容器指定一个名称;
  • --dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
  • --dns-search exam: 指定容器DNS搜索域名,默认和宿主一致;
  • -h "mars": 指定容器的hostname;
  • -e username="ritchie": 设置环境变量;
  • --env-file=[]: 从指定文件读入环境变量;
  • --cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;
  • -m :设置容器使用内存最大值;
  • --net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;
  • --link=[]: 添加链接到另一个容器;
  • --expose=[]: 开放一个端口或一组端口;
  • --volume , -v: 绑定一个卷
  • 启动一个容器通过镜像去启动一个容器,如果本地没有会先拉取镜像,拿mysql为例[root@xxx docker]# docker run -it ubuntu:15.10 /bin/bash
    Unable to find image 'ubuntu:15.10' locally
    15.10: Pulling from library/ubuntu
    7dcf5a444392: Pull complete
    759aa75f3cee: Pull complete
    3fa871dc8a2b: Pull complete
    224c42ae46e7: Pull complete
    Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3
    Status: Downloaded newer image for ubuntu:15.10
  • 启动容器并后台运行[root@xxx docker]# docker run -itd ubuntu
    fecd6ab9940248da91e0b8468ec84d3084003446c135b4dd05527c7070266da8注:加了 -d 参数默认不会进入容器,想要进入容器需要使用指令 docker exec(下面会介绍到)
  • 停止一个容器[root@xxx docker]#docker stop <CONTAINER ID>
  • 启动已停止运行的容器[root@xxx docker]#docker start <CONTAINER ID>
  • 重启已停止的容器[root@xxx docker]#docker restart <CONTAINER ID>
  • 列出正在运行的容器 [root@xxx docker]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  • 显示所有的容器,包括未运行的[root@xxx docker]# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    2a050e0796d6 5f94f53bbced "jshell" 10 minutes ago Exited (0) 10 minutes ago peaceful_goldstine
    6094792e149f 5f94f53bbced "jshell" 12 minutes ago Exited (0) 12 minutes ago optimistic_benz
  • 删除容器[root@xxx docker]# docker rm 2a0 609
    2a0
    609
  • 进入容器[root@xxx docker]# docker exec -it ab303 /bin/bash
    root@ab303aa94008:

3、示例

1、用容器部署mysql8

# 查看已有的镜像 [root@xxx docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 8.0 3218b38490ce 4 weeks ago 516MB mysql latest 3218b38490ce 4 weeks ago 516MB ubuntu latest ba6acccedd29 3 months ago 72.8MB # 用mysql 8的镜像启动一个容器设置root用户密码为“root”,映射主机3306到容器3306端口,这里没有加-d是为了看效果 [root@xxx docker]# docker run -ite MYSQL_ROOT_PASSWORD=root -p 3306:3306 mysql:8.0 2022-01-20 02:46:19+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.27-1debian10 started. 2022-01-20 02:46:19+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' 2022-01-20 02:46:19+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.27-1debian10 started. 2022-01-20 02:46:19+00:00 [Note] [Entrypoint]: Initializing database files 2022-01-20T02:46:19.794276Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.27) initializing of server in progress as process 42 2022-01-20T02:46:19.806542Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2022-01-20T02:46:20.799002Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2022-01-20T02:46:22.472818Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main 2022-01-20T02:46:22.473492Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main 2022-01-20T02:46:22.642697Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. 2022-01-20 02:46:26+00:00 [Note] [Entrypoint]: Database files initialized 2022-01-20 02:46:26+00:00 [Note] [Entrypoint]: Starting temporary server mysqld will log errors to /var/lib/mysql mysqld is running as pid 89 #查看已启动的容器 [root@xxx docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ab303aa94008 mysql:8.0 "docker-en…" 52 minutes ago Up 52 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp flamboyant_poitras # 进入docker部署的mysql、登录、登出: [root@xxx docker]# docker exec -it ab303 /bin/bash root@ab303aa94008:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 17 Server version: 8.0.27 MySQL Community Server - GPL Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select host,user from user; +-----------+------------------+ | host | user | +-----------+------------------+ | % | root | | localhost | my | | localhost | my | | localhost | my | | localhost | root | +-----------+------------------+ 5 rows in set sec) mysql> quit Bye root@ab303aa94008:/# exit exit [root@xxx docker]#

六、问题

一、 image has dependent child images 错误

在 Docker 中删除 image 时有时会遇到类似

Error response from daemon: conflict: unable to delete 6ec9a5a0fc9f (cannot be forced) - image has dependent child images

这样的错误,原因是有另外的 image FROM 了这个 image,可以使用下面的命令列出所有在指定 image 之后创建的 image 的父 image

docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=xxxxxx)

其中 xxxxxx 是报错 image 的 id,在文章开头的例子中就是 6ec9a5a0fc9f。从列表中查找到之后就可以核对并删除这些 image。

示例

1、查看我的镜像列表。

[root@xxx ~]# docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE docker-test latest 193900123eba 4 months ago 661 MB <none> <none> 3698e4175d75 4 months ago 661 MB <none> <none> 3bfaab90ad42 4 months ago 661 MB docker.io/java 8 d23bdf5b1b1b 5 years ago 643 MB

2、删除none的镜像(删不掉)

[root@xxx ~]# docker rmi 3698e4175d75 3bfaab90ad42 Error response from daemon: conflict: unable to delete 3698e4175d75 (cannot be forced) - image has dependent child images Error response from daemon: conflict: unable to delete 3bfaab90ad42 (cannot be forced) - image has dependent child images

3、查找出所有在指定 image 之后创建的 image 的父 image

[root@xxx ~]# docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q --filter since=3698e4175d75) [docker-test:latest] sha256:193900123ebabef4b2cfc1ed86f8cd521f1ddc6f615282d7ad3e7d7bf22ee565 sha256:3bfaab90ad420efea78888a1a1d2a15e675b663d55cb6c629ff66a8b2bcabc55

4、删除关联的依赖镜像,关联的none镜像也会被删除

[root@xxx ~]# docker rmi 19390 Untagged: docker-test:latest Deleted: sha256:193900123ebabef4b2cfc1ed86f8cd521f1ddc6f615282d7ad3e7d7bf22ee565 Deleted: sha256:3bfaab90ad420efea78888a1a1d2a15e675b663d55cb6c629ff66a8b2bcabc55 Deleted: sha256:3698e4175d758f9a27fda4d62ae1d85eab8035aeb1684023a249027e87e3a3f1 Deleted: sha256:6e397b2b982d9fc44374e8d901e6c9218af038c7816bc11add1fbb4a240c2036

5、再次查看镜像列表,相关镜像已经删除

[root@xxx ~]# docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/java 8 d23bdf5b1b1b 5 years ago 643 MB

其他操作

# 停止所有“Exited”容器 ➜ ~ docker ps -a | grep "Exited" | awk '{print $1 }'|xargs docker stop # 删除所有“Exited”容器 ➜ ~ docker ps -a | grep "Exited" | awk '{print $1 }'|xargs docker rm # 删除所有none容器 ➜ ~ docker images|grep none|awk '{print $3 }'|xargs docker rmi

关于作者: luda

无忧经验小编鲁达,内容侵删请Email至wohenlihai#qq.com(#改为@)

热门推荐