目录
5.docker start/stop 启动和停止容器的操作
5.docker attach/exec 进入当前正在运行的容器
一、docker帮助命令
docker version #显示docker的版本信息
docker info #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令
二、docker镜像命令
1.docker images 查看镜像
docker images #查看所有本地主机上的镜像
# 解释
REPOSITORY # 镜像的仓库
TAG # 镜像的标签
IMAGE ID # 镜像的ID
CREATED # 镜像的创建时间
SIZE # 镜像的大小
# 可选项
--all , -a # 列出所有镜像
--quiet , -q # 只显示镜像的id
2.docker search 搜索镜像
[root@WRF /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL
mysql MySQL is a widely used, open-source relation… 14809 [OK]
mariadb MariaDB Server is a high performing open sou… 5658 [OK]
percona Percona Server is a fork of the MySQL relati… 624 [OK]
# 可选项
--filter=STARS=3000 # 搜素出来的镜像就是STARS大于3000的
[root@WRF /]# docker search mysql --filter=STARS=1000
NAME DESCRIPTION STARS OFFICIAL
mysql MySQL is a widely used, open-source relation… 14809 [OK]
mariadb MariaDB Server is a high performing open sou… 5658 [OK]
3.docker pull 拉取镜像
[root@WRF /]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
772a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
# 等价于
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@WRF /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
4.docker rmi 删除镜像
[root@WRF /]# docker rmi -f IMAGE ID # 删除指定镜像
[root@WRF /]# docker rmi -f IMAGE ID1 IMAGE ID2 IMAGE ID3 # 删除多个镜像
[root@WRF /]# docker rmi -f $(docker images -aq) # 删除所有镜像
三、docker容器命令
以centos为例,通过centos镜像创建容器【先docker pull centos,拉取centos镜像】
1.docker run 新建容器并启动
docker run [可选参数] image
# 参数说明
--name=“Name” 容器名字 tomcat01 tomcat02 用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-p 随机指定端口(大写P)
# 测试,启动并进入容器
[root@WRF /]# docker run -it centos /bin/bash
[root@WRF /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 从容器中退回主机
[root@WRF /]# exit
exit
[root@WRF /]# ls
bin boot dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
2.docker ps 列出所有运行的容器
# docker ps 命令
# 列出当前正在运行的容器
-a # 列出正在运行的容器包括历史容器
-n=? # 显示最近创建的容器
-q # 只显示当前容器的编号
[root@WRF /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@WRF /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8ea346bdac47 centos "/bin/bash" 18 hours ago Exited (0) 18 hours ago loving_wozniak
75bc510c2814 hello-world "/hello" 40 hours ago Exited (0) 40 hours ago focused_sammet
[root@WRF /]# docker ps -qa
8ea346bdac47
75bc510c2814
3.exit 退出容器
exit # 直接退出容器并关闭
Ctrl + P + Q # 容器不关闭退出
4.docker rm 删除容器
docker rm -f 容器id # 删除指定容器
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -a -q|xargs docker rm -f # 删除所有的容器
5.docker start/stop 启动和停止容器的操作
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前正在运行的容器
docker kill 容器id # 杀掉当前的容器(不能再次启动,可查到历史运行记录)
四、docker其他常用命令
1.docker run -d 后台启动容器
# 命令 docker run -d 镜像名
[root@WRF /]# docker run -d centos
# 问题 docker ps, 发现centos停止了
# 常见的坑, docker 容器使用后台运行, 就必须要有一个前台进程,docker发现没有应用,就会自动停止
# 如这样启动nginx, 容器启动后,发现自己没有提供服务,就会立即停止,就是没有程序了
2.docker logs 查看日志
docker logs -tf --tail number 容器id
# 显示日志
-tf # 显示日志
--tail number # 显示日志条数
# 自己编写一段shell脚本
[root@WRF /]# docker run -d centos /bin/sh -c "while true;do echo gengge;sleep 1;done"
a0d580a21251da97bc050763cf2d5692a455c228fa2a711c3609872008e654c2
[root@WRF /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0d580a21251 centos "/bin/sh -c 'while t…" 3 seconds ago Up 1 second lucid_black
3.docker top 查看容器中进程信息
[root@WRF /]# docker top b9a42ad79eb2
UID PID PPID C STIME TTY TIME CMD
root 20834 20812 0 11:09 pts/0 00:00:00 /bin/bash
4.docker inspect 查看镜像/容器的元数据
# 命令
docker inspect 镜像id/容器id
5.docker attach/exec 进入当前正在运行的容器
# 我们通常容器使用后台方式运行的, 需要进入容器,修改一些配置
# 方式一
docker exec -it 容器id /bin/bash
# 测试
[root@WRF /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a438f27d3151 centos "/bin/bash" About a minute ago Up About a minute objective_mccarthy
[root@WRF /]# docker exec -it a438f27d3151 /bin/bash
[root@a438f27d3151 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@a438f27d3151 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 03:26 pts/0 00:00:00 /bin/bash
root 15 0 0 03:28 pts/1 00:00:00 /bin/bash
root 30 15 0 03:28 pts/1 00:00:00 ps -ef
# 方式二
docker attach 容器id
6.docker cp 从容器中拷贝文件到主机
docker cp 容器id:容器内路径 目的地主机路径
[root@WRF /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a438f27d3151 centos "/bin/bash" 17 minutes ago Up 17 minutes objective_mccarthy
[root@WRF /]# docker attach a438f27d3151
[root@a438f27d3151 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@a438f27d3151 /]# cd home
[root@a438f27d3151 home]# ls
[root@a438f27d3151 home]# touch test.java
[root@a438f27d3151 home]# ls
test.java
[root@a438f27d3151 home]# exit
exit
[root@WRF /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@WRF /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a438f27d3151 centos "/bin/bash" 20 minutes ago Exited (0) 31 seconds ago objective_mccarthy
3a61728e3684 mysql "docker-entrypoint.s…" About an hour ago Exited (137) 54 minutes ago elastic_dhawan
34f288ef6677 mysql "docker-entrypoint.s…" About an hour ago Exited (0) About an hour ago quirky_kepler
[root@WRF /]# docker cp a438f27d3151:/home/test.java home
Successfully copied 1.54kB to /home
[root@WRF /]# cd home
[root@WRF home]# ls
test.java
五、docker部署软件练习
1.安装nginx
# 1. 搜索镜像 search 建议去docker hub搜索,可以看到帮助文档
# 2. 下载镜像 pull
# 3. 运行测试
[root@WRF /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 9 months ago 13.3kB
nginx latest 605c77e624dd 2 years ago 141MB
mysql 5.7 c20987f18b13 2 years ago 448MB
mysql latest 3218b38490ce 2 years ago 516MB
centos latest 5d0da3dc9764 2 years ago 231MB
# -d 后台运行
# -name 给容器命名
# -p 宿主机端口:容器内部端口
[root@WRF /]# docker run -d --name nginx01 -p 3344:80 nginx
713d1458c0407a9429b4e44b4b818f60c0ef311c40e4756a5e42073b9059591f
[root@WRF /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
713d1458c040 nginx "/docker-entrypoint.…" 13 seconds ago Up 12 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
# 发起本地的一个url请求
[root@WRF /]# curl localhost:3344
<!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>
2.安装tomcat
# 官方的使用
docker run -it --rm tomcat:9.0
# 我们之前的启动都是后台的,停止了容器之后, 容器还是可以查到,docker run -it --rm 一般用来测试,用完就删
# 下载再启动
docker pull tomcat
# 启动运行
docker run -d -p 3355:8080 --name tomcat01 tomcat
# 测试访问没有问题
# 进入容器
docker exec -it tomcat01 /bin/bash
# 发现问题:1.linux命令少了, 2. webapps下内容为空,阿里云净吸纳过默认是最小的镜像,所有不必要的都剔除了,保证最小可运行环境即可
#复制webapps.dist下所有内容到webapps目录下
[root@WRF /]# docker exec -it tomcat01 /bin/bash
root@5d7d12b5eb9c:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
root@5d7d12b5eb9c:/usr/local/tomcat# cd webapps.dist
root@5d7d12b5eb9c:/usr/local/tomcat/webapps.dist# ls
ROOT docs examples host-manager manager
root@5d7d12b5eb9c:/usr/local/tomcat/webapps.dist# cd ..
root@5d7d12b5eb9c:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@5d7d12b5eb9c:/usr/local/tomcat# cd webapps
root@5d7d12b5eb9c:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
3.安装es+k8s
# es 暴露的端口很多
# es 十分的耗内存
# es 的数据一般需要放置到安全目录! 挂载
# --net somenetwork 网络配置
# 启动elasticsearch
[root@WRF /]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
Unable to find image 'elasticsearch:7.6.2' locally
7.6.2: Pulling from library/elasticsearch
ab5ef0e58194: Pull complete
c4d1ca5c8a25: Pull complete
941a3cc8e7b8: Pull complete
43ec483d9618: Pull complete
c486fd200684: Pull complete
1b960df074b2: Pull complete
1719d48d6823: Pull complete
Digest: sha256:1b09dbd93085a1e7bca34830e77d2981521a7210e11f11eda997add1c12711fa
Status: Downloaded newer image for elasticsearch:7.6.2
5e198067d123de1346bf1a4c0b4ffc35577997c5e2282f5279e3b8174d9ba10c
# 启动了linux就卡主了,docker stats 查看cpu状态
# 测试一下es成功了
[root@WRF /]# curl localhost:9200
{
"name" : "5e198067d123",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "K6IKQPSBSnO48BQPqbEIMQ",
"version" : {
"number" : "7.6.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
"build_date" : "2020-03-26T06:34:37.794943Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
# 增加内存限制,修改配置文件 -e 环境配置修改
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
六、commit镜像
docker commit 提交容器成为一个新的版本
# 命令和git 原理类似
docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名:[TAG]
[root@WRF /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64b215075f20 tomcat:9.0 "catalina.sh run" About a minute ago Up About a minute 0.0.0.0:3355->8080/tcp, :::3355->8080/tcp tomcat01
[root@WRF /]# docker commit -a="WRF" -m="tomcat webapp add conf" 64b215075f20 tomcat001:1.0
sha256:9ef092fa07f7003823ea696047ae12783b9f7fcbb56b49b9907e88ef74abd137
[root@WRF /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat001 1.0 9ef092fa07f7 10 seconds ago 685MB
hello-world latest d2c94e258dcb 9 months ago 13.3kB
nginx latest 605c77e624dd 2 years ago 141MB
tomcat 9.0 b8e65a4d736d 2 years ago 680MB
mysql 5.7 c20987f18b13 2 years ago 448MB
centos latest 5d0da3dc9764 2 years ago 231MB
elasticsearch 7.6.2 f29a1ee41030 3 years ago 791MB