Docker是一种应用程序,它使得在容器中运行应用程序变得简单和容易,这就像虚拟机,只有更便携、更资源友好,而且更依赖于主机操作系统。要详细介绍Docker容器的不同组件,请参阅Docker生态系统:对常见组件的介绍。
在Ubuntu 16.04上安装Docker有两种方法。一种方法是在现有的操作系统安装上安装它。另一种方法是使用一个名为Docker机器的工具,在服务器上自动安装Docker。在本教程中,您将学习如何在现有的Ubuntu 16.04安装上安装和使用它。
64位Ubuntu 16.04服务器
使用sudo特权的非根用户,Ubuntu 16.04的初始设置指南说明了如何设置这个。
注意:Docker需要一个64位版本的Ubuntu,以及一个等于或大于3.10的内核版本。默认的64位Ubuntu 16.04服务器满足这些要求。
本教程中的所有命令都应该作为非根用户运行。如果命令需要root访问权限,那么它将先于sudo。Ubuntu 16.04的初始设置指南解释了如何添加用户并给予他们sudo访问。
官方Ubuntu 16.04存储库中提供的Docker安装包可能不是最新版本。要获得最新最伟大的版本,请从官方的Docker存储库中安装Docker。本节将向您展示如何做到这一点。
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-get install python-software-properties
sudo apt-get update
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-cache policy docker-ce
您应该看到类似于下面的输出:
docker-ce:
Installed: (none)
Candidate: 17.03.1~ce-0~ubuntu-xenial
Version table:
17.03.1~ce-0~ubuntu-xenial 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
17.03.0~ce-0~ubuntu-xenial 500
500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
sudo apt-get install -y docker-ce
sudo systemctl status docker
输出应该类似如下,显示服务是活动的和运行的:
docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2016-05-01 06:53:52 CDT; 1 weeks 3 days ago
Docs: https://docs.docker.com
Main PID: 749 (docker)
默认情况下,运行docker命令需要根权限——也就是说,您必须用sudo为命令前缀。它也可以由docker组中的用户运行,它是在安装docker时自动创建的。如果您尝试运行docker命令,而无需在sudo或docker组中预先设置,您将得到这样的输出:
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.
sudo usermod -aG docker ${USER}
su - ${USER}
id -nG
sudo usermod -aG docker username
随着Docker的安装和工作,现在是熟悉命令行实用程序的时候了。使用docker包括传递一系列选项和命令,然后是参数。语法采用这种形式:docker [option] [command] [arguments]
docker
在Docker 1.11.1中,可用子命令的完整列表包括:
Output
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-subcommand --help
docker info
Docker容器是由Docker映像运行的。默认情况下,它从Docker Hub(Docker)管理的Docker registry中提取这些图像。Docker是Docker项目背后的公司。任何人都可以在Docker Hub上构建和托管他们的Docker映像,因此大多数应用程序和Linux发行版都需要在Docker Hub上运行Docker容器。
docker run hello-world
应该包括以下内容的输出应该表明Docker在正确地工作:
Output
Hello from Docker.
This message shows that your installation appears to be working correctly.
...
docker search ubuntu
该脚本将抓取Docker Hub并返回其名称与搜索字符串匹配的所有图像的列表。在这种情况下,输出将与以下类似:
Output
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating s... 3808 [OK]
ubuntu-upstart Upstart is an event-based replacement for ... 61 [OK]
torusware/speedus-ubuntu Always updated official Ubuntu docker imag... 25 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 24 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components... 23 [OK]
nickistre/ubuntu-lamp LAMP server on Ubuntu 6 [OK]
nickistre/ubuntu-lamp-wordpress LAMP on Ubuntu with wp-cli installed 5 [OK]
nuagebec/ubuntu Simple always updated Ubuntu docker images... 4 [OK]
nimmis/ubuntu This is a docker images different LTS vers... 4 [OK]
maxexcloo/ubuntu Docker base image built on Ubuntu with Sup... 2 [OK]
admiringworm/ubuntu Base ubuntu images based on the official u... 1 [OK]
...
docker pull ubuntu
docker run ubuntu
docker images
输出应该类似如下:
Output
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest c5f1cf30c96b 7 days ago 120.8 MB
hello-world latest 94df4f0ce8a4 2 weeks ago 967 B
您在前面运行的hello - world容器是一个在发出测试消息后运行和出口的容器的示例。然而,容器可能比这更有用,它们可以是交互式的。毕竟,它们与虚拟机相似,只是更加资源友好。
docker run -it ubuntu
Output
root@d9b100f2f636:/#
apt-get update
apt-get install -y nodejs
当您启动Docker映像时,您可以像使用虚拟机一样创建、修改和删除文件。您所做的更改将只适用于该容器。您可以启动并停止它,但是一旦您使用docker rm命令销毁它,更改将永远丢失。本节将向您展示如何将容器的状态保存为新的Docker映像。
exit
docker commit -m "What did you do to the image" -a "Author Name" container-id repository/new_image_name
例如:
docker commit -m "added node.js" -a "Sunday Ogwu-Chinuwa" d9b100f2f636 finid/ubuntu-nodejs
docker images
输出应该与此类似:
Output
finid/ubuntu-nodejs latest 62359544c9ba 50 seconds ago 206.6 MB
ubuntu latest c5f1cf30c96b 7 days ago 120.8 MB
hello-world latest 94df4f0ce8a4 2 weeks ago 967 B
docker ps
您将看到类似如下的输出:
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7c79cc556dd ubuntu "/bin/bash" 3 hours ago Up 3 hours silly_spence
docker ps -a
docker ps -l
docker stop container-id
从现有映像创建新映像后的下一个逻辑步骤是,与您的朋友、整个世界的Docker Hub或其他您可以访问的Docker注册表共享它。要将图像推送到Docker中心或任何其他Docker注册表,您必须在那里有一个帐户。
docker login -u docker-registry-username
docker login -u docker-registry-username
它需要一些时间来完成,当完成时,输出将类似如下:
Output
The push refers to a repository [docker.io/finid/ubuntu-nodejs]
e3fbbfb44187: Pushed
5f70bf18a086: Pushed
a3b5c80a4eba: Pushed
7f18b442972b: Pushed
3ce512daaf78: Pushed
7aae4540b42d: Pushed
...
如果一个push尝试导致了这种错误,那么您可能没有登录:
Output
The push refers to a repository [docker.io/finid/ubuntu-nodejs]
e3fbbfb44187: Preparing
5f70bf18a086: Preparing
a3b5c80a4eba: Preparing
7f18b442972b: Preparing
3ce512daaf78: Preparing
7aae4540b42d: Waiting
unauthorized: authentication required