如何在 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) <docker@docker.com>" 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.container.club.tw
   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.container.club.tw/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 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告