查看docker安装的镜像:
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 15 months ago 448MB
查看docker所有的容器:
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cb2282893697 mysql:5.7 "docker-entrypoint.s…" 2 weeks ago Up 5 days 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
可以通过系统指令df -h查看内存分布:
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 2.7G 48G 6% /
/dev/sda1 1014M 151M 864M 15% /boot
/dev/mapper/centos-home 46G 33M 46G 1% /home
overlay 50G 2.7G 48G 6% /var/lib/docker/overlay2/76f68e80a852c9850abb6efc685da41bc323d
或者通过指令查找docker info 来查找root路径:
sudo docker info | grep "Docker Root Dir"
然后通过du指令查看 /var/lib/docker目录占用内存:
[root@localhost ~]# du -sh /var/lib/docker
876M /var/lib/docker
也可以通过docker 自带df查看内存详情:
[root@localhost ~]# docker system df -v
Images space usage:
REPOSITORY TAG IMAGE ID CREATED SIZE SHARED SIZE UNIQUE SIZE CONTAINERS
mysql 5.7 c20987f18b13 15 months ago 448.3MB 0B 448.3MB 1
Containers space usage:
CONTAINER ID IMAGE COMMAND LOCAL VOLUMES SIZE CREATED STATUS NAMES
cb2282893697 mysql:5.7 "docker-entrypoint.s…" 0 261B 2 weeks ago Up 5 days mysql
Local Volumes space usage:
VOLUME NAME LINKS SIZE
Build cache usage: 0B
CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED
通过docker logs 来查看当前镜像日志,可以快速定位问题:
[root@localhost ~]# docker logs -f --tail=300 mysql
2023-03-10T05:26:43.633523Z 0 [Note] Plugin 'FEDERATED' is disabled.
2023-03-10T05:26:43.636119Z 0 [Note] InnoDB: Buffer pool(s) load completed at 230310 5:26:43
2023-03-10T05:26:43.640394Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2023-03-10T05:26:43.640477Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
删除容器指令:
#删除一个容器
docker rm -f 容器名/容器ID
#删除多个容器 空格隔开要删除的容器名或容器ID
docker rm -f 容器名/容器ID 容器名/容器ID 容器名/容器ID
#删除全部容器
docker rm -f $(docker ps -aq)
删除镜像指令:
#删除一个
docker rmi -f 镜像名/镜像ID
#删除多个 其镜像ID或镜像用用空格隔开即可
docker rmi -f 镜像名/镜像ID 镜像名/镜像ID 镜像名/镜像ID
#删除全部镜像 -a 意思为显示全部, -q 意思为只显示ID
docker rmi -f $(docker images -aq)
删除无用的镜像:
# 删除异常停止的容器
docker rm `docker ps -a | grep Exited | awk '{print $1}'`
# 删除名称或标签为none的镜像
docker rmi -f `docker images | grep '' | awk '{print $3}'`
构建镜像:
sudo docker build -t jdk_tomcat:8 .
通过docker run 启动jdk_tomcat:8 当前镜像
sudo docker run -itd -p 8080:8080 --name jdk_tomcat jdk_tomcat:8
提交自己定义的镜像:
commit:从容器创建一个新的镜像。参数说明:
-a : 提交的镜像作者;
-c : 使用Dockerfile指令来创建镜像;
-m : 提交时的说明文字;
-p : 在commit时,将容器暂停。
将容器cb2282893697保存为新的镜像
# 并添加提交人信息和说明信息。
docker commit -a "guodong" -m "my db" cb2282893697 mysql:v1
通过docker login 登录容器存放的地址:
docker login --username=*** registry.cn-hangzhou.aliyuncs.com
通过标签和推送指令:
$ docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/lskdxhl/jdk_comcat:[镜像版本号]
$ docker push registry.cn-hangzhou.aliyuncs.com/lskdxhl/jdk_comcat:[镜像版本号]
拉取上传的镜像:
sudo docker pull registry.cn-hangzhou.aliyuncs.com/lskdxhl/jdk_comcat:8
可以通过docker指令docker exec来实现:
[root@localhost ~]# docker exec -it mysql /bin/bash
root@cb2282893697:/#
获取容器/镜像的元数据:
[root@localhost /]# docker inspect mysql
保存当前mysql镜像:
docker save mysql -o /my_images.tar
生成的位置在:
[root@localhost ~]# cd /
[root@localhost /]# ls
bin data etc lib media my_images.tar proc run shop sys usr
boot dev home lib64 mnt opt root sbin srv tmp var
加载当前保存的镜像:
docker load -i 镜像保存文件位置
查找docker 容器ID
[root@localhost /]# docker ps | grep mysql
cb2282893697 mysql:5.7 "docker-entrypoint.s…" 2 weeks ago Up 5 days 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
容器重命名:
docker rename 容器名称 新容器名称
搜索镜像:
[root@localhost /]# docker search mysql
[root@localhost /]# docker search --filter=STARS=13960 mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13960 [OK]