软件。
如何在Ubutu 18.04上安装和使用Docker
介绍
Docker是一个简化了在容器中管理应用程序过程的应用程序。容器允许在资源隔离的进程中运行应用程序。与虚拟机(vm等)类似,但是容器更便携,更友好,更依赖于主机操作系统。
在本教程中,你将在Ubuntu 18.04上安装并使用Docker Community Edition (CE)。你将安装Docker本身,处理容器和镜像,并将镜像推送到Docker存储库。
准备
如下:
- 已配置好的Ubuntu18.04包括:拥有sudo权限的用户和防火墙。
- 如果你希望创建自己的镜像并将其推送到Docker Hub,请在Docker Hub上注册一个帐户.
具体步骤
步骤1—安装Docker
官方Ubuntu存储库中可用的Docker安装包可能不是最新版本。为了确保获得最新版本,我们将从官方Docker存储库安装Docker。为此,我们将添加一个新的包源,从Docker中添加GPG键以确保下载是有效的,然后安装包。
首先,更新现有的包列表
sudo apt update
接下来,安装一些必要的包
sudo apt install apt-transport-https ca-certificates curl software-properties-common
然后将官方Docker存储库的GPG密钥添加到系统中
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
将Docker存储库添加到APT源
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
接下来,使用来自新添加的repo的Docker包更新包数据库
sudo apt update
确保你要从Docker repo而不是默认的Ubuntu repo中安装
apt-cache policy docker-ce
你将看到这样的输出,尽管Docker的版本号可能不同
Output of apt-cache policy docker-ce
docker-ce:
Installed: (none)
Candidate: 18.03.1~ce~3-0~ubuntu
Version table:
18.03.1~ce~3-0~ubuntu 500
500 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages
请注意,Docker -ce没有安装,但是安装的候选程序来自Ubuntu 18.04(仿生)的Docker存储库。
最后,安装Docker
sudo apt install docker-ce
输出应该类似于以下内容,显示服务是活动的并正在运行
Output
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2018-07-05 15:08:39 UTC; 2min 55s ago
Docs: https://docs.docker.com
Main PID: 10096 (dockerd)
Tasks: 16
CGroup: /system.slice/docker.service
├─10096 /usr/bin/dockerd -H fd://
└─10113 docker-containerd --config /var/run/docker/containerd/containerd.toml
安装Docker现在不仅提供Docker服务(守护进程),还提供Docker命令行实用程序或Docker客户机。我们将在本教程的后面讨论如何使用docker命令。
步骤2—在不使用Sudo的情况下执行Docker命令(可选)
默认情况下,docker命令只能由root用户或docker组中的用户运行,docker组是在docker安装过程中自动创建的。如果你试图在不使用sudo作为前缀或不属于docker组的情况下运行docker命令,你将得到如下输出
Output
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.
如果希望在运行docker命令时避免输入sudo,请将你的用户名添加到docker组:
sudo usermod -aG docker ${USER}
要应用新的组成员关系,请从服务器注销并返回,或输入以下命令:
su - ${USER}
系统会提示你输入用户密码以继续。
输入以下命令确认你的用户现在已经添加到docker组中
id -nG
Output
sammy sudo docker
如果需要向未登录的docker组添加用户,请显式声明该用户名
sudo usermod -aG docker username
本文的其余部分假设你作为docker组中的用户运行docker命令。如果你选择不这样做,请在命令前面加上sudo。
步骤3—Docker命令
使用docker包括向它传递一系列选项和命令以及参数。语法如下:
docker [option] [command] [arguments]
要查看所有可用的子命令,请输入:
docker
从Docker 18开始,可用子命令的完整列表包括
Output
attach Attach local standard input, output, and error streams 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 to files or directories 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 Docker objects
kill Kill one or more running containers
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
pause Pause all processes within one or more containers
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 one or more containers
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 (streamed to STDOUT by default)
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 one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
要查看特定命令可用的选项,请输入
docker docker-subcommand --help
要查看关于Docker的全系统信息,请使用
docker info
步骤4—使用Docker镜像
Docker容器是根据Docker镜像构建的。默认情况下,Docker从Docker Hub (Docker Hub是由Docker管理的Docker注册表)中提取这些镜像,Docker是Docker项目背后的公司。任何人都可以在Docker Hub上托管自己的Docker镜像,所以你需要的大多数应用程序和Linux发行版都将在那里托管镜像。
要检查是否可以从Docker Hub访问和下载镜像,请输入:
docker run hello-world
Output
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9bb5a5d4561a: Pull complete
Digest: sha256:3e1764d0f546ceac4565547df2ac4907fe46f007ea229fd7ef2718514bcec35d
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
Docker最初无法在本地找到hello-world镜像,因此它从Docker Hub(默认存储库)下载了该镜像。下载镜像之后,Docker从镜像创建一个容器,并在容器中执行应用程序,显示消息。通过使用Docker命令和search子命令,可以搜索Docker Hub上可用的镜像。例如,要搜索Ubuntu镜像,输入:
docker search ubuntu
该脚本将抓取Docker Hub并返回名称与搜索字符串匹配的所有镜像的列表。在这种情况下,输出将与此类似:
Output
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 7917 [OK]
dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC 193 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 156 [OK]
ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 93 [OK]
ubuntu-upstart Upstart is an event-based replacement for th… 87 [OK]
neurodebian NeuroDebian provides neuroscience research s… 50 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components=m… 38 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 36 [OK]
nuagebec/ubuntu Simple always updated Ubuntu docker images w… 23 [OK]
tutum/ubuntu Simple Ubuntu docker images with SSH access 18
i386/ubuntu Ubuntu is a Debian-based Linux operating sys… 13
ppc64le/ubuntu Ubuntu is a Debian-based Linux operating sys… 12
1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 10 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mariadb-10 ubuntu-16-nginx-php-phpmyadmin-mariadb-10 6 [OK]
eclipse/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 6 [OK]
codenvy/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, … 4 [OK]
darksheer/ubuntu Base Ubuntu Image -- Updated hourly 4 [OK]
1and1internet/ubuntu-16-apache ubuntu-16-apache 3 [OK]
1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 3 [OK]
1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK]
pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc… 1
1and1internet/ubuntu-16-healthcheck ubuntu-16-healthcheck 0 [OK]
pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 0
smartentry/ubuntu ubuntu with smartentry 0 [OK]
ossobv/ubuntu
...
在官方专栏中,OK表示由项目背后的公司构建和支持的形象。一旦确定了要使用的镜像,就可以使用pull
子命令将其下载到计算机。
docker pull ubuntu
你将看到以下输出:
Output
Using default tag: latest
latest: Pulling from library/ubuntu
6b98dfc16071: Pull complete
4001a1209541: Pull complete
6319fc68c576: Pull complete
b24603670dc3: Pull complete
97f170c87c6f: Pull complete
Digest: sha256:5f4bdc3467537cbbe563e80db2c3ec95d548a9145d64453b06939c4592d67b6d
Status: Downloaded newer image for ubuntu:latest
下载镜像之后,可以使用已下载镜像和run子命令运行容器。正如你在hello-world示例中看到的,如果在使用run子命令执行docker时没有下载镜像,那么docker客户机将首先下载镜像,然后使用它运行容器。
要查看已下载到计算机的镜像,请输入:
docker images
输出应该类似如下所示
Output
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 113a43faa138 4 weeks ago 81.2MB
hello-world latest e38bc07ac18e 2 months ago 1.85kB
正如你将在本教程后面看到的,可以修改用于运行容器的镜像并使用它们生成新的镜像,然后可以将这些镜像上传到Docker Hub或其他Docker注册中心.
步骤5—运行一个Docker容器
你在上一步中运行的hello-world容器是一个在发出测试消息后运行和退出的容器示例。容器可能比这有用得多,而且它们可以是交互式的。毕竟,它们与虚拟机相似,只是资源更友好。例如,让我们使用Ubuntu的最新镜像运行一个容器。i和-t开关的组合提供了对容器的交互式shell访问:
docker run -it ubuntu
你的命令提示符应该更改,以反映你现在正在容器中工作的事实,并且应该采用这种形式:
Output
root@d9b100f2f636:/#
注意命令提示符中的容器id。在本例中,它是d9b100f2f636。稍后你需要该容器ID来标识想要删除它的容器。
现在你可以在容器中运行任何命令。例如,让我们更新容器中的包数据库。你不需要在任何命令前面加上sudo前缀,因为你是作为root用户在容器中操作的:
apt update
接着安装任意应用,让我们安装Node.js:
apt install nodejs
这个容器中的Node.js来自Ubuntu官方库。安装完成后,验证Node.js已安装:
node -v
你将看到终端中显示的版本号:
Output
v8.10.0
你在容器内所做的任何更改只适用于该容器。
要退出容器,请在提示符处输入exit
。
步骤6—管理Docker容器
在使用Docker一段时间后,你的计算机上将有许多活动(运行)和非活动容器。要查看正在运行的,请使用
docker ps
你将看到与下面类似的输出
Output
CONTAINER ID IMAGE COMMAND CREATED
在本教程中,你启动了两个容器;一个来自hello-world镜像,另一个来自ubuntu镜像。这两个容器都不再运行,但是它们仍然存在于你的系统中。
要查看所有活动和非活动容器:
docker ps -a
你将看到类似这样的输出:
d9b100f2f636 ubuntu "/bin/bash" About an hour ago Exited (0) 8 minutes ago sharp_volhard
01c950718166 hello-world "/hello" About an hour ago Exited (0) About an hour ago festive_williams
要查看你创建的最新容器:
docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9b100f2f636 ubuntu "/bin/bash" About an hour ago Exited (0) 10 minutes ago sharp_volhard
要启动停止的容器,请使用docker start
,后面跟着容器ID或容器名称。让我们以d9b100f2f636的ID启动基于ubuntu的容器
docker start d9b100f2f636
容器将启动,你可以使用docker ps查看其状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9b100f2f636 ubuntu "/bin/bash" About an hour ago Up 8 seconds sharp_volhard
要停止正在运行的容器,请使用docker stop
,后面跟着容器ID或名称。这次,我们将使用Docker分配给容器的名称,即sharp_volhard:
docker stop sharp_volhard
一旦你决定不再需要容器,使用docker rm
命令删除它,再次使用容器ID或名称。使用docker ps -a
命令查找与hello-world镜像关联的容器的容器ID或名称,并删除它。
docker rm festive_williams
你可以启动一个新的容器,并使用--name
为其命名。你还可以使用--rm
来创建一个容器,该容器在停止时将自己移除。有关这些选项和其他选项的更多信息,请参见docker run help
命令。
容器可以转换为镜像,你可以使用镜像构建新的容器。让我们看看它是如何工作的。
步骤7—将容器中的更改提交到Docker镜像
启动Docker镜像时,可以像使用虚拟机一样创建、修改和删除文件。你所做的更改将仅应用于该容器。你可以启动和停止它,但是一旦使用docker rm
命令销毁它,更改将永远丢失。
本节将向你展示如何将容器的状态保存为新的Docker镜像。
在安装节点。在Ubuntu容器中,你现在有了一个运行在j镜像上的容器,但是这个容器与你用来创建它的镜像不同。但是你可能想要重用这个Node.js容器作为以后新镜像的基础。
然后使用以下命令将更改提交到一个新的Docker镜像实例。
docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
-m开关用于提交消息,它帮助你和其他人了解你所做的更改,而-a用于指定作者。container_id是你在本教程前面启动交互式Docker会话时注意到的一个。除非你在Docker Hub上创建了其他存储库,否则存储库通常是你的Docker Hub用户名。
例如,对于容器ID为d9b100f2f636的用户sammy,命令将是
docker commit -m "added Node.js" -a "sammy" d9b100f2f636 sammy/ubuntu-nodejs
提交镜像时,新镜像将在本地保存在计算机上。
再次列出Docker镜像将显示新镜像,以及它派生自的旧镜像
docker images
你将看到这样的输出:
Output
REPOSITORY TAG IMAGE ID CREATED SIZE
sammy/ubuntu-nodejs latest 7c1f35226ca6 7 seconds ago 179MB
ubuntu latest 113a43faa138 4 weeks ago 81.2MB
hello-world latest e38bc07ac18e 2 months ago 1.85kB
在本例中,ubuntu-nodejs是新镜像,它是从Docker Hub中现有的ubuntu镜像衍生出来的。大小的差异反映了所做的更改。在本例中,更改是安装了Node.js。因此,下次需要使用预装了Node.js的Ubuntu运行容器时,只需使用此镜像即可。
你还可以从Dockerfile构建镜像,这使你可以在新镜像中自动安装软件。