资讯详情

Docker

文章目录

    • 1. Docker 安装方式
      • 1.1 script 脚本安装
      • 1.2 yum 源 安装
      • 1.3 rpm 安装包
    • 2. 实际安装 Docker
      • 2.1 使用 rpm 安装包进行 Docker 系统配置的安装
      • 2.2 正常安装按官方文档正常安装 docker
      • 2.3 卸载 docker
    • 3. Docker 加速配置 (RPM 安装包 用)
      • 3.1 阿里云镜像加速器
    • 4. 底层原理
    • 5. docker 常用命令
      • 5.1 基础命令
      • 5.2 镜像命令
        • (1) docker images 查看当地主机上的所有镜像
        • (2) docker search 搜索镜像
        • (3) docker pull 镜像名[:tag] 下载镜像
        • (4) docker rmi 删除镜像
      • 5.3 容器命令
        • 5.3.1 拉取一个 centos 容器
        • 5.3.2 运行容器
        • 5.3.3 进入容器
        • 5.3.4 退出容器
        • 5.3.5 查看操作过的容器列表
        • 5.3.6 删除容器
        • 5.3.7 启动和停止容器
        • 5.3.8 导入和导出容器
      • 5.4 其它常用命令
        • 5.4.1 日志的查看
        • 5.4.2 查看容器中的过程信息
        • 5.4.3 查看容器元数据
        • 5.4.4 进入当前正在运行的容器
        • 5.4.5 拷贝操作
            • (1) 复制操作命令
            • (2) 拷贝测试
        • 5.4.6 其它容器管理命令
            • (1) docker pause
            • (2) docker port
            • (3) docker rename
            • (4) docker stats
            • (5) docker top
            • (6) docker unpause
    • 6. 常用容器部署
      • 6.1 部署 nginx
        • (1) 搜索并下载镜像
        • (2) 运行镜像
        • (3) 配置文件
        • (4) 访问测试
        • (5) 容器内安装 vim
      • 6.2 部署 tomcat
        • (1) 下载镜像并运行
        • (2) 进入容器
        • (3) 访问测试
    • ! Docker容器使用问题:Failed to get D-Bus connection: Operation not permitted
            • 解决方案 容器以特权模式运行
    • 7. Docker 镜像 详解
      • 7.1 什么是镜像
      • 7.2 docker 镜像 加载原理
      • 7.3 镜像的分层
        • 7.3.1 分层解析
      • 7.4 提交镜像
        • 7.4.1 提交测试
        • (1) 启动官方 tomcat 镜像
        • (2) 发现默认情况 tomcat 没有 webapps 应用认官方镜像 webapps 下面没有文件)
        • (3) 手动将 webapps.dist 递归复制下面的文件 webapps 目录下
        • (4) 通过操作容器 commit 作为新镜像提交
        • (5) 运行提交的新镜像
        • (6) 导入和导出镜像
    • 8. 图形管理工具 Portaninter 安装
    • 9. 容器数据卷
      • 9.1 文件挂载方式
      • 9.2 安装 MySQL 建立数据卷同步技术
      • 9.3 常用的数据卷命令
            • 1. 创建数据卷
            • 2. 查看所有数据卷
            • 3. 查看指定数据卷的信息
            • 4. 删除数据卷
            • 5. 删除容器时,删除相关卷
            • 6. 无主数据卷可能占据大量控件,清理时的命令
      • 9.4 Dockerfile 数据卷设置在中间
      • 9.5 容器间的数据同步(数据卷容器,备份)通过数据卷实现
    • 10. Dockerfile
      • 10.1 Dockerfile 介绍
      • 10.2 Dockerfile 指令说明
      • 10.3 通过 Dockerfile 构建一个 centos镜像
    • 11. 发布自己的镜像
    • 12. Docker 网络
      • 12.1 docker 网络模式
            • host 模式
            • none 模式
      • 12.2 单向通信 --link
      • 12.3 创建桥接网络实现容器间的双向通信
      • 12.4 更改 docker0 默认网桥的 IP 段
      • 12.5 创建网络/容器时指定 IP
    • 13. Docker-compose 编排工具
      • 13.1 compose 工具的安装与卸载
            • 安装
            • 卸载
      • 13.2 compose 常用命令
            • 列出容器 ps
            • 查看服务日志输出 logs
            • 输出绑定的公共端口 port
            • 重新构建服务 build
            • 启动服务 start
            • 停止服务 stop
            • 删除已停止服务的容器 rm
            • 创建和启动容器 up
            • 进入运行中的容器 exec
            • 其他管理命令
      • 13.3 docker-compose.yml 文件
            • 编写规则
      • 13.4 docker-compose.yml 一键部署 WP 博客
    • 14. docker swarm 编排工具
      • 14.1 相关概念
            • swarm
            • 节点
            • 服务
            • 任务
      • 14.2 swarm 常用命令
        • docker swarm 管理 swarm 集群
        • docker service 管理服务
        • docker node 管理 swarm 集群中的节点
      • 14.3 排空容器,删除节点
      • 14.4 docker swarm 创建 nginx 集群
      • 14.5 节点宕机情况
    • ?. Docker 拉取 WordPress 应用
      • ?.1 centos 7 Docker容器启动报WARNING: IPv4 forwarding is disabled. Networking will not work
    • 云课堂 Dockerfile 构建 Java网站镜像
            • 1. 准备两个必要的压缩安装包放入指定路径,创建 dockerfile 文件 与两个压缩安装包在同一路径
            • 2. 编辑 dockerfile 文件 内容
            • 3. 构建镜像,生成容器
    • 云课堂 Dockerfile 构建 nginx 镜像

1. Docker 安装方式

1.1 script 脚本安装

yum update
$ curl -sSL https://get.docker.com/ | sh
systemctl start docker
systemctl enable docker
docker run hello-world

1.2 yum 源 安装

yum update
cat >/etc/yum.repos.d/docker.repo <<-EOF
[dockerrepo]
name=Docker Repository
baseurl=http://yum.dockerproject.org/repo/main/centos/7
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg EOF

1.3 rpm 安装包

yum update
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

2. 实际安装 Docker

官方文档:https://docs.docker.com/

2.1 使用 rpm 安装包进行 Docker 安装的系统配置

关闭 firewalld 防火墙
systemctl stop firewalld
systemctl disable firewalld

禁用 selinux		 /etc/selinux/config
getenforce   -->    Disabled    

开启 iptables (Docker 环境需要)
systemctl start iptables
		如果没有可以安装	yum -y install iptables-services
systemctl start iptables
systemctl enable iptables
清空原 iptables 安全规则后再保存配置
iptables -F
service iptables save

最后 reboot 重启虚拟机,重启后默认会采用最新版本的内核

[root@localhost ~]# mkdir docker
将 两个 rpm 安装包移动到 docker 目录下	
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/ 下的两个 rpm 安装包
   docker-ce-17.03.0.ce-1.el7.centos.x86_64.rpm	docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm
[root@localhost ~]# cd docker/
[root@localhost docker]# yum -y install * 

安装完毕后启用 docker 服务
systemctl start docker
systemctl enable docker
systemctl status docker
docker version

成功开启服务后,利用 docker 官方给出的测试镜像来检测 docker 安装
[root@localhost docker]# docker run hello-world

使用 docker images 命令 可以查看当前系统中使用 docker 下载好的所有镜像文件

2.2 根据官方文档正常安装 docker

1.卸载旧的版本
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2.需要的安装包
sudo yum install -y yum-utils

3.设置镜像的仓库
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo		# 默认是国外镜像
	
sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo		# 国内阿里云镜像
	
4.更新 yum 软件包索引(非必需)
yum makecache fast

5.安装 docker 	docker-ce 社区版	docker-ee 企业版
yum install docker-ce docker-ce-cli containerd.io

6.启动 docker
systemctl start docker

7.查看 docker 版本号,运行 hello-world 测试镜像
docker version
docker run hello-world

8.查看下载好的 hello-world 镜像
docker images

2.3 卸载 docker

1.删除依赖
yum remove docker-ce docker-cd-cli containerd.io

2.删除资源
rm -rf /var/lib/docker		# docker 的默认工作路径

3. Docker 加速配置 (RPM 安装包 用)

为了加速下载国外的镜像文件

阿里云 Docker 官网: https://dev.aliyun.com/search.html
cp /lib/systemd/system/docker.service	/etc/systemd/system/docker.service
chmod 777 /etc/systemd/system/docker.service
vim /etc/systemd/system/docker.service
	ExecStart=/user/bin/dockerd-current	 --registry-mirror=https://kfp63jaj.mirror.aliyuncs.com  
systemctl daemon-reload
systemctl restart docker
ps -ef | grep docker

3.1 阿里云镜像加速器

sudo mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<-'EOF'
{ 
        
  "registry-mirrors": ["https://be1oqm8e.mirror.aliyuncs.com"]
}
EOF

sudo systemctl daemon-reload

sudo systemctl restart docker

4. 底层原理

1. docker 是怎么工作的
	docker 是一个 client-server 结构的系统,docker 的守护进程运行在主机上,通过 socket 从 客户端 访问
	docker server 接收到 docker-client 的指令,就会执行这个命令
	
2. docker 为什么比 VM 快
	(1) docker 有着比虚拟机更少的额抽象层
	(2) docker 利用的是宿主机的内核,VM 则是需要 Guest OS
  在新建一个容器的时候,docker 不需要像虚拟机一样重新加载一个操作系统内核,可以避免引导,而虚拟机需要加载 Guest OS,docker 利用宿主机的操作系统可以省略这个过程,所以会更快速更高效

5. docker 常用命令

官方帮助文档:https://docs.docker.com/engine/reference/commandline

5.1 基础命令

docker version          #查看docker的版本信息
docker info             #查看docker的系统信息,包括镜像和容器的数量
docker 命令 --help       #帮助命令(可查看可选的参数)
docker COMMAND --help

命令的帮助文档地址: https://docs.docker.com/engine/reference/commandline/docker/

5.2 镜像命令

(1) docker images 查看本地主机上的所有镜像

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    bf756fb1ae65   11 months ago   13.3kB

#解释:
1.REPOSITORY  镜像的仓库源
2.TAG  镜像的标签
3.IMAGE ID 镜像的id
4.CREATED 镜像的创建时间
5.SIZE 镜像的大小

# 可选参数
-a/--all 列出所有镜像
-q/--quiet 只显示镜像的id

(2) docker search 搜索镜像

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10308     [OK]
mariadb                           MariaDB is a community-developed fork of MyS…   3819      [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   754                  [OK]
percona                           Percona Server is a fork of the MySQL relati…   517       [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   86
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   79
centurylink/mysql                 Image containing mysql. Optimized to be link…   60                   [OK]


#可选参数
Search the Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results (default 25)
      --no-trunc        Don't truncate output
           
# 搜索收藏数大于3000的镜像
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   10308     [OK]
mariadb   MariaDB is a community-developed fordockerk of MyS…   3819      [OK]

(3) docker pull 镜像名[:tag] 下载镜像

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull mysql
Using default tag: latest            #如果不写tag默认就是latest
latest: Pulling from library/mysql
6ec7b7d162b2: Pull complete          #分层下载,docker image的核心-联合文件系统
fedd960d3481: Pull complete
7ab947313861: Pull complete
64f92f19e638: Pull complete
3e80b17bff96: Pull complete
014e976799f9: Pull complete
59ae84fee1b3: Pull complete
ffe10de703ea: Pull complete
657af6d90c83: Pull complete
98bfb480322c: Pull complete
6aa3859c4789: Pull complete
1ed875d851ef: Pull complete
Digest: sha256:78800e6d3f1b230e35275145e657b82c3fb02a27b2d8e76aac2f5e90c1c30873 	#签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest  		
#下载来源的真实地址  #docker pull mysql 等价于 docker pull docker.io/library/mysql:latest

指定版本下载

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
6ec7b7d162b2: Already exists
fedd960d3481: Already exists
7ab947313861: Already exists
64f92f19e638: Already exists
3e80b17bff96: Already exists
014e976799f9: Already exists
59ae84fee1b3: Already exists
7d1da2a18e2e: Pull complete
301a28b700b9: Pull complete
529dc8dbeaf3: Pull complete
bc9d021dc13f: Pull complete
Digest: sha256:c3a567d3e3ad8b05dfce401ed08f0f6bf3f3b64cc17694979d5f2e5d78e10173
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

(4) docker rmi 删除镜像

#1.删除指定的镜像id
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f  镜像id

#2.删除多个镜像id
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f  镜像id 镜像id 镜像id

#3.删除全部的镜像id (采用镜像ID的方式一次性删除所有镜像)
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker rmi -f  $(docker images -aq)

5.3 容器命令

5.3.1 拉取一个 centos 容器

docker pull centos

5.3.2 运行容器

docker run [可选参数] image

#参数说明
--name="名字"           指定容器名字
-d                     后台方式运行
-it                    使用交互方式运行,进入容器查看内容
-p                     指定容器的端口
	(
		-p ip:主机端口:容器端口  配置主机端口映射到容器端口
		-p 主机端口:容器端口
		-p 容器端口
					)
-P                     随机指定端口(大写的P)

5.3.3 进入容器

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -it centos /bin/bash
[root@bd1b8900c547 /]# ls      
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

5.3.4 退出容器

#exit 停止并退出容器
#Ctrl+P+Q  不停止容器并退出(容器于后台运行)
[root@bd1b8900c547 /]# exit
exit
[root@iZwz99sm8v95sckz8bd2c4Z ~]#

5.3.5 列表查看运行过的容器

#docker ps 
     # 列出当前正在运行的容器
-a   # 列出所有容器的运行记录
-n=? # 显示最近创建的n个容器
-q   # 只显示容器的编号

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
bca129320bb5   centos         "/bin/bash"   4 minutes ago   Exited (0) 3 minutes ago             optimistic_shtern
bd1b8900c547   centos         "/bin/bash"   6 minutes ago   Exited (0) 5 minutes ago             cool_tesla
cf6adbf1b506   bf756fb1ae65   "/hello"      5 hours ago     Exited (0) 5 hours ago               optimistic_darwin

5.3.6 删除容器

docker rm 容器id                 	   #删除指定的容器,不能删除正在运行的容器,强制删除使用 rm -f
docker rm -f $(docker ps -aq)   	#删除所有的容器
docker rm -v 容器ID                  #删除容器挂载的数据卷
docker ps -a -q|xargs docker rm 	#删除所有的容器

5.3.7 启动和停止容器

docker start 容器id          #启动容器
docker restart 容器id        #重启容器
docker stop 容器id           #停止当前运行的容器
docker kill 容器id           #强制停止当前容器

5.3.8 容器的导入和导出

:如果要导出某个窗口到本地,可以用 docker export 命令,可将容器导出为 tar 文件格式

docker export [options] container

options 为 -o 参数,表示指定导出的 tar 文件名

将容器名为 mycentos 的容器导出,文件格式为 "cent-日期"
	docker export -o centos-`date +%y%m%d`.tar mycentos

:可以用 docker import 命令导入一个镜像,类型为 tar 文件

docker import [option] [repository[:tag]]

5.4 其他常用命令

5.4.1 日志的查看

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker logs --help

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
  -n, --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)

常用:
docker logs -tf 容器id
docker logs --tail number 容器id #num为要显示的日志条数


#docker容器后台运行,必须要有一个前台的进程,否则会自动停止

#编写shell脚本循环执行,使得centos容器保持运行状态
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker run -d centos /bin/sh -c "while true;do echo hi;sleep 5;done"
c703b5b1911ff84d584390263a35707b6024816e1f46542b61918a6327a570dc
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
c703b5b1911f   centos    "/bin/sh -c 'while t…"   13 seconds ago   Up 10 seconds             pedantic_banach
[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker logs -tf --tail 10 c703b5b1911f
2020-12-27T03:34:07.255599560Z hi
2020-12-27T03:34:12.257641517Z hi
2020-12-27T03:34:17.259706294Z hi
2020-12-27T03:34:22.261693707Z hi
2020-12-27T03:34:27.262609289Z hi
2020-12-27T03:34:32.267862677Z hi
2020-12-27T03:34:37.270382873Z hi
2020-12-27T03:34:42.272414182Z hi
2020-12-27T03:34:47.274823243Z hi
2020-12-27T03:34:52.277419274Z hi

5.4.2 查看容器中的进程信息

docker top 容器ID

[root@iZwz99sm8v95sckz8bd2c4Z ~]# docker top c703b5b1911f
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                11156               11135               0                   11:31               ?                   00:00:00            /bin/sh -c while true;do echo hi;sleep 5;done
root                11886               11156               0                   11:43               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5

5.4.3 查看容器的元数据

docker inspect 容器ID

[root@localhost /]# docker inspect 675d09b7df1b
[
    { 
        
        "Id": "675d09b7df1b8dca7eb2134e73fbc020bdaf12dc2eaf1e9861d71152c5305199",
        "Created": "2022-02-01T09:20:22.252323295Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": { 
        
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 66230,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-02-01T09:20:22.672433457Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/675d09b7df1b8dca7eb2134e73fbc020bdaf12dc2eaf1e9861d71152c5305199/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/675d09b7df1b8dca7eb2134e73fbc020bdaf12dc2eaf1e9861d71152c5305199/hostname",
        "HostsPath": "/var/lib/docker/containers/675d09b7df1b8dca7eb2134e73fbc020bdaf12dc2eaf1e9861d71152c5305199/hosts",
        "LogPath": "/var/lib/docker/containers/675d09b7df1b8dca7eb2134e73fbc020bdaf12dc2eaf1e9861d71152c5305199/675d09b7df1b8dca7eb2134e73fbc020bdaf12dc2eaf1e9861d71152c5305199-json.log",
        "Name": "/practical_taussig",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": { 
        
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": { 
        
                "Type": "json-file",
                "Config" 

标签: 连接器df1ec

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台