如何在Ubuntu中安装Docker和运行 Docker容器

Docker是一种开源且流行的操作系统级虚拟化(俗称“容器化”)技术,主要在Linux和Windows上运行。 Docker使用容器可以更轻松地创建,部署和运行应用程序。

使用容器,开发人员(和系统管理员)可以使用运行应用程序所需的一切来打包应用程序 - 代码,运行时,库,环境变量和配置文件,并将它们作为一个包发送出去。 是的,真是太好了!

在本文中,我们将向您展示如何安装Docker CE(Community Edition),在Ubuntu发行版上创建和运行Docker容器。

在Ubuntu中安装Docker CE(Community Edition)

1.要安装Docker CE,首先,您需要使用以下命令从系统中删除旧版本的Docker,称为docker,docker.io或docker-engine。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo apt-get remove docker docker-engine docker.io containerd runc

如何在Ubuntu中安装Docker和运行 Docker容器_linux

2.接下来,您需要设置Docker存储库以使用以下命令从存储库安装和更新Docker。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo apt-get update

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

OK

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

如何在Ubuntu中安装Docker和运行 Docker容器_ubuntu_02

3.使用以下命令更新apt软件包索引并安装最新版本的Docker CE。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo apt-get update

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo apt-get install docker-ce docker-ce-cli containerd.io

如何在Ubuntu中安装Docker和运行 Docker容器_f5_03

4.成功安装Docker CE软件包后,该服务应自动启动并自动启用以在系统引导时启动,您可以使用以下命令检查其状态。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo systemctl status docker

[sudo] linuxidc 的密码:

● docker.service - Docker Application Container Engine

  Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: e

  Active: active (running) since Thu 2019-03-07 20:23:49 CST; 16min ago

    Docs: https://docs.docker.com

 Main PID: 15355 (dockerd)

    Tasks: 9

  CGroup: /system.slice/docker.service

          └─15355 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/contai

如何在Ubuntu中安装Docker和运行 Docker容器_f5_04

5.最后,通过运行hello-world映像验证Docker CE是否已正确安装。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker run hello-world

Unable to find image 'hello-world:latest' locally

latest: Pulling from library/hello-world

1b930d010525: Pull complete

Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535

Status: Downloaded newer image for hello-world:latest

Hello from Docker!

This message shows that your installation appears to be working correctly.

如何在Ubuntu中安装Docker和运行 Docker容器_ubuntu_05

在Ubuntu中学习基本的Docker命令

6.要获取有关Docker的信息,请运行以下命令。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker info

Containers: 1

 Running: 0

 Paused: 0

 Stopped: 1

Images: 1

Server Version: 18.09.3

Storage Driver: overlay2

 Backing Filesystem: extfs

 Supports d_type: true

 Native Overlay Diff: true

Logging Driver: json-file

Cgroup Driver: cgroupfs

Plugins:

 Volume: local

 Network: bridge host macvlan null overlay

 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog

Swarm: inactive

Runtimes: runc

Default Runtime: runc

Init Binary: docker-init

containerd version: e6b3f5632f50dbc4e9cb6288d911bf4f5e95b18e

runc version: 6635b4f0c6af3810594d2770f662f34ddc15b40d

init version: fec3683

Security Options:

 apparmor

 seccomp

  Profile: default

Kernel Version: 4.18.0-15-generic

Operating System: Ubuntu 18.04.2 LTS

OSType: linux

Architecture: x86_64

CPUs: 1

Total Memory: 2.847GiB

Name: linuxidc

ID: GOFC:PMVS:TLNX:K2UZ:4L5U:L46W:BQ5C:FVUE:7P76:XVP2:SAEZ:RAET

Docker Root Dir: /var/lib/docker

Debug Mode (client): false

Debug Mode (server): false

Registry: https://index.docker.io/v1/

Labels:

Experimental: false

Insecure Registries:

 127.0.0.0/8

Live Restore Enabled: false

Product License: Community Engine

WARNING: No swap limit support

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$

如何在Ubuntu中安装Docker和运行 Docker容器_ubuntu_06

7.要获取有关Docker版本的信息,请运行以下命令。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker version

Client:

 Version:          18.09.3

 API version:      1.39

 Go version:        go1.10.8

 Git commit:        774a1f4

 Built:            Thu Feb 28 06:53:11 2019

 OS/Arch:          linux/amd64

 Experimental:      false

Server: Docker Engine - Community

 Engine:

  Version:          18.09.3

  API version:      1.39 (minimum version 1.12)

  Go version:      go1.10.8

  Git commit:      774a1f4

  Built:            Thu Feb 28 05:59:55 2019

  OS/Arch:          linux/amd64

  Experimental:    false

如何在Ubuntu中安装Docker和运行 Docker容器_docker_07

8.要获取所有可用Docker命令的列表,请在终端上运行docker。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ docker

如何在Ubuntu中安装Docker和运行 Docker容器_centos_08

在Ubuntu中下载Docker镜像

9.要运行Docker容器,首先需要从Docker Hub下载映像 - 从其存储库提供免费映像。

例如,要下载名为CentOS 7的Docker镜像,请发出以下命令。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker search centos

如何在Ubuntu中安装Docker和运行 Docker容器_docker_09

10.根据您的要求确定要运行的映像后,通过运行以下命令在本地下载(在这种情况下,将下载并使用CentOS映像)。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker pull centos

Using default tag: latest

latest: Pulling from library/centos

a02a4930cb5d: Pull complete

Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426

Status: Downloaded newer image for centos:latest

如何在Ubuntu中安装Docker和运行 Docker容器_linux_10

11.要列出主机上所有可用的Docker映像,请运行以下命令。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker images

REPOSITORY          TAG                IMAGE ID            CREATED            SIZE

hello-world        latest              fce289e99eb9        2 months ago        1.84kB

centos              latest              1e1148e4cc2c        3 months ago        202MB

如何在Ubuntu中安装Docker和运行 Docker容器_ubuntu_11

12.如果您不再需要Docker镜像,可以使用以下命令将其删除。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker rmi centos

在Ubuntu中运行Docker容器

13.为了创建和运行Docker容器,首先需要在下载的CentOS映像中运行命令,因此基本命令是使用cat命令检查容器内的分发版本文件,如图所示。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker run centos cat /etc/issue

\S

Kernel \r on an \m

如何在Ubuntu中安装Docker和运行 Docker容器_ubuntu_12

14.要再次运行容器,首先需要通过运行以下命令来获取Container ID或Name,该命令将显示正在运行和已停止的容器的列表:

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker ps -l

CONTAINER ID        IMAGE              COMMAND            CREATED            STATUS                      PORTS              NAMES

fbca5da756f0        centos              "cat /etc/issue"    24 seconds ago      Exited (0) 22 seconds ago                      determined_tesla

如何在Ubuntu中安装Docker和运行 Docker容器_centos_13

15.获取Container ID或Name后,可以使用以下命令启动容器:

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker start fbca5da756f0

fbca5da756f0

或者

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker start determined_tesla

determined_tesla

这里,字符串fbca5da756f0表示容器ID,determined_tesla表示容器名称。

如何在Ubuntu中安装Docker和运行 Docker容器_centos_14

16.通过指定Container ID或Name来停止正在运行的容器运行docker stop命令。

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker stop fbca5da756f0

fbca5da756f0

或者

linuxidc@linuxidc:~/www.linuxidc.com/Linux公社 -$ sudo docker stop determined_tesla

determined_tesla

如何在Ubuntu中安装Docker和运行 Docker容器_docker_15

17.更好的方法是,您不必记住容器ID,就是使用--name选项为您创建的每个容器定义唯一的名称,如图所示。

docker run --name myname centos cat /etc/issue

18.要将Linux命令连接并运行到容器中,请发出以下命令。

$ docker run -it centos bash

19.要从正在运行的容器会话中退出并返回主机,必须键入exit命令,如下所示。

$ exit

这就是基本的容器操作