在本文中,我们将带你了解Centos7下安装Docker在这篇文章中,我们将为您详细介绍Centos7下安装Docker的方方面面,并解答CentOS7下安装bmd采集卡常见的疑惑,同时我们还将给您一
在本文中,我们将带你了解Centos7下安装Docker在这篇文章中,我们将为您详细介绍Centos7下安装Docker的方方面面,并解答CentOS7下安装bmd采集卡常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的01_在centos7下安装Docker、CentOS 7下安装Docker、CentOS 7下安装Docker以及Docker的基本使用、CentOS 7下安装Docker及基础操作。
本文目录一览:- Centos7下安装Docker(CentOS7下安装bmd采集卡)
- 01_在centos7下安装Docker
- CentOS 7下安装Docker
- CentOS 7下安装Docker以及Docker的基本使用
- CentOS 7下安装Docker及基础操作
Centos7下安装Docker(CentOS7下安装bmd采集卡)
(转载至:https://www.cnblogs.com/qgc1995/archive/2018/08/29/9553572.html)
一,Docker简介
百科说:Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
看起来有点雾,用过虚拟机的应该对虚拟化技术有点印象,不知道也没关系,就把它当成轻量级的虚拟机吧(虽然一个是完全虚拟化,一个是操作系统层虚拟化),这个解释到位:https://www.cnblogs.com/kex1n/p/6933039.html
百科又说:Docker 使用客户端-服务器 (C/S) 架构模式 使用远程API来管理和创建Docker容器。Docker 容器(Container)通过 Docker 镜像(Image)来创建,二者之间的关系类似于面向对象编程中的对象与类
那Docker由什么组成呢, 包括三个基本概念:
- 仓库(Repository)
- 镜像(Image)
- 容器(Container)
打个比方:你如果想玩英雄联盟中骚气的亚索,你首先得有这个英雄(Docker的镜像),然后你得花金币去英雄商店(Docker的仓库)买,接着进游戏就会看到一个半蹲的发型飘逸的剑客(Docker的容器),所以:
1,其中Registry是Docker用于存放镜像文件的仓库,Docker 仓库的概念跟Git 类似(就像商店存放所有的英雄,只是更改英雄的权限在某些非程序员手里)。
2,所谓镜像就是构建容器的源代码,是一个只读的模板,由一层一层的文件系统组成的,类似于虚拟机的镜像(英雄也是只读的,有自己的技能被动,你也不能进行操作)。
3,那么容器就是由Docker镜像创建的运行实例,类似于虚拟机,容器之间是相互隔离的,包含特定的应用及其所需的依赖文件(好比每个英雄都是隔离的,都有自己的皮肤,技能以及走的路线)。
注:Docker Hub
是Docker公司提供的一个注册服务器(Register)来保存多个仓库,每个仓库又可以包含多个具备不同tag的镜像
二,安装Docker
我是虚拟机装的Centos7,linux 3.10 内核,docker官方说至少3.8以上,建议3.10以上(ubuntu下要linux内核3.8以上, RHEL/Centos 的内核修补过, centos6.5的版本就可以——这个可以试试)
1,root账户登录,查看内核版本如下
[root@localhost ~]# uname -a
Linux localhost.qgc 3.10.0-862.11.6.el7.x86_64 #1 SMP Tue Aug 14 21:49:04 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
2,把yum包更新到最新(不要问我为什么)
[root@localhost ~]# yum update
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: centos.ustc.edu.cn
* extras: mirrors.aliyun.com
* updates: centos.ustc.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 bind-libs.x86_64.32.9.9.4-61.el7 将被 升级
---> 软件包 bind-libs.x86_64.32.9.9.4-61.el7_5.1 将被 更新
---> 软件包 bind-libs-lite.x86_64.32.9.9.4-61.el7 将被 升级
---> 软件包 bind-libs-lite.x86_64.32.9.9.4-61.el7_5.1 将被 更新
---> 软件包 bind-license.noarch.32.9.9.4-61.el7 将被 升级
---> 软件包 bind-license.noarch.32.9.9.4-61.el7_5.1 将被 更新
...
...
验证中 : 32:bind-license-9.9.4-61.el7.noarch 8/8
更新完毕:
bind-libs.x86_64 32:9.9.4-61.el7_5.1
bind-libs-lite.x86_64 32:9.9.4-61.el7_5.1
bind-license.noarch 32:9.9.4-61.el7_5.1
bind-utils.x86_64 32:9.9.4-61.el7_5.1
完毕!
[root@localhost ~]#
3,安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
[root@localhost ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: centos.ustc.edu.cn
* extras: mirrors.aliyun.com
* updates: centos.ustc.edu.cn
...
4,设置yum源
[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
已加载插件:fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
5,可以查看所有仓库中所有docker版本,并选择特定版本安装(如果有安装过先卸载旧版本: sudo yum remove docker docker-common docker-selinux docker-engine)
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
已加载插件:fastestmirror, langpacks
可安装的软件包
* updates: centos.ustc.edu.cn
Loading mirror speeds from cached hostfile
* extras: mirrors.aliyun.com
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
...
6,安装Docker,命令:yum install docker-ce-版本号,我选的是17.12.1.ce,如下
[root@localhost ~]# yum install docker-ce-17.12.1.ce
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: centos.ustc.edu.cn
* extras: mirrors.aliyun.com
* updates: centos.ustc.edu.cn
base | 3.6 kB 00:00
docker-ce-stable | 2.9 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
正在解决依赖关系
--> 正在检查事务
---> 软件包 docker-ce.x86_64.0.17.12.1.ce-1.el7.centos 将被 安装
--> 正在处理依赖关系 container-selinux >= 2.9,它被软件包 docker-ce-17.12.1.ce-1.el7.centos.x86_64 需要
...
7, 启动Docker,命令:systemctl start docker,然后加入开机启动,如下
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
8,验证安装是否成功(有client和service两部分表示docker安装启动都成功了)
[root@localhost ~]# docker version
Client:
Version: 17.12.1-ce
API version: 1.35
Go version: go1.9.4
Git commit: 7390fc6
Built: Tue Feb 27 22:15:20 2018
OS/Arch: linux/amd64
Server:
Engine:
Version: 17.12.1-ce
API version: 1.35 (minimum version 1.12)
Go version: go1.9.4
Git commit: 7390fc6
Built: Tue Feb 27 22:17:54 2018
OS/Arch: linux/amd64
Experimental: false
备注:因为我已经安装过一次,部分贴图可能有出入,具体可参考博客:https://www.cnblogs.com/yufeng218/p/8370670.html
爬一些常用Docker命令,更多命令详解,请访问:http://www.docker.org.cn/dockerppt/106.html:
----------------- docker ps 查看当前正在运行的容器
----------------- docker ps -a 查看所有容器的状态
----------------- docker start/stop id/name 启动/停止某个容器
----------------- docker attach id 进入某个容器(使用exit退出后容器也跟着停止运行)
----------------- docker exec -ti id 启动一个伪终端以交互式的方式进入某个容器(使用exit退出后容器不停止运行)
----------------- docker images 查看本地镜像
----------------- docker rm id/name 删除某个容器
----------------- docker rmi id/name 删除某个镜像
----------------- docker run --name test -ti ubuntu /bin/bash 复制ubuntu容器并且重命名为test且运行,然后以伪终端交互式方式进入容器,运行bash
----------------- docker build -t soar/centos:7.1 . 通过当前目录下的Dockerfile创建一个名为soar/centos:7.1的镜像
----------------- docker run -d -p 2222:22 --name test soar/centos:7.1 以镜像soar/centos:7.1创建名为test的容器,并以后台模式运行,并做端口映射到宿主机2222端口,P参数重启容器宿主机端口会发生改变
01_在centos7下安装Docker
目录
- 注意:整个过程都是在 root 用户下
- 1、root 账户登录,查看 linux 内核版本
- 2、把 yum 包更新到最新(期间要选择确认,输入 y 即可)
- 3、安装需要的软件包
- 4、设置 yum 源(选择其中一个)
- 5、可以查看所有仓库中所有 docker 版本,并选择特定版本安装。
- 6、安装 Docker,命令:yum install docker-ce-版本号
- 7、启动 Docker,命令:systemctl start docker,然后加入开机启动,命令:system enable docker。
- 8、查看 Docker 版本,命令:docker version
注意:整个过程都是在 root 用户下
1、root 账户登录,查看 linux 内核版本
2、把 yum 包更新到最新(期间要选择确认,输入 y 即可)
3、安装需要的软件包
,yum-util 提供 yum-config-manager 功能,另外两个是 devicemapper 驱动依赖的。
4、设置 yum 源(选择其中一个)
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo(中央仓库)
yum-config-manager --add-repo
https://mirrors.aliyun.com/docker/linux/centos/docker-ce/linux/centos/docker.repo(阿里仓库)
5、可以查看所有仓库中所有 docker 版本,并选择特定版本安装。
6、安装 Docker,命令:yum install docker-ce-版本号
,我选的是docker-ce-18.03.1.ce。
7、启动 Docker,命令:systemctl start docker
,然后加入开机启动,命令:system enable docker
。
8、查看 Docker 版本,命令:docker version
这里是在 Xshell6 里看到的图
CentOS 7下安装Docker
安装一些必要的系统工具:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
添加软件源信息:
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新 yum 缓存:
sudo yum makecache fast
安装 Docker-ce:
sudo yum -y install docker-ce
设置 Docker 开机自启
sudo systemctl enable docker
启动 Docker 后台服务
sudo systemctl start docker
测试运行 hello-world
[root@runoob ~]# docker run hello-world
CentOS 7下安装Docker以及Docker的基本使用
CentOS 7 安装 Docker CE https://cloud.tencent.com/developer/article/1382457
安装前的准备
系统要求
- 官方推荐使用 CentOS 7 的维护版本,已经归档的版本不受支持或未经测试
- 需要启用
centos-extras
repository。在 CentOS 7 中这个仓库是默认启用的,如果之前有将其禁用,则需要重新启用 - 推荐使用
overlay2
作为 Docker 的存储驱动
卸载旧版本
旧版本的 Docker 在 CentOS 中的包名为docker
或docker-engine
。如果之前安装了 Docker 的旧版本,需要先卸载旧版 Docker 及相关依赖:
> sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-selinux \ docker-engine-selinux \ docker-engine
若yum
提示卸载成功或没有找到相关包,即可进行下一步操作。
注意:/var/lib/docker/
目录下的内容,包括镜像、容器、卷组、网络等文件将被保留。Docker CE 的新包名为docker-ce
。
安装 Docker CE
有以下三种方法安装 Docker CE,可根据实际需要选择:
- 建立 Docker 仓库:安装过程及后续的更新方便,Docker 官方推荐。
- 下载 RPM 包手动安装:手动管理更新。适合离线环境。
- 通过安装脚本自动安装:适合测试及开发环境。
方法 1:建立 Docker 仓库
在首次安装 Docker CE 前需要建立 Docker repository,之后可通过仓库安装并更新 Docker。
建立仓库
1.安装所需软件包。yum-utils
提供了yum-config-manager
工具,存储驱动devicemapper
则依赖于device-mapper-persistent-data
和lvm2
:
> sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
2.使用以下命令建立stable
版本的 repository:
> sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
3.可选:启用edge
和test
仓库。这些仓库包含在docker.repo
文件中,但默认是禁用的。可以将它们与stable
仓库共同启用。
> sudo yum-config-manager --enable docker-ce-edge > sudo yum-config-manager --enable docker-ce-test
使用带--disable
参数的yum-config-manager
命令即可禁用edge
或test
仓库,使用--enable
参数则会重新启用。例如下面的命令将禁用edge
仓库:
> sudo yum-config-manager --disable docker-ce-edge
从 Docker
17.06
版本开始,stable
仓库的 releases 也会推送至edge
及test
仓库中。 点击此处查看 Docker 官方关于stable
和edge
的说明。
安装 Docker CE
1.使用以下命令安装最新版 Docker CE:
> sudo yum install docker-ce
如果提示是否接受 GPG 密钥,则需验证密钥指纹是否符合下面的内容,若符合即可点击 accept 继续安装:
060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35
如果启用了多个 Docker 仓库,并且在
yum install
或yum update
命令中没有指明版本,则会安装所有仓库中版本号最新的 Docker。
2.要安装指定版本的 Docker CE,则需要从仓库中列出所有可用的版本,再根据需要选择安装:
> yum list docker-ce --showduplicates | sort -r docker-ce.x86_64 18.09.0.ce-1.el7.centos docker-ce-stable
此时安装包名的格式为docker-ce-<VERSION STRING>
。例如安装18.03.0
版本的 Docker CE:
> sudo yum install docker-ce-18.03.0.ce
此时 Docker 应该已经安装完成,但还没有启动。新的用户组docker
也已创建,目前为空。
3.启动 Docker:
> sudo systemctl start docker
4.运行hello-world
镜像以验证 Docker 是否正确安装:
> sudo docker run hello-world 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. (amd64) 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 ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
升级 Docker CE
如需升级 Docker CE,则可根据上述安装教程,选择安装最新版docker-ce
,即可完成升级。
方法 2:下载 RPM 包手动安装
安装 Docker CE
如果无法使用 Docker 仓库,可以下载.rpm
安装包手动安装 Docker CE。
1.前往https://download.docker.com/linux/centos/7/x86_64/stable/Packages/,下载对应版本的 RPM 安装包。
2.使用yum
命令安装 RPM 包:
> sudo yum install /path/to/package.rpm
3.启动 Docker:
> sudo systemctl start docker
4.运行hello-world
镜像以验证 Docker 是否正确安装:
> sudo docker run hello-world 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. (amd64) 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 ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
升级 Docker CE
如需升级,则可下载新版本的 RPM 安装包,使用yum upgrade
命令升级:
> sudo yum -y upgrade /path/to/package.rpm
方法 3:通过安装脚本自动安装
通过 Docker 提供的一键安装脚本可以在开发环境中快速安装 Docker CE,且无需交互。get.docker.com 及 test.docker.com 分别对应edge
和test
版本,脚本源码存放在 docker-install 仓库 中。
Docker 官方不推荐在生产环境中使用安装脚本
下面的示例将使用 get.docker.com 提供的脚本安装 Docker CE 的最新发布版本。如果要安装最新测试版本,只需将脚本替换为 test.docker.com,并将下面示例命令中的get
替换为test
:
> curl -fsSL https://get.docker.com -o get-docker.sh > sudo sh get-docker.sh <output truncated>
如果需要让非root
用户使用 Docker,则使用以下命令将用户添加至docker
用户组:
> sudo usermod -aG docker your-user
注销并重新登录,即可生效。之后启动 Docker:
> sudo systemctl start docker
运行hello-world
镜像以验证 Docker 是否正确安装:
> sudo docker run hello-world 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. (amd64) 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 ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
卸载 Docker CE
1. 卸载 Docker 安装包
> sudo yum remote docker-ce
2. 删除相关文件
主机上的镜像、容器、卷组以及自定义的配置文件需要手动删除:
> sudo rm -rf /var/lib/docker
参考资料
Get Docker CE for CentOS | Docker Docs
Post-installation steps for Linux | Docker Docs
Docker Docs
镜像加速
如果发现Docker拉取镜像的速度非常缓慢,可以配置加速器来解决
在这里使用阿里云的加速地址:
https://br10hqrl.mirror.aliyuncs.com
修改Docker的配置文件来设置加速地址
vim /etc/docker/daemon.json
1
{
"registry-mirrors": ["https://br10hqrl.mirror.aliyuncs.com"]
}
1
2
3
如果没有该文件,新建一个
也可以通过以下命来设置
tee /etc/docker/daemon.json <<-''EOF''
{
"registry-mirrors": ["https://br10hqrl.mirror.aliyuncs.com"]
}
EOF
1
2
3
4
5
使配置文件生效
systemctl daemon-reload && systemctl restart docker
1
删除Docker-ce
yum remove -y docker-ce
rm -rf /var/lib/docker
1
2
Docker Hello World
Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序。
输出Hello world
docker run ubuntu /bin/echo "hello world"
1
Unable to find image ''ubuntu:latest'' locally
latest: Pulling from library/ubuntu
7413c47ba209: Pull complete
0fe7e7cbb2e8: Pull complete
1d425c982345: Pull complete
344da5c95cec: Pull complete
Digest: sha256:c303f19cfe9ee92badbbbd7567bc1ca47789f79303ddcef56f77687d4744cd7a
Status: Downloaded newer image for ubuntu:latest
hello world
1
2
3
4
5
6
7
8
9
参数解析:
docker:Docker的二进制文件
run:与前面的docker组合使用来运行一个容器
ubuntu:latest:指定要运行的镜像,Docker首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
/bin/echo “hello world”:在启动的容器里执行的命令
整句意思是:Docker以ubuntu:latest镜像创建一个容器,然后在容器里面执行bin/echo "hello world"命令,然后输出结果。
运行交互式的容器
我们可以通过docker的-i-t参数来让容器实现"对话"功能
docker run -it ubuntu:latest /bin/bash
root@a08b25f150bc:/#
1
2
参数解析:
-i:允许你对容器内的标准输入 (STDIN) 进行交互。
-t:在新容器内指定一个伪终端或终端。
通过-i-t两个参数我们进入到了ubuntu这个容器内部
在容器内部运行以下命令查看当前系统版本信息和当前目录下的文件列表
root@a08b25f150bc:/# cat /proc/version
Linux version 3.10.0-957.21.3.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Tue Jun 18 16:35:19 UTC 2019
root@a08b25f150bc:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@a08b25f150bc:/#
1
2
3
4
5
6
启动容器
在我们启动容器的时候通过-d参数可以使容器后台运行
docker run -d ubuntu:latest /bin/sh -c "while true; do echo hello world; sleep 1; done"
1391c001ef7f562c8c95730998a94b638f9be9fc05be9075db19fd2aa8f2fc9b
1
2
3
在输出中,我们没有看到期望的"hello world",而是一串长字符
1391c001ef7f562c8c95730998a94b638f9be9fc05be9075db19fd2aa8f2fc9b
这个长字符串叫做容器ID,对每个容器来说都是唯一的,我们可以通过容器ID来查看对应的容器发生了什么。
首先,我们需要确认容器在运行,可以通过docker ps来查看,加-a参数可以查看所有容器,包括退出、创建中和正在运行的容器。
docker ps
1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1391c001ef7f ubuntu:latest "/bin/sh -c ''while t…" 5 minutes ago Up 5 minutes mystifying_burnell
1
2
3
docker ps -a
1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1391c001ef7f ubuntu:latest "/bin/sh -c ''while t…" About an hour ago Exited (137) 19 minutes ago mystifying_burnell
700af38bbd37 ubuntu:latest "/bin/bash" About an hour ago Exited (0) About an hour ago practical_wescoff
a08b25f150bc ubuntu:latest "/bin/bash" About an hour ago Exited (130) About an hour ago gallant_curie
6eb5efafcfe8 ubuntu "/bin/echo ''hello wo…" About an hour ago Exited (0) About an hour ago sharp_curran
64a28ff4f5cd hello-world "/hello" 2 hours ago Exited (0) 2 hours ago clever_booth
1
2
3
4
5
6
7
参数解析:
CONTAINER ID:容器ID
NAMES:自动分配的容器名称
我们可以使用docker logs ["CONTAINER" | "NAMES"]来查看容器内的标准输出和日志
docker logs 1391c001ef7f
hello world
hello world
hello world
hello world
hello world
1
2
3
4
5
6
docker logs mystifying_burnell
hello world
hello world
hello world
hello world
hello world
1
2
3
4
5
6
停止容器
我们使用docker stop ["CONTAINER" | "NAMES"]来停止容器
docker stop 1391c001ef7f
1391c001ef7f
1
2
通过docker ps查看,容器已经停止工作
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1
2
也可以使用下面的命令来停止
docker stop mystifying_burnell
1
Docker 容器的使用
Docker的所有命令
我们可以输入doker --help或者直接输入docker来查看docker的所有命令
docker --help
1
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
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
Run ''docker COMMAND --help'' for more information on a command.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
我们还可以通过docker COMMAND --help来查看某个选项的更多信息
docker rm --help
1
运行一个Web应用
前面我们运行的容器并没有什么特别的用处。
接下来我们尝试使用Docker构建一个web应用程序
我们将在Docker容器中运行一个 Python Flask 应用来运行一个web应用。
下载镜像
我们可以通过docker pull命令来下载镜像,如果不指定镜像的tag则默认是用latest版本的镜像
docker pull training/webapp
1
docker pull training/webapp
Using default tag: latest
latest: Pulling from training/webapp
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
10bbbc0fc0ff: Pull complete
fca59b508e9f: Pull complete
e7ae2541b15b: Pull complete
9dd97ef58ce9: Pull complete
a4c1b0cb7af7: Pull complete
Digest: sha256:06e9c1983bd6d5db5fba376ccd63bfa529e8d02f23d5079b8f74a616308fb11d
Status: Downloaded newer image for training/webapp:latest
docker.io/training/webapp:latest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
上面我所下载的镜像没有指定tag,所以默认使用的是latest的镜像版本,我们可以使用docker pull image:tag来下载指定版本的镜像。
例如,我想下载一个1.16版本的Nginx稳定版镜像
docker pull nginx:1.16
1
1.16: Pulling from library/nginx
1ab2bdfe9778: Pull complete
f50f9bfaef26: Pull complete
eef791162233: Pull complete
Digest: sha256:764877c3b96e7d57f8f15bb84597dd108dcdf700bfc43fbe340dae764630386d
Status: Downloaded newer image for nginx:1.16
docker.io/library/nginx:1.16
1
2
3
4
5
6
7
当指定了tag时,docker下载镜像就会下载指定的tag版本镜像,而不是默认的latest镜像。
运行容器
docker run -d -P training/webapp python app.py
5bc0e295d0033648eb98943f33d7acfc0f603d7b62225f3db21d812f81da1634
1
2
参数解析:
-d:让容器在后台运行。
-P:将容器内部使用的端口映射到主机的随机端口,如果使用-p参数,则是指定容器内部端口映射到主机上的某个端口,具体用法-p <主机端口>:<容器内部端口>。
查看Web应用容器
我们可以使用docker ps来查看docker正在运行的容器:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5bc0e295d003 training/webapp "python app.py" 16 seconds ago Up 13 seconds 0.0.0.0:32768->5000/tcp upbeat_knuth
1
2
3
各栏目解析:
CONTAINER ID:指容器的ID,每一个容器运行时都会有一个唯一的ID。
IMAGE:容器使用的镜像。
COMMAND:容器执行的命令。
CREATED:容器创建的时间。
STATUS:容器运行的时长。
PORTS:容器的端口映射情况,后面是容器内部使用的5000端口,映射到主机的32768端口,我们访问主机的32768端口,就可以使用容器提供的服务了。
NAMES:容器的名字,可以通过--name指定,和ID一样是唯一的。
通过标准输出,我们看到了容器提供服务的端口,打开浏览器,输入主机的ip:容器映射到主机的端口,来访问容器提供的服务了,这里我输入http://192.168.6.128:32768来访问。
前面我们使用-P来随机映射端口,下面我们自己来指定端口,使用-p参数指定
docker run -d -p 5000:5000 --name web training/webapp
4c1acc0051961193cc8faf8dcacf05dea454fb960217a243ae89b8e327ee8daf
1
2
我们把容器内部的5000端口映射到主机的5000端口,打开浏览器访问http://192.168.6.128:5000,就可以得到容器提供的服务了。
查看容器的端口
我们可以通过docker ps来查看正在运行的容器的ID、名字、和端口,要查看某个容器的端口,可以根据他的容器ID或者名字来获取它的端口信息,例如:
docker ps
1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c1acc005196 training/webapp "python app.py" About an hour ago Up About an hour 0.0.0.0:5000->5000/tcp web
5bc0e295d003 training/webapp "python app.py" 3 hours ago Up 3 hours 0.0.0.0:32768->5000/tcp upbeat_knuth
1
2
3
4
我们要查看第一个容器的端口信息,它的ID是4c1acc005196,名字是web
docker port 4c1acc005196
5000/tcp -> 0.0.0.0:5000
1
2
docker port web
5000/tcp -> 0.0.0.0:5000
1
2
查看容器的日志信息
当我们运行docker容器时,需要查看它的日志信息,来确定是否运行正常,我们通过docker logs [CONTAINER | NAMES]来查看
docker logs -f 4c1acc005196
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.6.1 - - [16/Aug/2019 07:00:48] "GET / HTTP/1.1" 200 -
192.168.6.1 - - [16/Aug/2019 07:00:48] "GET /favicon.ico HTTP/1.1" 404 -
1
2
3
4
-f:让docker logs可以向tail -f一样持续输出容器内部的标准输出
查看WEB应用程序容器的进程
我们可以使用docker top来查看容器内部运行的进程
docker top 4c1acc005196
UID PID PPID C STIME TTY TIME CMD
root 19910 19891 0 14:02 ? 00:00:01 python app.py
1
2
3
检查WEB应用程序
使用docker inspect来查看Docker的底层信息,他会返回一个json文件记录着Docker容器的配置和状态信息。
docker inspect 4c1acc005196
1
[
{
"Id": "4c1acc0051961193cc8faf8dcacf05dea454fb960217a243ae89b8e327ee8daf",
"Created": "2019-08-16T06:02:31.210492449Z",
"Path": "python",
"Args": [
"app.py"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 19910,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-08-16T06:02:32.26229118Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
停止WEB应用容器
docker stop 4c1acc005196
4c1acc005196
1
2
重启WEB应用程序
已经停止的容器,我们可以使用docker start来启动
我们先找出退出容器的ID和名字,然后启动它,我们使用docker ps -a来找出退出的容器。
docker ps -a
1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c1acc005196 training/webapp "python app.py" 2 hours ago Exited (137) 6 minutes ago web
5bc0e295d003 training/webapp "python app.py" 4 hours ago Up 4 hours 0.0.0.0:32768->5000/tcp upbeat_knuth
1
2
3
我们还可以通过docker ps -l查询最后一次创建的容器
docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c1acc005196 training/webapp "python app.py" 2 hours ago Up 8 minutes 0.0.0.0:5000->5000/tcp web
1
2
3
上面输出我们获取了退出容器的ID,下面启动它
docker start 4c1acc005196
4c1acc005196
1
2
正在运行的容器我们可以通过docker restart来重启。
删除WEB应用容器
我们可以使用docker rm命令来删除不需要的容器
docker stop 4c1acc005196
4c1acc005196
docker rm 4c1acc005196
4c1acc005196
1
2
3
4
5
删除容器时,容器必须是停止状态,否则会报错,但是可以使用-f参数来强制删除。
docker rm 4c1acc005196
Error response from daemon: You cannot remove a running container 4c1acc0051961193cc8faf8dcacf05dea454fb960217a243ae89b8e327ee8daf. Stop the container before attempting removal or force remove
1
2
Docker 镜像的使用
当运行容器时,使用的镜像如果在本地中不存在,docker 就会自动从 docker 镜像仓库中下载,默认是从 Docker Hub 公共镜像源下载。
下面我们来学习:
1、管理和使用本地 Docker 主机镜像
2、创建镜像
列出镜像列表
我们可以使用 docker images 来列出本地主机上的镜像。
runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04 90d5884b1ee0 5 days ago 188 MB
php 5.6 f40e9e0f10c8 9 days ago 444.8 MB
nginx latest 6f8d099c3adc 12 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
ubuntu 15.10 4e3b13c8a266 4 weeks ago 136.3 MB
hello-world latest 690ed74de00f 6 months ago 960 B
training/webapp latest 6fae60ef3446 11 months ago 348.8 MB
1
2
3
4
5
6
7
8
9
10
各个选项说明:
**REPOSITORY:**表示镜像的仓库源
**TAG:**镜像的标签
**IMAGE ID:**镜像ID
**CREATED:**镜像创建时间
**SIZE:**镜像大小
同一仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如ubuntu仓库源里,有15.10、14.04等多个不同的版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。
所以,我们如果要使用版本为15.10的ubuntu系统镜像来运行容器时,命令如下:
runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@d77ccb2e5cca:/#
1
2
如果要使用版本为14.04的ubuntu系统镜像来运行容器时,命令如下:
runoob@runoob:~$ docker run -t -i ubuntu:14.04 /bin/bash
root@39e968165990:/#
1
2
如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像。
获取一个新的镜像
当我们在本地主机上使用一个不存在的镜像时 Docker 就会自动下载这个镜像。如果我们想预先下载这个镜像,我们可以使用 docker pull 命令来下载它。
Crunoob@runoob:~$ docker pull ubuntu:13.10
13.10: Pulling from library/ubuntu
6599cadaf950: Pull complete
23eda618d451: Pull complete
f0be3084efe9: Pull complete
52de432f084b: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:15b79a6654811c8d992ebacdfbd5152fcf3d165e374e264076aa435214a947a3
Status: Downloaded newer image for ubuntu:13.10
1
2
3
4
5
6
7
8
9
下载完成后,我们可以直接使用这个镜像来运行容器。
查找镜像
我们可以从 Docker Hub 网站来搜索镜像,Docker Hub 网址为: https://hub.docker.com/
我们也可以使用 docker search 命令来搜索镜像。比如我们需要一个httpd的镜像来作为我们的web服务。我们可以通过 docker search 命令搜索 httpd 来寻找适合我们的镜像。
runoob@runoob:~$ docker search httpd
1
**NAME:**镜像仓库源的名称
**DESCRIPTION:**镜像的描述
**OFFICIAL:**是否docker官方发布
拖取镜像
我们决定使用上图中的httpd 官方版本的镜像,使用命令 docker pull 来下载镜像。
runoob@runoob:~$ docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
8b87079b7a06: Pulling fs layer
a3ed95caeb02: Download complete
0d62ec9c6a76: Download complete
a329d50397b9: Download complete
ea7c1f032b5c: Waiting
be44112b72c7: Waiting
1
2
3
4
5
6
7
8
9
下载完成后,我们就可以使用这个镜像了。
runoob@runoob:~$ docker run httpd
1
创建镜像
当我们从docker镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。
1.从已经创建的容器中更新镜像,并且提交这个镜像
2.使用 Dockerfile 指令来创建一个新的镜像
更新镜像
更新镜像之前,我们需要使用镜像来创建一个容器。
runoob@runoob:~$ docker run -t -i ubuntu:15.10 /bin/bash
root@e218edb10161:/#
1
2
在运行的容器内使用 apt-get update 命令进行更新。
在完成操作之后,输入 exit命令来退出这个容器。
此时ID为e218edb10161的容器,是按我们的需求更改的容器。我们可以通过命令 docker commit来提交容器副本。
runoob@runoob:~$ docker commit -m="has update" -a="runoob" e218edb10161 runoob/ubuntu:v2
sha256:70bf1840fd7c0d2d8ef0a42a817eb29f854c1af8f7c59fc03ac7bdee9545aff8
1
2
各个参数说明:
**-m:**提交的描述信息
**-a:**指定镜像作者
**e218edb10161:**容器ID
**runoob/ubuntu:v2:**指定要创建的目标镜像名
我们可以使用 docker images 命令来查看我们的新镜像 runoob/ubuntu:v2:
runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/ubuntu v2 70bf1840fd7c 15 seconds ago 158.5 MB
ubuntu 14.04 90d5884b1ee0 5 days ago 188 MB
php 5.6 f40e9e0f10c8 9 days ago 444.8 MB
nginx latest 6f8d099c3adc 12 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
ubuntu 15.10 4e3b13c8a266 4 weeks ago 136.3 MB
hello-world latest 690ed74de00f 6 months ago 960 B
training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
1
2
3
4
5
6
7
8
9
10
11
使用我们的新镜像 runoob/ubuntu 来启动一个容器
runoob@runoob:~$ docker run -t -i runoob/ubuntu:v2 /bin/bash
root@1a9fbdeb5da3:/#
1
2
构建镜像
我们使用命令 docker build , 从零开始来创建一个新的镜像。为此,我们需要创建一个 Dockerfile 文件,其中包含一组指令来告诉 Docker 如何构建我们的镜像。
runoob@runoob:~$ cat Dockerfile
FROM centos:6.7
MAINTAINER Fisher "fisher@sudops.com"
RUN /bin/echo ''root:123456'' |chpasswd
RUN useradd runoob
RUN /bin/echo ''runoob:123456'' |chpasswd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" >/etc/default/local
EXPOSE 22
EXPOSE 80
CMD /usr/sbin/sshd -D
1
2
3
4
5
6
7
8
9
10
11
每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的。
第一条FROM,指定使用哪个镜像源
RUN 指令告诉docker 在镜像内执行命令,安装了什么。。。
然后,我们使用 Dockerfile 文件,通过 docker build 命令来构建一个镜像。
runoob@runoob:~$ docker build -t runoob/centos:6.7 .
Sending build context to Docker daemon 17.92 kB
Step 1 : FROM centos:6.7
---> d95b5ca17cc3
Step 2 : MAINTAINER Fisher "fisher@sudops.com"
---> Using cache
---> 0c92299c6f03
Step 3 : RUN /bin/echo ''root:123456'' |chpasswd
---> Using cache
---> 0397ce2fbd0a
Step 4 : RUN useradd runoob
......
1
2
3
4
5
6
7
8
9
10
11
12
参数说明:
-t :指定要创建的目标镜像名
. :Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
使用docker images 查看创建的镜像已经在列表中存在,镜像ID为860c279d2fec
runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/centos 6.7 860c279d2fec About a minute ago 190.6 MB
runoob/ubuntu v2 70bf1840fd7c 17 hours ago 158.5 MB
ubuntu 14.04 90d5884b1ee0 6 days ago 188 MB
php 5.6 f40e9e0f10c8 10 days ago 444.8 MB
nginx latest 6f8d099c3adc 12 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
ubuntu 15.10 4e3b13c8a266 5 weeks ago 136.3 MB
hello-world latest 690ed74de00f 6 months ago 960 B
centos 6.7 d95b5ca17cc3 6 months ago 190.6 MB
training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
1
2
3
4
5
6
7
8
9
10
11
12
13
我们可以使用新的镜像来创建容器
runoob@runoob:~$ docker run -t -i runoob/centos:6.7 /bin/bash
[root@41c28d18b5fb /]# id runoob
uid=500(runoob) gid=500(runoob) groups=500(runoob)
1
2
3
从上面看到新镜像已经包含我们创建的用户runoob
设置镜像标签
我们可以使用 docker tag 命令,为镜像添加一个新的标签。
runoob@runoob:~$ docker tag 860c279d2fec runoob/centos:dev
1
docker tag 镜像ID,这里是 860c279d2fec ,用户名称、镜像源名(repository name)和新的标签名(tag)。
使用 docker images 命令可以看到,ID为860c279d2fec的镜像多一个标签。
runoob@runoob:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/centos 6.7 860c279d2fec 5 hours ago 190.6 MB
runoob/centos dev 860c279d2fec 5 hours ago 190.6 MB
runoob/ubuntu v2 70bf1840fd7c 22 hours ago 158.5 MB
ubuntu 14.04 90d5884b1ee0 6 days ago 188 MB
php 5.6 f40e9e0f10c8 10 days ago 444.8 MB
nginx latest 6f8d099c3adc 13 days ago 182.7 MB
mysql 5.6 f2e8d6c772c0 3 weeks ago 324.6 MB
httpd latest 02ef73cf1bc0 3 weeks ago 194.4 MB
ubuntu 15.10 4e3b13c8a266 5 weeks ago 136.3 MB
hello-world latest 690ed74de00f 6 months ago 960 B
centos 6.7 d95b5ca17cc3 6 months ago 190.6 MB
training/webapp latest 6fae60ef3446 12 months ago 348.8 MB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Dcoker 容器的连接
前面我们实现了通过网络端口来访问运行在 docker 容器内的服务。下面我们来实现通过端口连接到一个 docker 容器
网络端口映射
我们创建了一个 python 应用的容器。
runoob@runoob:~$ docker run -d -P training/webapp python app.py
fce072cc88cee71b1cdceb57c2821d054a4a59f67da6b416fceb5593f059fc6d
1
2
另外,我们可以指定容器绑定的网络地址,比如绑定 127.0.0.1。
我们使用 -P 参数创建一个容器,使用 docker ps 可以看到容器端口 5000 绑定主机端口 32768。
runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fce072cc88ce training/webapp "python app.py" 4 minutes ago Up 4 minutes 0.0.0.0:32768->5000/tcp grave_hopper
1
2
3
我们也可以使用 -p 标识来指定容器端口绑定到主机端口。
两种方式的区别是:
-P :是容器内部端口随机映射到主机的高端口。
-p : 是容器内部端口绑定到指定的主机端口。
runoob@runoob:~$ docker run -d -p 5000:5000 training/webapp python app.py
33e4523d30aaf0258915c368e66e03b49535de0ef20317d3f639d40222ba6bc0
runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33e4523d30aa training/webapp "python app.py" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp berserk_bartik
fce072cc88ce training/webapp "python app.py" 8 minutes ago Up 8 minutes 0.0.0.0:32768->5000/tcp grave_hopper
1
2
3
4
5
6
另外,我们可以指定容器绑定的网络地址,比如绑定 127.0.0.1。
runoob@runoob:~$ docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py
95c6ceef88ca3e71eaf303c2833fd6701d8d1b2572b5613b5a932dfdfe8a857c
runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95c6ceef88ca training/webapp "python app.py" 6 seconds ago Up 6 seconds 5000/tcp, 127.0.0.1:5001->5000/tcp adoring_stonebraker
33e4523d30aa training/webapp "python app.py" 3 minutes ago Up 3 minutes 0.0.0.0:5000->5000/tcp berserk_bartik
fce072cc88ce training/webapp "python app.py" 10 minutes ago Up 10 minutes 0.0.0.0:32768->5000/tcp grave_hopper
1
2
3
4
5
6
7
这样我们就可以通过访问 127.0.0.1:5001 来访问容器的 5000 端口。
上面的例子中,默认都是绑定 tcp 端口,如果要绑定 UDP 端口,可以在端口后面加上 /udp。
runoob@runoob:~$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
6779686f06f6204579c1d655dd8b2b31e8e809b245a97b2d3a8e35abe9dcd22a
runoob@runoob:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6779686f06f6 training/webapp "python app.py" 4 seconds ago Up 2 seconds 5000/tcp, 127.0.0.1:5000->5000/udp drunk_visvesvaraya
95c6ceef88ca training/webapp "python app.py" 2 minutes ago Up 2 minutes 5000/tcp, 127.0.0.1:5001->5000/tcp adoring_stonebraker
33e4523d30aa training/webapp "python app.py" 5 minutes ago Up 5 minutes 0.0.0.0:5000->5000/tcp berserk_bartik
fce072cc88ce training/webapp "python app.py" 12 minutes ago Up 12 minutes 0.0.0.0:32768->5000/tcp grave_hopper
1
2
3
4
5
6
7
8
docker port 命令可以让我们快捷地查看端口的绑定情况。
runoob@runoob:~$ docker port adoring_stonebraker 5000
127.0.0.1:5001
1
2
Docker容器连接
端口映射并不是唯一把 docker 连接到另一个容器的方法。
docker 有一个连接系统允许将多个容器连接在一起,共享连接信息。
docker 连接会创建一个父子关系,其中父容器可以看到子容器的信息。
容器命名
当我们创建一个容器的时候,docker 会自动对它进行命名。另外,我们也可以使用 –name 标识来命名容器,例如:
runoob@runoob:~$ docker run -d -P --name runoob training/webapp python app.py
43780a6eabaaf14e590b6e849235c75f3012995403f97749775e38436db9a441
1
2
我们可以使用 docker ps 命令来查看容器名称。
runoob@runoob:~$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43780a6eabaa training/webapp "python app.py" 3 minutes ago Up 3 minutes 0.0.0.0:32769->5000/tcp runoob
1
2
3
点赞 1
————————————————
版权声明:本文为CSDN博主「莫失莫忘c」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/TangXuZ/article/details/100082144
CentOS 7下安装Docker及基础操作
Docker安装
第一步:
移除旧的版本sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-selinux \ docker-engine-selinux \ docker-engine
移除历史镜像和容器等数据,如果之前已经安装了Docker,不移除,再次安装Docker,历史的镜像和容器都还存在,注意!!! 这行指令会删除历史的所有数据,请酌情执行
rm -rf /var/lib/docker
第二步
安装一些必要的系统工具sudo yum install -y yum-utils device-mapper-persistent-data lvm2
第三步:
添加软件源信息sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
第四步:
更新 yum 缓存sudo yum makecache fast
第五步:
安装 最新的Docker-cesudo yum -y install docker-ce
安装指定版本
sudo yum install -y docker-ce-18.09.7 docker-ce-cli-18.09.7 containerd.io
第六步:
启动 Docker 后台服务sudo systemctl start docker // 设置开机自启 sudo systemctl enable docker
其他指令(可以不执行的指令)
// 停止 sudo systemctl stop docker // 重启 sudo systemctl restart docker // 查看状态 sudo systemctl status docker // 设置开机自启 sudo systemctl enable docker // 取消开机自启 sudo systemctl disable docker // 查看是否已经配置开机自启 sudo systemctl is-enabled docker
Docker基础操作
Docker服务操作
启动docker
sudo systemctl start docker
停止docker
sudo systemctl stop docker
重启docker
sudo systemctl restart docker
Docker配置国内镜像源
vi /etc/docker/daemon.json
添加以下配置
{ "registry-mirrors": [ "https://registry.docker-cn.com", "http://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn" ] }
Docker镜像操作
查看镜像
sudo docker images
拉取镜像
docker pull 镜像名称:版本号 如:sudo docker pull redis:5.0.4
删除镜像
sudo docker rmi 镜像ID
运行镜像
sudo docker run --name test -p 80:80 -p 8081:8081 -v /data/log:/data/log --restart=always -d test:1.0.0
- 时间同步
-v /etc/localtime:/etc/localtime
- name
表示运行的容器的别名 - p
宿主机的端口域容器端口的映射关系 - v
文件挂载 - restart
重启方式,自动重启 - d
后台运行
- 时间同步
镜像打标签
sudo docker tag 基础镜像:版本 镜像地址(默认docker仓库)/路径/镜像名称:版本 如:sudo docker tag redis:5.0.4 hub.c.163.com/test/redis:5.0.4
推送镜像
sudo docker push hub.c.163.com/test/redis:5.0.4
Docker容器操作
查看容器运行情况
sudo docker ps
查看所有的容器运行情况
sudo docker ps -a
查询特定的容器
sudo docker ps|grep redis
停止容器
sudo docker stop 容器id
重启容器
sudo docker restart 容器id
启动容器
sudo docker start 容器id
删除容器
sudo docker rm 容器id
查看容器运行的日志
sudo docker logs -f 容器ID
Docker运行日志保存的目录
cd /var/lib/docker/containers
清理Docker容器运行的日志
#首先进入上面的目录 #cd进入对应的容器 #清空 容器id-json.log的记录文件即可清理容器运行的日志 #或者停止、删除、重启镜像
进入容器
sudo docker exec -it 容器ID /bin/bash
使用exit退出
执行容器内的脚本
以Nginx检查配置文件是否正确测试sudo docker exec 容器ID /usr/sbin/Nginx -s reload
Docker-compose安装
方式一(推荐)
// 直接下载 // github的源 sudo curl -L https://github.com/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose // Daocloud镜像 curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose // 下载完成之后设置权限 sudo chmod +x /usr/local/bin/docker-compose // 卸载 sudo rm /usr/local/bin/docker-compose
方式二
//依赖python 可能受python版本的影响导致安装失败 yum install epel-release yum install -y python-pip pip install docker-compose
基础命令
#启动容器,如果镜像不存在则先下载镜像,如果容器没创建则创建容器,如果容器没启动则启动 docker-compose up -d #停止并移除容器 docker-compose down #重启服务 docker-compose restart
异常情况
Could not find a version that satisfies the requirement requests<3,>=2.20.0 (from docker-compose) (from versions: ) No matching distribution found for requests<3,>=2.20.0 (from docker-compose) You are using pip version 8.1.2, however version 19.3.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
pip install --upgrade pip // 升级成功即可安装
容器日志管理
容器的日志查看
sudo docker logs -f 容器ID
容器日志保存的位置
/var/lib/docker/containers/
以上路径下保存了当前容器的各种资源及配置,让容器被删除的时候,这里对应的文件夹也会被删除
以下红色部分即为容器运行时的日志带来的问题
当容器不删除重启的时候,这里的日志文件将会变得越来越大,占用了大量的内存资源;而实际的情况下,以SpringBoot项目为例,我们都会在项目里面定义自己的日志策略,因此容器运行时记录的运行日志实际上是一份多余的日志;既然是日志,必定就会占用IO,IO又是一个比较耗时的操作,而且文件越大,操作的性能就会下降。既然没啥用,那我们何不关掉它。
关闭容器运行日志
配置daemon.json
vim /etc/docker/daemon.json #添加以下配置 "log-driver":"none"
#重启dockerservice docker restart
移除并重新运行镜像,再来查看容器日志,发现*-json.log已经没有了
个人的建议是,测试环境是可以开启这个容器日志,如果镜像测试一旦稳定,推到生产的时候,出于性能考虑(因为这部分日志没有太大的价值),所以可以直接关闭掉;
限制容器运行日志大小
配置daemon.json
vim /etc/docker/daemon.json #添加以下配置 "log-driver":"json-file", "log-opts": {"max-size":"1m", "max-file":"3"}
- max-size
表示日志文件的大小 max-file
表示日志文件的个数,这里是3,那么最多会生成三个日志文件,每个日志文件最大1m;超过3个之后,会把最旧的那个文件给删除掉。
- max-size
定时任务清空日志
不太建议使用这种方式,虽然可以解决问题,但是我认为不是一个最好的方法
创建清除日志的脚本
vim /etc/docker/clean_docker_container_logs.sh#!/bin/sh echo "======== start clean docker containers logs ========" #查找/var/lib/docker/containers/路径下以-json.log结尾的文件 logs=$(find /var/lib/docker/containers/ -name *-json.log) for log in $logs do echo "clean logs : $log" #将对应文件的内容置为null cat /dev/null > $log # 或者 # echo "">$log done echo "======== end clean docker containers logs ========"
然后在crontab设置定时任务执行
#5分钟执行一次 */5 * * * * sh /etc/docker/clean_docker_container_logs.sh
关于Centos7下安装Docker和CentOS7下安装bmd采集卡的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于01_在centos7下安装Docker、CentOS 7下安装Docker、CentOS 7下安装Docker以及Docker的基本使用、CentOS 7下安装Docker及基础操作等相关知识的信息别忘了在本站进行查找喔。
本文标签: