how to install and use docker in ubuntu20.04

sudo apt-get remove docker docker-engine docker.io containerd runc

  安装Docker之前,确保之前安装的Docker已经删除。这行命令是为了卸载系统上已经安装的Docker引擎和相关组件。

  • docker:Docker软件包。
  • docker-engine:Docker引擎。
  • docker.io:Docker软件包。
  • containerd:Docker容器运行时。
  • runc:Docker容器管理工具。

  执行这个命令会删除上述组件及其相关文件,卸载Docker引擎并清理其配置和数据。注意,这个命令只是卸载Docker引擎,但不会删除镜像、容器和卷等数据。如果需要删除这些数据,请使用docker rm、docker rmi等相关命令进行清理。

一、从官方资料库安装Docker

1) sudo apt update

用于更新软件包列表,它会连接到Ubuntu软件包仓库并下载最新的软件包列表。

2) sudo apt install apt-transport-https ca-certificates curl software-properties-common -y

这条命令用于安装一些必要的工具和依赖项,以便您可以通过HTTPS协议安装和更新软件包。具体而言:

  • apt-transport-https:它允许apt工具通过HTTPS协议使用存储库,以更安全地获取软件包。
  • ca-certificates:这是一个证书包,其中包含基本的CA证书,可帮助您验证下载软件包时的HTTPS连接。
  • curl:这是一个命令行工具,可用于从Web服务器下载文件或数据。
  • software-properties-common:它包含常用的软件源管理工具。

通过添加-y参数,您可以在安装过程中避免交互式提示,从而使安装过程更快捷和便捷。

apt list --installed | grep apt-transport-https
apt list --installed | grep ca-certificates
apt list --installed | grep curl
apt list --installed | grep software-properties-common

如上命令可以帮助我们查看是否这些软件包,若是则返回软件包详细信息;如果没有则不返回任何信息。

3) curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

这个命令是从Docker官方网站下载一个GPG密钥,然后使用sudo权限将密钥添加到本地系统的APT密钥环中。密钥是用于验证从Docker下载的软件包的完整性和真实性的一种安全方式。在密钥添加到APT密钥环之后,系统就能够验证从Docker下载的软件包的完整性和真实性了。

输出OK即可。

4) sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

这个命令是向Ubuntu系统的APT源列表中添加Docker软件的下载源,它告诉APT从Docker的官方下载站点中获取Docker软件包,并指定了软件包的版本(focal)和体系结构(amd64)。这个命令会将这些信息添加到系统的/etc/apt/sources.list文件中,使得之后可以通过APT命令安装、更新、卸载Docker软件。

5) apt-cache policy docker-ce

用来检查Docker软件包的版本和安装源信息。在执行这个命令时,它会列出系统中可用的Docker软件包的版本信息,以及它们所在的软件源的优先级等详细信息。这个命令可以帮助我们确认是否成功添加了Docker的安装源,并查看当前可用的Docker软件包的版本信息。

6) sudo apt install docker-ce -y

用来安装Docker的。在执行这个命令时,它会自动下载并安装Docker软件包,安装过程中可能会需要用户确认一些操作,使用-y参数可以自动确认这些操作。执行完成后,Docker就安装完成了。

7) sudo systemctl status docker

用于检查Docker服务的状态,包括Docker服务是否已经启动、是否运行正常等。执行该命令后,会显示Docker服务的详细状态信息,包括运行状态、日志信息等。如果服务正在运行,则会显示"Active: active (running)"。如果服务未运行,则会显示"Active: inactive (dead)"。如下便是输出信息:

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2023-03-18 15:23:00 CST; 3min 52s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 10053 (dockerd)
      Tasks: 9
     Memory: 25.0M
     CGroup: /system.slice/docker.service
             └─10053 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

3月 18 15:23:00 ubuntu dockerd[10053]: time="2023-03-18T15:23:00.151378865+08:00" level=info msg="[core] [Channel #4 SubChannel #5] Subchannel Connectivity change to READY" module=grpc
3月 18 15:23:00 ubuntu dockerd[10053]: time="2023-03-18T15:23:00.151525595+08:00" level=info msg="[core] [Channel #4] Channel Connectivity change to READY" module=grpc
3月 18 15:23:00 ubuntu dockerd[10053]: time="2023-03-18T15:23:00.222979057+08:00" level=info msg="Loading containers: start."
3月 18 15:23:00 ubuntu dockerd[10053]: time="2023-03-18T15:23:00.658406863+08:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon option --bip can be used t>
3月 18 15:23:00 ubuntu dockerd[10053]: time="2023-03-18T15:23:00.818535589+08:00" level=info msg="Loading containers: done."
3月 18 15:23:00 ubuntu dockerd[10053]: time="2023-03-18T15:23:00.885901055+08:00" level=info msg="Docker daemon" commit=bc3805a graphdriver=overlay2 version=23.0.1
3月 18 15:23:00 ubuntu dockerd[10053]: time="2023-03-18T15:23:00.886237929+08:00" level=info msg="Daemon has completed initialization"
3月 18 15:23:00 ubuntu dockerd[10053]: time="2023-03-18T15:23:00.943005373+08:00" level=info msg="[core] [Server #7] Server created" module=grpc
3月 18 15:23:00 ubuntu systemd[1]: Started Docker Application Container Engine.
3月 18 15:23:00 ubuntu dockerd[10053]: time="2023-03-18T15:23:00.957570105+08:00" level=info msg="API listen on /run/docker.sock"

二、如何使用Docker

docker

Docker命令可以用于管理Docker容器,包括以下功能:

  1. 启动和停止容器
  2. 构建、删除和导出镜像
  3. 容器与本地文件系统之间的复制和挂载
  4. 配置和管理网络和卷
  5. 查看Docker的日志和运行状态
  6. 进入容器的shell并在其中执行命令
  7. 与其他容器进行通信和链接

通过Docker命令,用户可以轻松地创建和管理Docker容器和镜像,并快速地将应用程序部署到不同的环境中。(Docker commands can only be run with the sudo prefix on Ubuntu.)


 

在 Docker 中,Image、Container 和 Volume 是三个重要的概念。

  • Docker Image:Docker 镜像是一个只读的模板,用于创建 Docker 容器。可以将镜像看作是一个类,而容器则是这个类的实例。
  • Docker Container:Docker 容器是 Docker 镜像的运行实例。容器可以被启动、停止、删除,容器内的文件系统可以被读写,容器可以与网络互动。
  • Docker Volume:Docker Volume 是一个可供一个或多个容器使用的持久化数据存储机制,数据可以存储在主机文件系统中,也可以存储在远程主机上的一些网络存储服务中。

总的来说,Docker Image 是容器运行所必需的文件和配置的静态表示,Docker Container 是 Docker Image 的动态实例,而 Docker Volume 则是 Docker 容器中数据的持久化存储方式

1)Docker Images的使用

i. 搜索需要的Docker Images

sudo docker search [keyword]

[keyword]用于指定自己需要的images,像是 sudo docker search ubuntu ,会输出如下结果:

NAME                             DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
ubuntu                           Ubuntu is a Debian-based Linux operating sys…   15724     [OK]       
websphere-liberty                WebSphere Liberty multi-architecture images …   293       [OK]       
open-liberty                     Open Liberty multi-architecture images based…   59        [OK]       
neurodebian                      NeuroDebian provides neuroscience research s…   99        [OK]       
ubuntu-debootstrap               DEPRECATED; use "ubuntu" instead                 50        [OK]       
ubuntu-upstart                   DEPRECATED, as is Upstart (find other proces…   112       [OK]       
ubuntu/nginx                     Nginx, a high-performance reverse proxy & we…   79                   
ubuntu/cortex                    Cortex provides storage for Prometheus. Long…   3                    
ubuntu/squid                     Squid is a caching proxy for the Web. Long-t…   53                   
ubuntu/apache2                   Apache, a secure & extensible open-source HT…   55                   
ubuntu/mysql                     MySQL open source fast, stable, multi-thread…   43                   
ubuntu/redis                     Redis, an open source key-value store. Long-…   17                   
ubuntu/bind9                     BIND 9 is a very flexible, full-featured DNS…   46                   
ubuntu/prometheus                Prometheus is a systems and service monitori…   40                   
ubuntu/postgres                  PostgreSQL is an open source object-relation…   25                   
ubuntu/kafka                     Apache Kafka, a distributed event streaming …   26                   
ubuntu/zookeeper                 ZooKeeper maintains configuration informatio…   5                    
ubuntu/grafana                   Grafana, a feature rich metrics dashboard & …   7                    
ubuntu/memcached                 Memcached, in-memory keyvalue store for smal…   5                    
ubuntu/prometheus-alertmanager   Alertmanager handles client alerts from Prom…   8                    
ubuntu/dotnet-deps               Chiselled Ubuntu for self-contained .NET & A…   6                    
ubuntu/cassandra                 Cassandra, an open source NoSQL distributed …   2                    
ubuntu/dotnet-runtime            Chiselled Ubuntu runtime image for .NET apps…   5                    
ubuntu/telegraf                  Telegraf collects, processes, aggregates & w…   4                    
ubuntu/dotnet-aspnet             Chiselled Ubuntu runtime image for ASP.NET a…   3

当执行这个命令时,Docker 会连接到 Docker Hub 并搜索 Ubuntu 相关的镜像。搜索结果包括了镜像名称、描述、星级、官方/非官方等信息。这个命令可以帮助用户在本地 Docker 中下载所需的 Ubuntu 镜像。

ii. 拉取需要的Docker Images

sudo docker pull ubuntu

用于从 Docker 镜像仓库中拉取(下载)一个名为 "ubuntu" 的镜像到本地。这个命令会默认下载最新的版本(tag 为 "latest"),也可以通过指定 tag 下载其他版本的镜像。下载完成后,就可以在本地使用这个镜像创建 Docker 容器了。输出如下信息:

Using default tag: latest
latest: Pulling from library/ubuntu
2ab09b027e7f: Pull complete 
Digest: sha256:67211c14fa74f070d27cc59d69a7fa9aeff8e28ea118ef3babc295a0428a6d21
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

iii. 查看现有的Docker Images

sudo docker images

这条命令会列出当前系统中所有的 Docker 镜像。包括镜像 ID、镜像名称、镜像大小和镜像创建时间等信息。

2)Docker Containers的使用

sudo docker run hello-world

这个命令会在Docker中运行一个名为"hello-world"的容器。它首先会检查本地是否已经有这个镜像,如果没有就会从Docker Hub上下载这个镜像,然后运行容器,输出一些基本信息并退出。这个命令主要用于测试Docker是否正常工作。主要输出如下信息:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:ffb13da98453e0f04d33a6eee5bb8e46ee50d08ebe17735fc0779d0349e889e9
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/

i. 运行Docker Container

sudo docker run [image-name]

像是我们可以运行 sudo docker run ubuntu ,通过这种方式来运行刚才拉取的Docker Images--Ubuntu。

这条命令会启动一个 Ubuntu 镜像的容器,但是由于没有指定具体的命令,容器会立即启动并立即退出。如果想要保持容器运行,可以通过 -it 参数启动一个交互式的 shell 终端。例如,可以运行以下命令启动一个 Ubuntu 容器并进入其 shell 终端:

sudo docker run -it ubuntu

这会启动一个交互式的容器,并打开一个 shell 终端。在 shell 中,你可以运行任意 Ubuntu 系统命令。如果想要退出容器,可以使用 exit 命令或者使用快捷键 Ctrl + D 退出。

在交互模式下运行容器后,通常通过任何命令与系统交互。此外,访问是root,因此不需要sudo。容器内部所做的任何更改仅适用于该容器。

通过在提示符下运行exit命令退出容器。

ii. 查看运行的Docker Containers

sudo docker ps

这条命令会列出当前正在运行的 Docker 容器列表。

sudo docker ps -a

这条命令用于列出所有的容器,包括运行中的和已经停止的容器。输出所有现有容器及其详细信息,包括容器ID、图像、命令、创建时间、状态、端口和唯一名称。

CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                      PORTS     NAMES
ac74ef9750b0   ubuntu        "/bin/bash"   14 minutes ago   Exited (0) 6 minutes ago              cool_haslett
af9f15fbb53c   ubuntu        "/bin/bash"   31 minutes ago   Exited (0) 31 minutes ago             eloquent_nightingale
58f4bb08b829   hello-world   "/hello"      46 minutes ago   Exited (0) 46 minutes ago             competent_hypatia

sudo docker ps -l

用来显示最近创建的容器。 -l--latest的缩写,意味着显示最近创建的容器,即最后一个创建的容器。 如果没有新的容器,则不会显示任何内容。 它会显示容器的ID,所使用的镜像,容器的状态,容器创建时间以及使用的命令等信息。

CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
ac74ef9750b0   ubuntu    "/bin/bash"   18 minutes ago   Exited (0) 10 minutes ago             cool_haslett

iii. 启动Docker Container

sudo docker start [container-ID | container-name]

用来启动一个已经停止的 Docker 容器的。其中,[container-ID | container-name] 表示要启动的容器的 ID 或者名称。如果容器没有停止,则不能使用该命令。如: sudo docker start af9f15fbb53c 

iv. 进入Docker Container

sudo docker exec -it [container-ID | container-name] /bin/bash

此命令使用 docker exec 来在正在运行的容器中执行一个新的进程,这里是一个交互式的bash shell (/bin/bash)。 -it 选项允许您与容器进行交互,并且进入bash shell。

v. 停止Docker Container

sudo docker stop [container-ID | container-name]

用于停止运行中的Docker容器,需要指定要停止的容器的ID或名称。像是 sudo docker stop ac74ef9750b0 

vi. 移除Docker Container

sudo docker rm [container-ID | container-name]

这个命令将会删除这个容器的文件系统、日志等数据。如果容器正在运行中,需要先使用docker stop命令停止容器,才能使用docker rm命令删除容器。

3)Docker Volumes的使用

Docker Volume 是一种文件系统机制,允许用户保留由 Docker 容器生成和使用的数据。卷确保数据持久性,并提供了更好的选择来保存数据,因为它们不会增加 Docker 容器的大小。使用 Docker Volume,容器可以将数据存储在本地主机或云存储中,并且可以随时访问和共享这些数据,即使容器被删除或重新创建也可以保留数据。

i. 创建Docker Volume

sudo docker volume create example-volume

这个命令是在Docker中创建一个名为example-volume的卷。卷是Docker用来持久化数据的一种机制,可以将卷挂载到Docker容器中,这样容器就可以在卷中读写数据,从而实现了数据的持久化。使用卷的好处是,它可以将容器中的数据与容器本身分离开来,容器被删除时数据不会被删除,也可以被多个容器共享。

ii. 移除Docker Volume

sudo docker volume rm example-volume

这个命令是用来删除一个指定的 Docker volume。其中,[volume-name] 是要删除的 Docker volume 的名称。如果删除成功,命令不会有任何输出。如果指定的 volume 不存在,命令会输出一个错误信息。

三、后言

本文仅涉及Docker基本的内容,如需要进一步了解可参考如下内容:

1)Docker的安装与使用:How To Install Docker on Ubuntu 20.04 | phoenixNAP KB

2)Docker Image的优化:Docker Image Size - How to Keep It Small? (phoenixnap.com)

3)如何SSH到Docker:How to SSH into a Running Docker Container and Run Commands (phoenixnap.com)

4)如何更新Docker Images 和 Container:How To Update Docker Image And Container {3 Easy Steps} (phoenixnap.com)

posted @ 2023-03-18 17:37  有梦-  阅读(74)  评论(0编辑  收藏  举报