如何在 CentOS 7 上安装和使用 Docker 及容器
本文将学习如何安装 Docker。Docker 是一个非常好的工具,它可以在自己的容器中运行应用程序。它的工作原理类似于虚拟机,但更便携、更节省资源,并且对主机操作系统的依赖性更小。在 CentOS 7 上安装 Docker 有两种方法。第一种方法是使用现有操作系统进行安装,或者使用名为 Docker Machine 的工具启动服务器并进行自动安装。
此处出于演示目的,我们将在现有操作系统上进行安装。
在 CentOS 7 上安装 Docker
在开始安装之前,我们需要 64 位 CentOS 7 系统,并且需要一个具有 sudo 权限的非 root 用户。Docker 安装程序可在 CentOS 7 官方存储库中找到,但不是最新版本。为了获得最新版本,我们将从 Docker 官方存储库安装 Docker。
$ sudo yum check-update
现在运行以下命令以添加 Docker 官方存储库,下载最新版本的 Docker 并安装它。
$ curl -fsSL https://get.docker.com/ | sh
请记住,您需要注销并重新登录才能使更改生效!
安装完成后,我们需要使用以下命令启动 Docker 守护进程
$ sudo systemctl start docker
要查看 Docker 服务是否正在运行,我们可以运行以下命令
$ sudo systemctl status docker
我们需要启用 Docker 在启动时自动启动,需要运行以下命令
$ sudo systemctl enable docker
将当前用户添加到 Docker 组
我们需要将当前用户添加到系统的 Docker 组,尽管安装过程中已启用此功能,但如果没有此步骤,我们无法使用非 root 用户运行 Docker 命令行工具。
$ sudo usermod –aG docker $(whoami)
或者
$ sudo usermond –aG docker username
使用 Docker 命令
由于我们刚刚安装了 Docker 并确认其正在运行,我们需要学习一些命令行工具及其子命令和参数。
$ docker [option] [command] [arguments]
要查看所有子命令,我们需要输入以下命令
$ docker CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@centos-linux-1 ~]# 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 Sub 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 镜像用于运行 Docker 容器,这些镜像是从 Docker Hub 中拉取的。
要检查我们是否有权访问 Docker Hub,请运行以下命令
$ docker run hello-world
我们可以使用 docker 命令及其 search 子命令搜索 Docker Hub 上可用的镜像,例如,我们将搜索 CentOS 镜像,以下是搜索 Docker Hub 上 CentOS 镜像的命令。
[root@centos-linux-1 ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 2417 [OK] ansible/centos7-ansible Ansible on Centos7 80 [OK] jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8... 26 [OK] nimmis/java-centos This is docker images of CentOS 7 with dif... 13 [OK] million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 12 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 10 [OK] torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK] jdeathe/centos-ssh-mysql CentOS-6 6.7 x86_64 / MySQL. 7 [OK] nickistre/centos-lamp LAMP on centos setup 4 [OK] centos/mariadb55-centos7 3 [OK] nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 3 [OK] consol/sakuli-centos-xfce Sakuli end-2-end testing and monitoring co... 2 [OK] blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK] darksheer/centos Base Centos Image -- Updated hourly 1 [OK] timhughes/centos Centos with systemd installed and running 1 [OK] grayzone/centos auto build for centos. 0 [OK] ustclug/centos USTC centos 0 [OK] januswel/centos yum update-ed CentOS image 0 [OK] kz8s/centos Official CentOS plus epel-release 0 [OK] grossws/centos CentOS 6 and 7 base images with gosu and l... 0 [OK] harisekhon/centos-scala Scala + CentOS (OpenJDK tags 2.10-jre7 - 2... 0 [OK] jsmigel/centos-epel Docker base image of CentOS w/ EPEL installed 0 [OK] ericuni/centos centos dev 0 [OK] dmglab/centos CentOS with some extras - This is for the ... 0 [OK] repositoryjp/centos Docker Image for CentOS. 0 [OK]
将 Docker 镜像下载到本地
我们可以从 Docker Hub 下载镜像,以下是使用 pull 命令下载 Docker 镜像的命令
$ docker pull centos docker pull centos Using default tag: latest latest: Pulling from library/centos 0653bff3c5cf: Extracting 29.52 MB/70.6 MB Message from syslogd@centos-linux-1 at Jul 11 08:06:44 ... 0653bff3c5cf: Pull complete Digest: sha256:81e4f2f663eaa1bf46ff9be348396dd7053734b257ef4147d7133d6f25bbf7cf Status: Downloaded newer image for centos:latest
我们可以使用 run 子命令运行我们下载的镜像
$ docker run centos
并使用以下命令查看下载到我们计算机的镜像
$ docker images docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 05188b417f30 9 days ago 196.8 MB hello-world latest c54a2cc56cbb 9 days ago 1.848 kB
运行 Docker 容器
由于我们已将容器镜像下载到本地系统,我们将使用 CentOS 的最新镜像以及 –i 和 –t 开关运行容器,这些开关将为容器提供交互式 shell
$ docker run –it centos [root@0aac55bf786f /]#
提示符将更改为容器 ID,“0aac55bf786f”是下载容器的 ID
我们可以使用以下命令在容器中安装一些软件,我们将在此容器中安装 MySQL 服务器
[root@0aac55bf786f /]# yum install mysql-server
在容器中安装 MySQL 后,我们可以使用以下命令将更改提交到 Docker Hub,其中 –m 开关用于提交消息,–a 用于添加作者姓名
General Syntax $ docker commit -m "What did you do to the image" -a "Author Name" container-id repository/new_image_name
Example $ docker commit -m "added MySQL-server" -a "Chandra Prakash" 0aac55bf786f finid/centos-mysql_db sha256:77993fb3b476e02324b8bf7ebfebe1e792b93df2eecc70208997ac95455c65e8
要确认镜像已提交并列在本地存储库中,您可以运行以下命令。
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE finid/centos-mysql_db latest 77993fb3b476 28 seconds ago 268.4 MB centos latest 05188b417f30 9 days ago 196.8 MB hello-world latest c54a2cc56cbb 9 days ago 1.848 kB
在完成设置和配置后,我们学习了 Docker 及其容器的基础知识,以及如何拉取镜像、使用镜像以及安装一些软件。借助 Docker,我们可以做更多的事情,在后续文章中,我们将学习更多内容。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP