如何在 Ubuntu 16.04 上安装和使用 Docker


Docker 是一个开源项目,它自动化了应用程序在软件容器中的部署。容器允许开发人员打包所有项目资源,例如库、依赖项、资产等。Docker 使用 **Go 编程** 语言编写,并由 **Dotcloud** 开发。它基本上是一个容器引擎,它使用 Linux 内核功能(如命名空间和控制组)在操作系统之上创建容器,并自动化容器上的应用程序部署。

安装 Docker

在安装 Docker 之前,需要更新软件包。要更新软件包,请使用以下命令:

$ sudo apt-get update

使用以下命令将官方 Docker 存储库的 GPG 密钥添加到系统中:

$ sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D

示例输出应如下所示:

Executing: /tmp/tmp.akuLSrhQz8/gpg.1.sh --keyserver
hkp://p80.pool.sks-keyservers.net:80
--recv-keys
58118E89F3A912897C070ADBF76221572C52609D
gpg: requesting key 2C52609D from hkp server p80.pool.sks-keyservers.net
gpg: key 2C52609D: public key "Docker Release Tool (releasedocker) <[email protected]>" imported
gpg: Total number processed: 1
gpg:             imported: 1 (RSA: 1)

要将 Docker 存储库添加到 APT 源,请使用以下命令:

$ echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list

要使用新添加的存储库中的 Docker 软件包更新软件包数据库,请使用以下命令:

$ sudo apt-get update

确保您即将从 Docker 存储库而不是默认的 Ubuntu 存储库进行安装。要验证它,请使用以下命令:

$ apt-cache policy docker-engine

示例输出应如下所示:

docker-engine:
   Installed: (none)
   Candidate: 1.11.1-0~xenial
   Version table:
   1.11.1-0~xenial 500
      500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages
   1.11.0-0~xenial 500
      500 https://apt.dockerproject.org/repo ubuntu-xenial/main amd64 Packages

请注意,docker-engine 未安装,要安装 Docker-engine,请使用以下命令:

$ sudo apt-get install -y docker-engine

示例输出应如下所示:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
   apport-hooks-elementary contractor libgda-5.0-4 libgda-5.0-common
   libgranite-common libgranite3 libgsignon-glib1 libindicate5 libnoise-core0
   libtagc0 mysql-server-5.7 mysql-server-core-5.7
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
   aufs-tools cgroupfs-mount
The following NEW packages will be installed:
   aufs-tools cgroupfs-mount docker-engine
0 upgraded, 3 newly installed, 0 to remove and 212 not upgraded.
Need to get 14.6 MB of archives.
After this operation, 73.7 MB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial/universe amd64 aufs-tools amd64 1:3.2+20130722-1.1ubuntu1 [92.9 kB]
..............................................................................................

要检查 Docker 是否已启动,请使用以下命令:

$ sudo systemctl status docker

示例输出应如下所示:

docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: e
   Active: active (running) since Mon 2016-05-23 10:36:08 IST; 5min ago
   Docs: https://docs.docker.net.cn
   Main PID: 14186 (docker)
   Tasks: 20 (limit: 512)
   Memory: 21.5M
   CPU: 385ms
   CGroup: /system.slice/docker.service
            ├─14186 /usr/bin/docker daemon -H fd://
            └─14204 docker-containerd -l /var/run/docker/libcontainerd/docker-con

May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.169666150+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.261017824+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.469630404+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.829495384+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.830931516+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.831009718+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.831055091+05:30"
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.831113464+05:30"
May 23 10:36:08 linux systemd[1]: Started Docker Application Container Engine.
May 23 10:36:08 linux docker[14186]: time="2016-05-23T10:36:08.855330148+05:30

要启动 Docker 服务,请使用以下命令:

$ sudo service docker start

要查看 Docker 的所有可用子命令,请使用以下命令:

$ docker

示例输出应如下所示:

Usage: docker [OPTIONS] COMMAND [arg...]
   docker daemon [ --help | ... ]
   docker [ --help | -v | --version ]

A self-sufficient runtime for containers.

Options:
   --config=~/.docker Location of client config files
   -D, --debug Enable debug mode
   -H, --host=[] Daemon socket(s) to connect to
   -h, --help Print usage
   -l, --log-level=info Set the logging level
   --tls Use TLS; implied by --tlsverify
   --tlscacert=~/.docker/ca.pem Trust certs signed only by this CA
   --tlscert=~/.docker/cert.pem Path to TLS certificate file
   --tlskey=~/.docker/key.pem Path to TLS key file
   --tlsverify Use TLS and verify the remote
   -v, --version Print version information and quit

Commands:
   attach             Attach 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 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 a container or image
   kill               Kill a running container
   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
   network            Manage Docker networks
   pause              Pause all processes within a container
   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 a container
   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
   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 a running container
   tag                Tag an image into a repository
   top                Display the running processes of a container
   unpause            Unpause all processes within a container
   update             Update configuration of one or more containers
   version            Show the Docker version information
   volume             Manage Docker volumes
   wait               Block until a container stops, then print its exit code

您可以使用带 search 子命令的 docker 命令搜索 Docker Hub 上可用的镜像。

$ sudo docker search ubuntu

示例输出应如下所示:

NAME
AUTOMATED                         DESCRIPTION                                     STARS    OFFICIAL          
ubuntu                            Ubuntu is a Debian-based Linux operating s...    3953       [OK]
ubuntu-upstart                   Upstart is an event-based replacement for ...       62       [OK]
rastasheep/ubuntu-sshd
[OK]                           Dockerized SSH service, built on top of of...          26    
torusware/speedus-ubuntu
[OK]                            Always updated official Ubuntu docker imag...       26
ubuntu-debootstrap
[OK]                           debootstrap --variant=minbase --components...       24          [OK]
nickistre/ubuntu-lamp
[OK]                              LAMP server on Ubuntu 6 [OK]
nickistre/ubuntu-lamp-wordpress
[OK]                               LAMP on Ubuntu with wp-cli installed 5 [OK]
nuagebec/ubuntu
[OK]                              Simple always updated Ubuntu docker images...       4
nimmis/ubuntu  
[OK]                               This is a docker images different LTS vers...       4
maxexcloo/ubuntu
[OK]                               Docker base image built on Ubuntu with Sup...       2
partlab/ubuntu
[OK]                              Simple Ubuntu docker images.                         1
darksheer/ubuntu
[OK]                               Base Ubuntu Image -- Updated hourly                   1
admiringworm/ubuntu
[OK]                               Base ubuntu images based on the official u...       1
jordi/ubuntu
[OK]                            UbuntuBase Image                                        1
webhippie/ubuntu
[OK]                              Docker images for ubuntu                            0
teamrock/ubuntu TeamRock's
[OK]                               Ubuntu image configured with AW...                0
esycat/ubuntu  
[OK]                                Ubuntu LTS                                        0
life360/ubuntu
[OK]                              Ubuntu is a Debian-based Linux operating s...       0
widerplan/ubuntu
[OK]                              Our basic Ubuntu images.                         0
konstruktoid/ubuntu
[OK]                              Ubuntu base image                                  0
lynxtp/ubuntu
[OK]                              https://github.com/lynxtp/docker-ubuntu             0
datenbetrieb/ubuntu  
[OK]                             custom flavor of the official ubuntu base ...       0
ustclug/ubuntu
[OK]                              ubuntu image for docker with USTC mirror             0
uvatbc/ubuntu  
[OK]                               Ubuntu images with unprivileged user             0
rallias/ubuntu  
[OK]                                  Ubuntu with the needful                         0

要查看已下载到计算机的镜像,请使用以下命令:

$ sudo docker images

示例输出应如下所示:

REPOSITORY          TAG                IMAGE ID                CREATED          SIZE
hello-world       latest                94df4f0ce8a4          3 weeks ago       967 B

要运行 Docker 容器,请使用以下命令:

$ sudo docker run -it hello-world

以上命令运行 hello-word 容器。示例输出应如下所示:

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.
   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 Hub account:
   https://hub.docker.com

For more examples and ideas, visit:
   https://docs.docker.net.cn/engine/userguide/

要列出 Docker 容器,请使用以下命令:

$ sudo docker ps

示例输出应如下所示:

CONTAINER ID PORTS         IMAGE        COMMAND                CREATED                         STATUS                             NAMES
f7c79cc556dd              ubuntu        "/bin/bash"          3 hours ago                     Up 3 hours                         silly_spence

通过本文,您将能够理解如何在 Ubuntu 16.04 上安装和使用 Docker,我们将推出更多基于 Linux 的技巧和提示。请继续关注!

更新于: 2020年1月20日

260 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告