docker的常用命令

启动docker

sudo systemctl start docker

帮助命令

docker version   #显示docker的版本信息
docker info #显示docker的系统信息,包括镜像和容器信息的数量
docker 命令 --help #帮助命令

docker官方帮助文档

镜像命令

[root@localhost ~]# docker images#查看所以本地的主机上的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 21 months ago 13.3kB

#解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATD 镜像的创建时间
SIZE 镜像的大小

#可选项(可用--help看)
-a, --all 列出所有镜像
--digests Show digests
-q, --quiet 只显示ID
docker search 搜索镜像(dockerhub)
NAME                            DESCRIPTION
STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14234 [OK]
mariadb MariaDB Server is a high performing open sou… 5439 [OK]

#可选项(可用--help看)通过搜索来过滤
--filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
docker pull 下载镜像 (镜像名[:tag]默认最新)
[root@localhost ~]# docker pull mysql
Using default tag: latest #如果不写tag.默认就是latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete #分层下载,docker iamge的核心 联合文件系统
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

#指定版本下载
docker pull mysql:5.7
docker rml 删除镜像
docker rml -f 镜像id(单个)
docker rml -f 镜像id 镜像id..(多个)
docker rml -f $(docker images -aq)(全部)

容器命令

说明:我们有了镜像才可以创建容器
docker pull centos
新建容器并启动
docker run [可选参数] image

#参数说明
--name 名字 容器名字
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-P 指定容器的端口
-p ip:主机名
-P 主机端口:容器端口(常用)
-P 容器端口
容器名字
-p 随机指定窗口

#测试、启动并进入容器
[root@localhost ~]# docker run -it centos /bin/bash
[root@8fbd032a161a /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr

#从容器中退出主机
[root@8fbd032a161a ~]# exit
exit
[root@localhost ~]# ls
anaconda-ks.cfg
列出所以的运行的容器
#docker ps 命令
#列出当前正在运行的容器
-a #列出当前正在运行的容器,导出历史运行过的容器
-n=?#显示最近创建的容器
-q #显示容器的编号
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8fbd032a161a centos "/bin/bash" 4 minutes ago Exited (0) 2 minutes ago keen_colden
3166b588223a hello-world "/hello" About an hour ago Exited (0) About an hour ago quirky_robinson
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
退出容器
exit  #直接退出
ctrl +p +q #容器永不停止
删除容器
docker rm 容器id #删除指定容器,不能删正在运行的容器,要强制删除加 -f
docker rm -f $(docker ps -aq)#删除全部容器
docker ps -a -q|xaegs docker rm #删除全部容器
启动和停止容器的操作
docker start 容器id    #启动
docker restart 容器id #重启
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止

常用其他命令

后台启动容器
#命令 docker run -d 镜像名 后台启动
#问题docker ps,发现centos停止了
#常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了
查看日志
docker logs -tf --tail 容器
#显示日志
-tf #查看日志
--tail (number)#要显示日志条数
查看容器中进程信息
#命令 
docker top 容器id
查看镜像的元数据
#命令
docker inspect 容器id
进入当前正在运行的容器
#我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置

#命令
docker exec -it 容器id bashShell #进入容器后开启一个新的终端,可以在里面操作(常用)
docker attach 容器 #进入容器正在执行的终端,不会启动新的进程

从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径

部署nginx

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 17 months ago 141MB
mysql latest 3218b38490ce 18 months ago 516MB
hello-world latest feb5d9fea6a5 21 months ago 13.3kB
centos latest 5d0da3dc9764 21 months ago 231MB
[root@localhost ~]# docker run -d --name nginx01 -p 3344:80 nginx
88ed367e89cf5cf45dd2d025ebab0efcb90f455d6bc78d9bc0fd93b28ce56432
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
88ed367e89cf nginx "/docker-entrypoint.…" 10 seconds ago Up 8 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
[root@localhost ~]# 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>

可视化

portainer
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer