在本教程中,牛奇网将向您介绍如何在 CentOS 7 安装和使用 Docker。本教程基于 64 位的 CentOS 7 操作系统,且使用具有 sudo 权限的非 root 用户。Docker 需要安装在 64 位的 CentOS 7上, 并且内核版本不能低于 3.10 。
第 1 步:安装 Docker
CentOS 7 官方软件库中也提供 Docker 的安装包,但是有可能不是最新版本。要安装最新版本的 Docker,需要从官方的 Docker 存储库安装。
首先更新软件包列表:
sudo yum check-update
运行下面的命令,添加 Docker 官方存储库,下载并安装最新版本的 Docker:
curl -fsSL https://get.docker.com/ | sh
安装完成后,启动 Docker 守护进程:
sudo systemctl start docker
验证其是否正常运行:
sudo systemctl status docker
输出类似于下面的内容,表明该服务处于活动状态:
Output
● 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)
最后,确保它在服务器启动时自动启动:
sudo systemctl enable docker
第 2 步 :不使用 Sudo 执行 Docker 命令(可选)
默认情况下,运行 docker
命令需要 root 权限,也就是说必须在命令前加上 sudo
。如果不加 sudo执行命令,可能会返回下面的错误提示:
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 $(whoami)
注销该用户并重新登录即可生效。
如果要将用户添加到未登录的 docker 组中,可使用以下命令声明用户名:
sudo usermod -aG docker username
在加入 docker 用户组后,就不需要在执行 docker 命令时添加 sudo 了。
第 3 步 :使用 Docker 命令
安装 Docker 后,我们就需要开始使用 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
第 4 步:使用 Docker 镜像
默认情况下,Docker 从 Docker Hub 拉取镜像。任何人都可以在 Docker Hub 上构建和托管自己的 Docker 镜像,因此运行 Docker 容器所需的大多数应用程序都有在 Docker Hub 上的映像。
要检查是否可以从 Docker Hub 访问和下载镜像,请执行:
docker run hello-world
如果输出以下内容表明 Docker 工作正常:
Output
Hello from Docker.
This message shows that your installation appears to be working correctly.
...
使用带有子命令的 docker
命令,可搜索 Docker Hub 上可用的镜像。例如,要搜索 CentOS 镜像,可执行下面的命令:
docker search centos
该脚本将爬取 Docker Hub ,并返回名称匹配的所有镜像的列表,输出将类似于下面所示:
Output
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 2224 [OK]
jdeathe/centos-ssh CentOS-6 6.7 x86_64 / CentOS-7 7.2.1511 x8... 22 [OK]
jdeathe/centos-ssh-apache-php CentOS-6 6.7 x86_64 / Apache / PHP / PHP M... 17 [OK]
million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 11 [OK]
nimmis/java-centos This is docker images of CentOS 7 with dif... 10 [OK]
torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK]
nickistre/centos-lamp LAMP on centos setup 3 [OK]
...
在 OFFICIAL 列中,OK 表示由 Docker 项目官方构建和支持。确定要使用的镜像后,可以使用 pull
子命令将其拉取到本地:
docker pull centos
拉取镜像后,使用下面的命令运行:
docker run centos
要查看已下载到本地的镜像,可执行下面的命令:
docker images
输出应类似于以下内容:
[secondary_lable Output]
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 778a53015523 5 weeks ago 196.7 MB
hello-world latest 94df4f0ce8a4 2 weeks ago 967 B
第 5 步:运行 Docker 容器
在上一步中我们运行 hello-world
进行测试。接下来,我们使用最新的 CentOS 镜像运行一个容器。-i 和 -t 开关可以使我们用 shell 访问容器:
docker run -it centos
接下来就会在容器内运行命令:
Output
[root@59839a1b7de2 /]#
现在就可以在容器内运行命令了。例如,我们可以在该容器中安装 MariaDB :
yum install mariadb-server
第 6 步 :将容器中的更改提交到 Docker 镜像
启动 Docker 镜像后,可以像使用虚拟机一样创建、修改和删除文件。接下来牛奇网向您介绍,如何将容器的状态保存为新的 Docker 镜像。
在 CentOS 容器内安装 MariaDB 后,我们可以将容器的最新状态保存为新的镜像。我们先要退出:
exit
然后使用以下命令,将更改提交到新的 Docker 镜像。-m 用于提交更改信息,-a 用于指定作者。容器 ID 是之前运行容器时的 59839a1b7de2 。存储库是 Docker Hub 用户名。
docker commit -m "What did you do to the image" -a "Author Name" container-id repository/new_image_name
例如:
docker commit -m "added mariadb-server" -a "Sunday Ogwu-Chinuwa" 59839a1b7de2 finid/centos-mariadb
注意:当您提交镜像时,新镜像会保存在本地。在本教程的后面,我们将介绍如何将镜像推送到Docker Hub。
完成该操作后,查看本地的镜像情况:
docker images
输出应该是这样的:
OutputREPOSITORY TAG IMAGE ID CREATED SIZE
finid/centos-mariadb latest 23390430ec73 6 seconds ago 424.6 MB
centos latest 778a53015523 5 weeks ago 196.7 MB
hello-world latest 94df4f0ce8a4 2 weeks ago 967 B
在上面的输出中,centos-mariadb 是新镜像,它是从 Docker Hub 中的 CentOS 镜像派生而来的。在本示例中,我们在 CentOS 中安装了 MariaDB 。
第 7 步:列出 Docker 容器
使用 Docker 一段时间后,本地计算机上将有许多活动或非活动容器。运行下面的命令查看活动的容器:
docker ps
将看到类似于以下内容的输出:
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f7c79cc556dd centos "/bin/bash" 3 hours ago Up 3 hours silly_spence
要查看所有活动或非活动容器,可以在后面加个 -a :
docker ps -a
要查看创建的最新容器,可以加一个 -l:
docker ps -l
可以运行下面的命令关闭容器:
docker stop container-id
这里的 container-id
可以在 docker ps
命令的输出中找到。
第 8 步 :将 Docker 映像推送到 Docker 存储库
要将 Docker 镜像推送到 Docker Hub,先要在 Docker Hub 上创建帐户,然后登录到 Docker Hub。系统将提示您进行身份验证:
docker login -u docker-registry-username
然后可以使用以下命令推送自己的镜像:
docker push docker-registry-username/docker-image-name
这需要一些时间才能完成,完成后将输出下面的内容:
Output
The push refers to a repository [docker.io/finid/centos-mariadb]
670194edfaf5: Pushed
5f70bf18a086: Mounted from library/centos
6a6c96337be1: Mounted from library/centos
...
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/linux/how-to-install-and-use-docker-on-centos-7/