Docker 的安装和配置
由于我没有买服务器,所以用的是VMware,配置了CentOS7的系统,网络使用NAT模式,通过Xshell工具通过ssh管道链接虚拟器。
Docker文档上的安装流程
帮助文档上有安装流程,按照官方文档来进行一次安装
1. 卸载旧版本
旧版本的Docker的名称为或。 在尝试安装新版本之前,请卸载任何此类旧版本以及关联的依赖项:
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2. 安装方法;
可以根据需要以不同的方式安装 Docker 引擎:
使用仓库安装
先安装Docker需要的环境
yum install -y yum-utils
设置镜像仓库 --官方仓库是docker 国外的
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
阿里云 docker镜像地址
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装最新版本的docker引擎
yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
安装特定版本的docker命令
ce 社区版 ee企业版
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io docker-buildx-plugin docker-compose-plugin
yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
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
例如
yum -y install docker-ce-18.09.9-3.el7 docker-ce-cli-18.09.9-3.el7
这里用的是最新版本安装
3.启动docker
systemctl start docker 启动docker守护进程
systemctl stp docker.sockert 关闭docker守护进程
我这里启动碰到报错
百度解决方案
地址 缺少service的解决方案
这是因为缺少 rhel-push-plugin.socket 单元,该单元是rhel-push-plugin软件包的一部分。所以我们执行以下指令就可以成功解决:
curl -sSL https://get.docker.com/ | sh
启动成功
由图可见 并无提示成功,所以要查看下是否安装成功
使用命令 查看docker 版本信息
docker version
通过运行映像来验证 Docker 引擎安装是否成功
docker run hello-world
启动信息是发现现在没有hello-world镜像 去社区下载 下载成功 运行 输出Hello from Docker! 运行成功。
卸载Dokcer删除依赖
yum remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
删除资源 docker 镜像资源默认都在这两个路径下
删除依赖
yum remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
删除镜像资源
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
如果是阿里云服务器还可以用阿里云镜像加速器,我用的本地虚拟机,所以就不配了
4 操作Docker
查看已经下载镜像
docker images
docker run 运行流程图
docker run imagesname
docker 是怎么 工作的
Docker 是一个 Client - service 客户端与服务的结构的系统,Docker的守护进程在主机上,通过Socker从客户端访问!
Docker-Service 接受到Docker-Client的命令就会执行
Docker 的运行原理图
Docker 为什么比虚拟机(VM)快
– Docker 有比虚拟机更少的抽象层
– Docker 利用的宿主机的内核,而vm用的是Guest OS 所以当我重新建造一个容器的时候我不需要像vm一样还要搭建操作系统内核,我直接利用了宿主机的操作系统,vm是分钟级 ,Docker 是秒级
–虚拟机的安全性比Docker高,因为他是完全隔离的
docker操作命令
可以通过官方文档查看
docker官方命令文档
docker info docker的系统信息 相比docker version 更加详细包括镜像和容器的数量
docker help 查看docker 的帮助命令
镜像命令
docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 17 months ago 13.3kB
REPOSITORY 镜像源
TAG 镜像标签
REPOSITORY 镜像ID
CREATED 镜像创建时间
SIZE 镜像大小
docker images 可选项
[root@localhost ~]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all 显示所有镜像
--digests 显示摘要
-f, --filter filter 根据条件过滤输出
--format string 使用自定义模板格式化输出:
-q, --quiet 只显示镜像ID
可选项 -a -q
[root@localhost ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 17 months ago 13.3kB
[root@localhost ~]# docker images -q
feb5d9fea6a5
docker搜索命令 docker search
docker hub网页版
See 'docker --help'
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13860 [OK]
mariadb MariaDB Server is a high performing open sou… 5286 [OK]
percona Percona Server is a fork of the MySQL relati… 599 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 745 [OK]
通过 --filter 过滤性筛选 筛选 收藏点赞大于3000的mysql
[root@localhost ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13860 [OK]
mariadb MariaDB Server is a high performing open sou… 5286 [OK]
或者直接-f关键字也行
[root@localhost ~]# docker search centos -f=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos DEPRECATED; The official build of CentOS. 7511 [OK]
查找到依赖通过 docker pull 命令下载镜像
[root@localhost ~]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Download an image from a registry
Aliases:
docker image pull, docker pull
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
下载mysql
[root@localhost ~]# docker pull mysql
Using default tag: latest 如果不写tag 就是最新版本
latest: Pulling from library/mysql
b4ddc423e046: Pull complete 分层下载 docker的核心
b338d8e4ffd1: Pull complete
b2b1b06949ab: Pull complete
daf393284da9: Pull complete
1cb8337ae65d: Pull complete
f6c2cc79221c: Pull complete
4cec461351e0: Pull complete
ab6bf0cba08e: Pull complete
8df43cafbd11: Pull complete
c6d0aac53df5: Pull complete
b24148c7c251: Pull complete
Digest: sha256:d8dc78532e9eb3759344bf89e6e7236a34132ab79150607eb08cc746989aa047 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest docker的真实地址
这两个命令达到的效果是一样的
docker pull mysql
dokcer pull io/library/mysql:latest
指定依赖版本下载
docker pull mysql:5.7
docker分层下载的体现 下载不同的mysql版本 他们其中共共有的层无需下载直接引用即可
Already Exists
docker 镜像删除命令
docker rmi
[root@localhost ~]# docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Aliases:
docker image rm, docker image remove, docker rmi
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
通过id 删除镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 4f06b49211c0 3 days ago 530MB
mysql 5.7 be16cf2d832a 3 weeks ago 455MB
hello-world latest feb5d9fea6a5 17 months ago 13.3kB
[root@localhost ~]# docker rmi -f 4f06b49211c0
Untagged: mysql:latest
Untagged: mysql@sha256:d8dc78532e9eb3759344bf89e6e7236a34132ab79150607eb08cc746989aa047
Deleted: sha256:4f06b49211c09ddf194684fbc6c02be56773227927b1e937b489ec414a04b3f7
Deleted: sha256:c37feeb65db60f2c4f050d31bbb15b6c484157dee1adb5c9606ac0365a23fb2f
Deleted: sha256:a2827d51846f5a47e17efcff29fd631a2919e3518986b6aca9595f41ffb91e08
Deleted: sha256:bdcbffd6fa64f16bdc359a95adc01e29a08c988d00c07d1e0886381691c5fbbb
Deleted: sha256:abab2cb7f5c06e876f20709316506250d22ecf1a9a80a7335b5856440ec62dea
Deleted: sha256:273a05d7983e9456fa42bb421abf0a0d6efc6ccfcef48222c046d76c22a18e39
Deleted: sha256:3b99ea3f5cbfaa1575a5e2509ec8597def22d4d79b390d75014917b6a5f642d4
Deleted: sha256:8563b361cdd8efc210b7467e47c263613a88c621a4b38970269f90406aa58131
Deleted: sha256:2a1b2d738042755133d039a7daf1748ccfa2720d268d5eba40c81b7916a9179c
Deleted: sha256:899199e05f011449cf6d6b7650b9eaff7633d9d0a7d879214ac68209c419617e
Deleted: sha256:039d7eb7d1f64c9901924f551cefdd49602294d22343035f1a86e1b7207a0ce7
Deleted: sha256:4286bd3f1f4a6020052b600032b18bd63add505dd2d98dfdf9857d8f19fd8afa
[root@localhost ~]#
也可以通过多个id一起删除
docker rmi -f 镜像id 镜像id 镜像id
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 4f06b49211c0 3 days ago 530MB
mysql 5.7 be16cf2d832a 3 weeks ago 455MB
[root@localhost ~]# docker rmi -f 4f06b49211c0 be16cf2d832a
Untagged: mysql:latest
Untagged: mysql@sha256:d8dc78532e9eb3759344bf89e6e7236a34132ab79150607eb08cc746989aa047
Deleted: sha256:4f06b49211c09ddf194684fbc6c02be56773227927b1e937b489ec414a04b3f7
Deleted: sha256:c37feeb65db60f2c4f050d31bbb15b6c484157dee1adb5c9606ac0365a23fb2f
Deleted: sha256:a2827d51846f5a47e17efcff29fd631a2919e3518986b6aca9595f41ffb91e08
Deleted: sha256:bdcbffd6fa64f16bdc359a95adc01e29a08c988d00c07d1e0886381691c5fbbb
Deleted: sha256:abab2cb7f5c06e876f20709316506250d22ecf1a9a80a7335b5856440ec62dea
Deleted: sha256:273a05d7983e9456fa42bb421abf0a0d6efc6ccfcef48222c046d76c22a18e39
Deleted: sha256:3b99ea3f5cbfaa1575a5e2509ec8597def22d4d79b390d75014917b6a5f642d4
Deleted: sha256:8563b361cdd8efc210b7467e47c263613a88c621a4b38970269f90406aa58131
Deleted: sha256:2a1b2d738042755133d039a7daf1748ccfa2720d268d5eba40c81b7916a9179c
Deleted: sha256:899199e05f011449cf6d6b7650b9eaff7633d9d0a7d879214ac68209c419617e
Deleted: sha256:039d7eb7d1f64c9901924f551cefdd49602294d22343035f1a86e1b7207a0ce7
Deleted: sha256:4286bd3f1f4a6020052b600032b18bd63add505dd2d98dfdf9857d8f19fd8afa
Untagged: mysql:5.7
Untagged: mysql@sha256:8cf035b14977b26f4a47d98e85949a7dd35e641f88fc24aa4b466b36beecf9d6
Deleted: sha256:be16cf2d832a9a54ce42144e25f5ae7cc66bccf0e003837e7b5eb1a455dc742b
Deleted: sha256:2dd6e094d35f48086adcdff89f36c9be8166fbbda0775b72efd9b12788a400a0
Deleted: sha256:01de218cde8a6eef7c19083f1a9dfdcc10e656651137c5083c8e8b293561e674
Deleted: sha256:84e3163c39fc29722fbea1461bc279bafc9d2da5937b1f4e5db6e08c6f08e3b4
Deleted: sha256:2b39b403c72a380afa25629a8b0af21e5bd33b071985740242e63856e6453359
Deleted: sha256:bf76a072ee83eef013d21ef4aed92cea89f1d15edd62ce0d4204268f513a2658
Deleted: sha256:8112761750f4100814858c647189c97db4129852fec2060580625254fa9b3440
Deleted: sha256:a527ebda1c7ee77668ecfec84a62ba7cc46769946fb565ff7895f2a0aecfd082
Deleted: sha256:ba8f679e76a9faba19756857e6f5d6809d8ba0a90507c6a815f3823244636e64
Deleted: sha256:51b0a4dc7d8cdf87950abd409fe3155df9118e4b33a1bbe91d6367d62557db87
Deleted: sha256:f3277814332978b36b309d241f692a182e37564d96a94fdde0382cb471953483
Deleted: sha256:c233345f327a72c79e2e19c75fcf9089a0c1488dfb66dd00d49fa2a5d1f76057
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
通过嵌套命令删除删除全部镜像
[root@localhost ~]# docker rmi -f $(docker images -aq)
Untagged: mysql:latest
Untagged: mysql@sha256:d8dc78532e9eb3759344bf89e6e7236a34132ab79150607eb08cc746989aa047
Deleted: sha256:4f06b49211c09ddf194684fbc6c02be56773227927b1e937b489ec414a04b3f7
Deleted: sha256:c37feeb65db60f2c4f050d31bbb15b6c484157dee1adb5c9606ac0365a23fb2f
Deleted: sha256:a2827d51846f5a47e17efcff29fd631a2919e3518986b6aca9595f41ffb91e08
Deleted: sha256:bdcbffd6fa64f16bdc359a95adc01e29a08c988d00c07d1e0886381691c5fbbb
Deleted: sha256:abab2cb7f5c06e876f20709316506250d22ecf1a9a80a7335b5856440ec62dea
Deleted: sha256:273a05d7983e9456fa42bb421abf0a0d6efc6ccfcef48222c046d76c22a18e39
Deleted: sha256:3b99ea3f5cbfaa1575a5e2509ec8597def22d4d79b390d75014917b6a5f642d4
Deleted: sha256:8563b361cdd8efc210b7467e47c263613a88c621a4b38970269f90406aa58131
Deleted: sha256:2a1b2d738042755133d039a7daf1748ccfa2720d268d5eba40c81b7916a9179c
Deleted: sha256:899199e05f011449cf6d6b7650b9eaff7633d9d0a7d879214ac68209c419617e
Deleted: sha256:039d7eb7d1f64c9901924f551cefdd49602294d22343035f1a86e1b7207a0ce7
Deleted: sha256:4286bd3f1f4a6020052b600032b18bd63add505dd2d98dfdf9857d8f19fd8afa
Untagged: mysql:5.7
Untagged: mysql@sha256:8cf035b14977b26f4a47d98e85949a7dd35e641f88fc24aa4b466b36beecf9d6
Deleted: sha256:be16cf2d832a9a54ce42144e25f5ae7cc66bccf0e003837e7b5eb1a455dc742b
Deleted: sha256:2dd6e094d35f48086adcdff89f36c9be8166fbbda0775b72efd9b12788a400a0
Deleted: sha256:01de218cde8a6eef7c19083f1a9dfdcc10e656651137c5083c8e8b293561e674
Deleted: sha256:84e3163c39fc29722fbea1461bc279bafc9d2da5937b1f4e5db6e08c6f08e3b4
Deleted: sha256:2b39b403c72a380afa25629a8b0af21e5bd33b071985740242e63856e6453359
Deleted: sha256:bf76a072ee83eef013d21ef4aed92cea89f1d15edd62ce0d4204268f513a2658
Deleted: sha256:8112761750f4100814858c647189c97db4129852fec2060580625254fa9b3440
Deleted: sha256:a527ebda1c7ee77668ecfec84a62ba7cc46769946fb565ff7895f2a0aecfd082
Deleted: sha256:ba8f679e76a9faba19756857e6f5d6809d8ba0a90507c6a815f3823244636e64
Deleted: sha256:51b0a4dc7d8cdf87950abd409fe3155df9118e4b33a1bbe91d6367d62557db87
Deleted: sha256:f3277814332978b36b309d241f692a182e37564d96a94fdde0382cb471953483
Deleted: sha256:c233345f327a72c79e2e19c75fcf9089a0c1488dfb66dd00d49fa2a5d1f76057
Untagged: hello-world:latest
Untagged: hello-world@sha256:6e8b6f026e0b9c419ea0fd02d3905dd0952ad1feea67543f525c73a0a790fefb
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
容器命令
有了镜像才可以创建容器,linux ,下载一个centos镜像来测试
docker pull centos
创建容器并启动
docker run [可选参数] image
#参数说明
--name=‘NAME’ # 容器名称 用来区分容器
-d # 后台方式运行
-it # 使用交互运行进入容器查看内容
-p # 指定容器端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-p 容器端口
容器端口
-p #随机指定端口
测试
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 17 months ago 231MB
[root@localhost ~]# docker run -it centos /bin/bash
[root@8ee05ffd8a68 /]#
启动并进入容器
从容器退回主机
[root@8ee05ffd8a68 /]# exit
exit
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# cd ..
[root@localhost /]# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost /]#
查看正在运行的镜像
docker ps # 查看正在运行的镜像
docker ps -a # 查看正在运行的镜像和历史运行的镜像
docker ps -a -n = 1 # 显示最近的1个创建的镜像如果-n=2 就显示最近两个运行的镜像
docker ps -a -n=1 -q # 显示最近一次运行的镜像的id
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5480f026edde centos "/bin/bash" About a minute ago Exited (0) About a minute ago youthful_dhawan
8ee05ffd8a68 centos "/bin/bash" 6 minutes ago Exited (0) 4 minutes ago busy_taussig
982ca69f95f3 feb5d9fea6a5 "/hello" About an hour ago Exited (0) About an hour ago dazzling_thompson
4bdb8f166ccb feb5d9fea6a5 "/hello" 2 hours ago Exited (0) 2 hours ago busy_haibt
[root@localhost ~]#
退出镜像
exit 停止镜像并退出
Ctrl + P + Q 不停止镜像退出
删除镜像
是用 docker rmi -f id 删除容器是 docker rm -f id 后面可以用 $(docker ps -aq)迭代删除
容器和镜像的区别是 容器是操作系统 如Cenos 或者linux 镜像是分层下载
docker rm 容器id 根据容器id删除容器 无法删除正在运行的容器
docker rm -f 容器id 根据容器id强制删除容器 可以删除正在运行容器
docker rm -f $(docker ps -aq) 删除所有容器
docker ps -aq |xargs docker rm -f 强制删除所有容器
启动和停止容器的操作
docker start 容器id 启动一个容器
docker restart 容器id 重启容器
docker stop 容器id 停止一个容器
docker kill 容器id 杀死一个容器
测试操作
[root@localhost ~]# docker ps #查看正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a70f55e71145 centos "/bin/bash" 46 seconds ago Up 27 seconds agitated_kalam
[root@localhost ~]# docker kill a70f55e71145 #杀死正在运行容器
a70f55e71145
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker run centos #启动容器
[root@localhost ~]# docker run -it centos #交互启动容器
[root@368fd611468a /]# exit
exit
[root@localhost ~]# docker ps -aq #查看历史
368fd611468a
a6d658764ac6
a70f55e71145
[root@localhost ~]# docker start 368fd611468a #通过id启动容器
368fd611468a
[root@localhost ~]# docker ps #启动成功
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
368fd611468a centos "/bin/bash" 27 seconds ago Up 3 seconds recursing_khayyam
[root@localhost ~]# docker stop 368fd611468a #通过stop 停止容器
368fd611468a
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker rm -f $(docker ps -aq) #删除所有容器历史
368fd611468a
a6d658764ac6
a70f55e71145
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
常用其他命令
后台启动命令
这里有个问题就是docker会对启动的容器进行判断,如果容器内没有前台服务,没有正在运行的线程会立即停止 ,从下面的代码里可以看出来,没有正在运行服务的容器会立即停止
docker run -d 容器名称 后台启动不进行交互
测试
[root@localhost ~]# docker run -d centos
612284b555d223f2584a3fa5a890fa666362bf485f4eda85c0e3e8f4c9e700aa
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
612284b555d2 centos "/bin/bash" 52 seconds ago Exited (0) 51 seconds ago elated_nobel
查看日志
docker logs 查看日志
docker logs -f -t --tail 10 容器id 显示该容器的前10条日志
-f 跟踪日志输出
-ft 跟踪日志输入输出附带时间信息并打印全部内容
-t 打印日志附带时间信息
--tail 条数 显示条数
[root@localhost ~]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Aliases:
docker container logs, docker logs
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)
通过shll脚本测试
[root@539ab5530fe5 bin]# sh -c "while true ;do echo aaabbbaaabbb;sleep 1 ;done"
aaabbbaaabbb
aaabbbaaabbb
aaabbbaaabbb
[root@localhost ~]# docker logs -f -t --tail 10 539ab5530fe5
2023-02-28T06:51:04.856231743Z aaabbbaaabbb
2023-02-28T06:51:05.861101778Z aaabbbaaabbb
2023-02-28T06:51:06.865359633Z aaabbbaaabbb
2023-02-28T06:51:07.867081079Z aaabbbaaabbb
2023-02-28T06:51:08.869044880Z aaabbbaaabbb
2023-02-28T06:51:09.870585301Z aaabbbaaabbb
2023-02-28T06:51:10.872561641Z aaabbbaaabbb
2023-02-28T06:51:11.875809915Z aaabbbaaabbb
2023-02-28T06:51:12.877703018Z aaabbbaaabbb
2023-02-28T06:51:13.879861961Z aaabbbaaabbb
由于循环条件为true 会一自输出 ctrl + C 就能停止
查看容器进程信息
docker top 容器id
[root@localhost ~]# docker top 539ab5530fe5
UID PID PPID C STIME TTY TIME CMD
root 4591 4572 0 14:47 pts/0 00:00:00 /bin/bash
root 4629 4591 0 14:50 pts/0 00:00:00 sh -c while true ;do echo aaabbbaaabbb;sleep 1 ;done
root 5253 4629 0 14:57 pts/0 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
[root@localhost ~]#
查看容器信息
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 17 months ago 231MB
[root@localhost ~]# docker inspect 5d0da3dc9764
[
{
"Id": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"RepoTags": [
"centos:latest"
],
"RepoDigests": [
"centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177"
],
"Parent": "",
"Comment": "",
"Created": "2021-09-15T18:20:05.184694267Z",
"Container": "9bf8a9e2ddff4c0d76a587c40239679f29c863a967f23abf7a5babb6c2121bf1",
"ContainerConfig": {
"Hostname": "9bf8a9e2ddff",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"/bin/bash\"]"
],
"Image": "sha256:f5b050f177fd426be8fe998a8ecf3fb1858d7e26dff4080b29a327d1bd5ba422",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"DockerVersion": "20.10.7",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "sha256:f5b050f177fd426be8fe998a8ecf3fb1858d7e26dff4080b29a327d1bd5ba422",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 231268856,
"VirtualSize": 231268856,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/c57ebdeb6bf4a2136318ec52a697a9cc96ed5a6d48f6f054df46b7e3fa6e18b4/merged",
"UpperDir": "/var/lib/docker/overlay2/c57ebdeb6bf4a2136318ec52a697a9cc96ed5a6d48f6f054df46b7e3fa6e18b4/diff",
"WorkDir": "/var/lib/docker/overlay2/c57ebdeb6bf4a2136318ec52a697a9cc96ed5a6d48f6f054df46b7e3fa6e18b4/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
进入容器
docker exec -it 容器id 控制台地址 # 执行新的容器
-it 通过交互模式进入容器
[root@localhost ~]# docker exec -it 539ab5530fe5 /bin/bash
[root@539ab5530fe5 /]#
docker attach 容器id #进入正在运行的容器
[root@localhost ~]# docker attach 539ab5530fe5
aaabbbaaabbb
aaabbbaaabbb
aaabbbaaabbb
^C
文件拷贝
拷贝是一个手动的过程,我们可以使用-v卷的技术,可以实现自动同步 容器目录与主机目录打通
把文件从容器内部考本到主机上
docker cp 容器id:路径 本机路径
[root@localhost ~]# docker run -it centos /bin/bash
[root@62d4ca6cad5d /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@62d4ca6cad5d /]# cd home
[root@62d4ca6cad5d home]# touch test.java
[root@62d4ca6cad5d home]# ls
test.java
[root@62d4ca6cad5d home]# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
62d4ca6cad5d centos "/bin/bash" 42 seconds ago Exited (0) 8 seconds ago vigorous_hawking
[root@localhost ~]# docker cp 62d4ca6cad5d:/home/test.java /home
Successfully copied 1.536kB to /home
[root@localhost ~]# cd ..
[root@localhost /]# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost /]# cd home
[root@localhost home]# ls
test.java
[root@localhost home]#
把文件从主机考本到容器内
[root@localhost /]# cd root
[root@localhost ~]# touch test2.java
[root@localhost ~]# ls
2 anaconda-ks.cfg test2.java
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker run -it centos /bin/bash
[root@98fe4826171f /]# [root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
98fe4826171f centos "/bin/bash" 26 seconds ago Up 26 seconds clever_wu
[root@localhost ~]# docker cp /root/test2.java 98fe4826171f:/home
Preparing to copy...
Copying to container - 1.536kB
Successfully copied 1.536kB to 98fe4826171f:/home
[root@localhost ~]# docker attach 98fe4826171f
[root@98fe4826171f /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@98fe4826171f /]# cd home
[root@98fe4826171f home]# ls
test2.java
[root@98fe4826171f home]#
查看内存情况
docker stats
[root@localhost ~]# docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
bfd7d77db6b2 tomact001 0.22% 105.8MiB / 1.777GiB 5.82% 26.5kB / 298kB 0B / 0B 33
5546e8e0810a nginx001 0.00% 1.438MiB / 1.777GiB 0.08% 1.46kB / 1.27kB 0B / 0B 2
5 .命令小结
练习
部署nginx 测试请求nginx
nginx在容器内部修改可以通过手工修改配置文件,或者用 -v卷 达到在宿主机修改文件容器内部文件就会修改
[root@localhost ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 18154 [OK]
bitnami/nginx Bitnami nginx Docker Image 151 [OK]
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 23 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 79
kasmweb/nginx An Nginx image based off nginx:alpine and in… 4
rancher/nginx-ingress-controller 11
rancher/nginx-ingress-controller-defaultbackend 2
bitnami/nginx-exporter 3
rancher/nginx 2
rapidfort/nginx-ib RapidFort optimized, hardened image for NGIN… 0
rapidfort/nginx RapidFort optimized, hardened image for NGINX 2
ibmcom/nginx-ingress-controller Docker Image for IBM Cloud Private-CE (Commu… 4
vmware/nginx-photon 1
ibmcom/nginx-ingress-controller-ppc64le Docker Image for IBM Cloud Private-CE (Commu… 0
bitnami/nginx-ldap-auth-daemon 3
ibmcom/nginx-ppc64le Docker image for nginx-ppc64le 0
vmware/nginx 2
rancher/nginx-conf 0
rapidfort/nginx-official RapidFort optimized, hardened image for NGIN… 1
linuxserver/nginx An Nginx container, brought to you by LinuxS… 187
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 72 [OK]
bitnami/nginx-intel 1
rancher/nginx-ssl 0
circleci/nginx This image is for internal use 2
continuumio/nginx-ingress-ws 0
[root@localhost ~]# dcoker search nginx --filer=STARS=3000
-bash: dcoker: 未找到命令
[root@localhost ~]# dcoker search nginx --filter=STARS=3000
-bash: dcoker: 未找到命令
[root@localhost ~]# docker search nginx --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 18154 [OK]
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
bb263680fed1: Pull complete
258f176fd226: Pull complete
a0bc35e70773: Pull complete
077b9569ff86: Pull complete
3082a16f3b61: Pull complete
7e9b29976cce: Pull complete
Digest: sha256:6650513efd1d27c1f8a5351cbd33edf85cc7e0d9d0fcb4ffb23d8fa89b601ba8
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 3f8a00f137a0 2 weeks ago 142MB
centos latest 5d0da3dc9764 17 months ago 231MB
[root@localhost ~]# docker run -d --name nginx001 -p 8080:80 nginx
5546e8e0810ad8e3ae820b7157b7c32a0e02f2bbf36871a5ff98e082ddd53ee3
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5546e8e0810a nginx "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx001
[root@localhost ~]# curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]#
部署tomecat
使用命令docker run -it --rm tomcat:9.0 相比与普通启动 多了个–rm 代表这个容器停止之后直接删除了,docker ps -a 查不到了,用完即删除
测试
[root@localhost ~]# docker run -it --rm tomcat:9.0
Unable to find image 'tomcat:9.0' locally
9.0: Pulling from library/tomcat
10ac4908093d: Pull complete
6df15e605e38: Pull complete
2db012dd504c: Pull complete
8fa912900627: Pull complete
f8fe20946c04: Pull complete
ed4e24f89720: Pull complete
78026b68f48e: Pull complete
Digest: sha256:d7281362164662e538e29ab5ef26f2007ffe05c4b9237b0fa2561dca3332f783
Status: Downloaded newer image for tomcat:9.0
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /opt/java/openjdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bb766d2ddf86 tomcat:9.0 "catalina.sh run" 10 seconds ago Up 10 seconds 8080/tcp boring_lehmann
5546e8e0810a nginx "/docker-entrypoint.…" 17 minutes ago Up 10 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx001
[root@localhost ~]# docker stop bb766d2ddf86
bb766d2ddf86
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5546e8e0810a nginx "/docker-entrypoint.…" 17 minutes ago Up 11 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx001
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5546e8e0810a nginx "/docker-entrypoint.…" 17 minutes ago Up 11 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx001
98fe4826171f centos "/bin/bash" 40 minutes ago Exited (1) 20 minutes ago clever_wu
62d4ca6cad5d centos "/bin/bash" 46 minutes ago Exited (0) 45 minutes ago vigorous_hawking
[root@localhost ~]#
由于我用的本地虚拟机,所以我配置完成之后使用局域网访问
linux查看ip命令 ip addr
发现请求不到,原来是docker的tomcat是阉割版,需要把 webapps.dist中的文件复制到webapps文件中
复制webapps.dist文件到webapps中
root@bfd7d77db6b2:/usr/local/tomcat# ls
bin conf lib logs NOTICE RELEASE-NOTES temp webapps.dist
BUILDING.txt CONTRIBUTING.md LICENSE native-jni-lib README.md RUNNING.txt webapps work
root@bfd7d77db6b2:/usr/local/tomcat# cd webapps
root@bfd7d77db6b2:/usr/local/tomcat/webapps# ls
root@bfd7d77db6b2:/usr/local/tomcat/webapps# clear
root@bfd7d77db6b2:/usr/local/tomcat/webapps# ls
root@bfd7d77db6b2:/usr/local/tomcat/webapps# cd ..
root@bfd7d77db6b2:/usr/local/tomcat# ls
bin conf lib logs NOTICE RELEASE-NOTES temp webapps.dist
BUILDING.txt CONTRIBUTING.md LICENSE native-jni-lib README.md RUNNING.txt webapps work
root@bfd7d77db6b2:/usr/local/tomcat# cd webapps
root@bfd7d77db6b2:/usr/local/tomcat/webapps# ls
root@bfd7d77db6b2:/usr/local/tomcat/webapps# cp -r webapps.dist/* webapps
cp: cannot stat 'webapps.dist/*': No such file or directory
root@bfd7d77db6b2:/usr/local/tomcat/webapps# cd ..
root@bfd7d77db6b2:/usr/local/tomcat# cp -r webapps.dist/* /webapps
cp: target '/webapps' is not a directory
root@bfd7d77db6b2:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@bfd7d77db6b2:/usr/local/tomcat# cd webapps
root@bfd7d77db6b2:/usr/local/tomcat/webapps# ls
docs examples host-manager manager ROOT
部署成功
安装elasticsearch es + kibana
docker hub 官方es镜像地址
带网络配置
docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:tag
不带网络配置 本次使用这个 7.17.9
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.17.9
由于es启动非常耗资源 所以通过参数限制他说占用的资源
限制占用空间大小
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.17.9
[root@localhost ~]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.17.9
8e3ac81dd5161afa4f80be2aaa2a1ca6224ae1926e0fe7422dfa51fd6608c894
[root@localhost ~]# docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
8e3ac81dd516 elasticsearch 0.05% 159.1MiB / 1.777GiB 8.75% 586B / 0B 215MB / 0B 24