如何在Ubuntu 20.04上安装Docker

时间:2020-03-05 15:25:20  来源:igfitidea点击:

Docker是一个开源平台,使软件开发人员能够构建,部署和管理容器化应用程序。
这意味着将应用程序包装到容器中,该容器是源代码,库和在任何操作系统中运行所需的其他依赖项的组合。

此外,使用容器时,Docker会使更简单,更顺畅和更安全。

在本教程中,我将通过在Ubuntu 20上安装Docker的所有步骤。
04,并在使用Docker容器/图像时学习基本概念。

安装Docker.

在Ubuntu 20的官方软件存储库中.04,默认情况下,Docker安装包可用。
但是,它不是最新版本。
从官方Docker存储库安装最新版本的Docker是一项建议。

首先,我们需要更新当前包列表并安装准备工作包,以便通过运行以下命令将新存储库添加到系统:

$sudo apt update
$sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent

接下来,我们必须导入Docker存储库的GPG密钥:

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

然后,将Docker存储库添加到系统上的APT源:

$sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

运行以下命令以更新新添加的存储库:

$sudo apt update

确保我们将从其存储库而不是Ubuntu存储库安装Docker:

$apt-cache policy docker-ce

我们将看到输出是这样的:

docker-ce:
  Installed: (none)
  Candidate: 5:19.03.11~3-0~ubuntu-focal
  Version table:
     5:19.03.11~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:19.03.10~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:19.03.9~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages

上述输出表示从ubuntu 20的Docker Repo中的"Docker-CE"。
04将在系统上安装。

最后,安装Docker-CE(社区版):

$sudo apt install docker-ce

要验证是否已成功安装并运行odcker,请运行以下命令:

$sudo systemctl status docker

输出将显示Docker服务处于活动状态(正在运行):

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2017-06-17 15:29:48 +07; 8min ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 136640 (dockerd)
      Tasks: 17
     Memory: 50.8M
     CGroup: /system.slice/docker.service
             └─136640 /usr/bin/dockerd -H fd://--containerd=/run/containerd/containerd.sock
...

Docker服务状态

我们刚刚在Ubuntu机器上成功安装了Docker。
我们将深入潜入在本教程的以下部分中使用Docker命令的方式。

执行没有sudo特权的Docker

默认情况下,我们必须使用sudo权限或者docker组中的用户运行docker命令。

如果我们尝试在没有sudo的情况下运行docker命令或者使用docker组的权限,则会收到这样的错误:

docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

解决方法正在将用户添加到"Docker"组:

$sudo usermod -aG docker <user-name>

注销当前会话然后再次登录以应用成员资格。
通过运行命令,验证用户是否在"docker"组中:

$groups
ubuntu sudo docker

从现在开始,我们可以在没有'sudo'的情况下运行docker命令。

Docker命令

本节将解释如何使用docker命令。
在详细介绍之前,让我们来看看'docker'命令的语法:

$docker [options] [sub-commands] [arguments]

要列出Docker的所有可用子命令,请运行:

$docker

需要记住许多选项,子命令和参数。
让我们在本教程的以下部分中深入研究其中一些部分。

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  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 tar包 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.

Docker镜像

Docker镜像由源代码,库和应用程序所需的所有必要依赖项组成。
Docker图像可以从头开始构建,但我们可以从容器注册表中按下这些图像。
注册表是我们可以其中存储图像并使用唯一名称下载它们。
默认情况下,Docker的容器注册表是Docker集线器。

要检查是否可以从Docker Hub中提取图像,我们可以运行以下命令:

$docker run hello-world

正如我们在以下输出中看到的那样:Docker无法在本地找到"Hello-World"图像,它从Docker Hub中删除了图像。
下载图像后,Docker从该图像创建一个容器。
将执行容器中的应用程序并显示消息:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
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
...

要搜索Docker Hub上的图像,我们可以使用"搜索"子命令使用"docker"命令。
假设我们要搜索"Alpine"图像,请运行:

$docker search alpine

Docker搜索命令

如我们所见,输出返回名称具有"ALPINE"的所有图像的列表。

此外,我们可以通过使用图像的名称运行命令"Docker Image Pull"来下载Docker镜像。
例如:

$docker image pull alpine

根据互联网的速度,下载可能需要一分钟。

Using default tag: latest
latest: Pulling from library/alpine
df20fa9351a1: Pull complete 
Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

此外,如果要列出所有下载镜像,请运行以下命令:

$docker image ls

输出将显示图像的细节包括:存储库,标记,图像ID,创建的时间和文件大小。

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              a24bb4013296        2 weeks ago         5.57MB
hello-world         latest              bf756fb1ae65        5 months ago        13.3kB

有时,我们需要删除Docker镜像,可以运行命令:

$docker image rm <image-name>

或者

$docker image rm <image-id>

Docker容器

容器是Docker图像的运行实例。
容器执行单个应用程序或者过程的运行时。
我们可以通过运行"Docker Container"子命令与容器进行交互并启动/停止或者删除它。

例如,为了基于"ubuntu"图像,启动Docker容器,运行:

ubuntu@theitroad:~$docker container run ubuntu

从Docker Hub下载Ubuntu的较新图像并启动Ubuntu容器后,它会返回系统命令提示符,因为启动后立即停止Ubuntu容器。

Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
a4a2a29f9ba4: Pull complete 
127c9761dcba: Pull complete 
d13bf203e905: Pull complete 
4039240d2e0b: Pull complete 
Digest: sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce
Status: Downloaded newer image for ubuntu:latest
ubuntu@theitroad:~$

为了通过命令行与Ubuntu容器与Ubuntu容器进行交互,我们可以使用-it选项启动容器:

$docker container run -it ubuntu

现在,你已经进入了容器并访问了它的互动shell 。

root@45cc68d4395e:/#

到目前为止,我们已经在Ubuntu系统上运行了许多容器。
要列出所有活动(运行)容器,请运行以下命令:

$docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

结果返回空列表,因此没有任何容器运行。
如果要列出所有容器:活动和非活动,请运行:

$docker container ls -a

输出类似于:

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
45cc68d4395e        ubuntu              "/bin/bash"         14 minutes ago      Exited (0) 11 minutes ago                         stoic_dhawan
fc91e50d223e        ubuntu              "/bin/bash"         26 minutes ago      Exited (0) 26 minutes ago                         sleepy_ritchie
ce0d81682913        alpine              "/bin/bash"         26 minutes ago      Created                                           trusting_kowalevski
4291477f5ac2        alpine              "/bin/sh"           27 minutes ago      Exited (130) 27 minutes ago                       priceless_wozniak
86c0fd44a4f4        alpine              "/bin/sh"           28 minutes ago      Exited (0) 28 minutes ago                         objective_agnesi
3fdcaa196fca        hello-world         "/hello"            31 minutes ago      Exited (0) 31 minutes ago                         eloquent_rhodes

一旦要删除/删除容器,运行:

$docker container rm <container-id>