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

如何在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)

To add the Docker repository to APT sources, use the following command –

$ 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.com>
   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

你可以通过使用带有搜索子命令的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

To see the images that have been downloaded to your computer, use the following command –

$ 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.com/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的技巧和提示。继续阅读!

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程