dockerd进程及配置
# 启、停、状态
systemctl start|stop|status docker
# 启动配置
cat /usr/lib/systemd/system/docker.service
# dockerd指定端口启动
sudo docker daemon -H tcp://0.0.0.0:2375 -H unix://home/docker/docker.sock
# docker指定端口查看
docker -H tcp://0.0.0.0:2375 ps
export DOCKER_HOST="tcp://0.0.0.0:2375"
docker ps
# 配置
cat /etc/docker/daemon.json
# 存储
ls /var/lib/docker
# container:存放容器信息
# graph:存放镜像信息
# aufs:存放具体的内容文件
docker信息
# 查看版本,镜像容器数量等信息
docker info
docker镜像
images、history、inspect、search、pull、push、build、tag、rmi、image prune、save、load、export、import
# 查看所有本地镜像
docker image ls
docker images
# 通过名字或tag查看
docker images java
docker images java:8
# 查看镜像构建各层信息
docker image history nginx:1.22.1
docker history nginx:1.22.1
# 查看镜像详细信息
# docker image inspect [OPTIONS] IMAGE [IMAGE...]
docker image inspect nginx
# 搜索镜像,从镜像仓库
# docker search [OPTIONS] TERM
docker search nginx
# 搜索镜像,至少3星,不截断显示
docker search --filter stars=3 --no-trunc busybox
# 搜索镜像,官方的,至少3星
docker search --filter is-official=true --filter stars=3 busybox
# 拉取镜像,从镜像仓库
# docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
docker image pull nginx:1.22.1
docker pull nginx
# 推送镜像到镜像仓库
# docker image push [OPTIONS] NAME[:TAG]
docker image push nginx
docker push nginx
# 编译镜像,根据当前目录下Dockerfile
docker image build .
docker build .
# 创建镜像通过运行的容器
docker commit 容器ID testimage:v1
# 给镜像打标签,别名
# docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
docker image tag nginx:1.22.1 nginx:v1.22.1
docker tag nginx:1.22.1 nginx:v1.22.1
# 删除镜像
# docker image rm [OPTIONS] IMAGE [IMAGE...]
docker image rm nginx
docker rmi nginx
# 移除没使用的镜像
# docker image prune [OPTIONS]
# 移除悬空镜像
# 悬空镜像<none>:<none>,由于新构建镜像占用了原有镜像标签
# 重复构建同一命名和标签产生悬空镜像
docker image prune
docker rmi $(docker images -f "dangling=true" -q)
# 移除超过10天的悬空镜像
docker image prune --force --filter "until=240h"
# 移除没有被容器使用的镜像
docker image prune -a
# 保存镜像
# save没有丢失镜像的历史,可以回滚到之前层,用于外网无法连接
# docker image save [OPTIONS] IMAGE [IMAGE...]
# 保存单个镜像
docker save busybox > busybox.tar
docker save -o busybox.tar busybox
docker save myimage:latest | gzip > myimage_latest.tar.gz
# 保存多个镜像
docker save -o ubuntu.tar ubuntu:lucid ubuntu:saucy
# 载入镜像
# docker image load [OPTIONS]
# 载入镜像
docker load < busybox.tar.gz
docker load -i busybox.tar
# 导出一个镜像
# export丢失容器的挂载信息,无法回滚,用来制作基础镜像
# docker export [OPTIONS] CONTAINER
docker export busybox > busybox.tar
docker export -o="busybox.tar" busybox
# 导入创建一个镜像
# docker image import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
# 导入创建一个镜像
docker import https://example.com/exampleimage.tgz
cat busybox.tar.gz | docker import - busybox:new
docker import - busybox < busybox.tar
docker import /exampleimage.tgz
docker容器
ps、inspect、logs、run、start、stop、stats、exec、attach、kill、rm、container prune、cp、top、pause、unpase
# 查看容器
docker ps
# 查看运行的容器,不截断
docker ps --no-trunc
# 查看所有容器,运行和退出
docker ps -a
docker ps --size
docker ps --filter "name=nginx"
docker ps -a --filter 'exited=137'
# 查看容器详细信息
docker inspect nginx
docker inspect --format='{{ .State.Running }}' nginx
# 查看容器日志
docker logs nginx -f
# 最后10行
docker logs -f --tail=10 nginx
# 查看持续2秒
docker logs -f --until=2s nginx
# 运行容器
# docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
# 临时运行,退出终止容器,可Ctrl-p挂起Ctrl-q恢复
docker run --name=busybox busybox
# 临时运行,交互式进入容器
docker run --name=busybox -it busybox bash
# 临时运行,退出时删除容器
docker run –rm -it busybox bash
# 长期运行,暴露80端口,curl http://localhost:80/
docker run -d --name nginx -p 80:80 nginx:latest
# 查看镜像环境变量
docker run IMAGE env
# 运行实践
docker run -d \
--name nginx \
-m 800m \
--net=host \
--restart=always \
--log-opt max-size=100m \
--log-opt max-file=10 \
-u root \
-v /etc/nginx/:/etc/nginx/ \
nginx:latest
# 启、停、看状态容器
docker start|stop|stats nginx
# 进入容器
# 打开交互式shell,开启一个新终端,exit退出容器不终止
docker exec -it nginx bash
docker exec -it nginx pwd
# 查看容器环境变量
docker exec nginx env
docker exec -e VAR_A=1 nginx env
# 查看容器内进程
docker exec -it nginx ps -elf
docker exec -it nginx ps aux
# 进入容器
# 容器正在执行终端,exit退出容器终止,按Ctrl+P+Q退出
docker attach nginx
# 杀掉容器
# 杀掉所有容器,默认信号SIGKILL
docker kill $(docker ps -q)
docker kill nginx
docker kill --signal=SIGHUP nginx
# 删除停止容器
docker rm nginx php
docker rm -f nginx
# 删除容器和挂载
docker rm -v redis
# 删除停止的容器,-q查看容器ID
docker rm $(docker ps --filter status=exited -q)
docker ps --filter status=exited -q | xargs docker rm
# 移除停止的容器
docker container prune
# 移除创建超过5分钟,停止的容器
docker container prune --force --filter "until=5m"
# 在容器与主机拷贝文件目录
docker cp /www contianerID:/www/
docker cp contianerID:/www /tmp/
# 查看容器内的进程
docker top nginx
# 挂起容器内的进程
docker pause nginx
# 恢复容器内进程
docker unpause nginx
docker网络
network ls、network create、network connect、network diconnect、network rm、network prune
# 查看网络
# 安装docker引擎后,默认创建三种网络bridge、host、none
docker network ls
docker network ls --filter driver=bridge
# 查看网络下信息,可看到容器名称
docker network inspect host
# 创建网络
docker network create -d bridge my-bridge-network
# 连接容器到网络
docker network connect my-bridge-network nginx
# 断开容器从网络
docker network disconnet my-bridge-network nginx
# 删除网络
docker network rm my-bridge-network
# 移除没有使用的网络
docker network prune
# 移除创建超过5分钟,没有使用的网络
docker network prune --force --filter until=5m
# 容器指定网络类型
docker run -d --name nginx --net=my-bridge-network nginx
docker run -d --name nginx --net=host nginx
docker run -d --name nginx --net=none nginx
docker run -d --name nginx --net=container:php nginx
# 容器在网桥模式下,暴露端口80,curl localhost:80
docker run -d --name=nginx --net=my-bridge-network -p 80:80 nginx
# 运行容器,暴露端口80
# 获取容器IP,172.17.0.19:80
docker inspect nginx | grep IPAddress
# iptable转发端口
iptables -t nat -A DOCKER -p -tcp --dport 80 -j DNAT --to-destination 172.17.0.19:80
docker挂载
volume ls、volume create、volume inspect、volume rm、volume prune
# 查看挂载卷
docker volume ls
# 创建挂载卷,默认在/var/lib/docker/volumes/下
docker volume create my-vol
# 查看挂载卷详细信息
docker volume inspect my-vol
# 删除挂载卷
docker volume rm my-vol
# 清理匿名没有容器使用的挂载卷
docker volume prune
# 使用挂载卷
docker run -d -v my-vol:/world busybox ls /world
# 挂载,默认在主机/var/lib/docker/volumes/下随机一个文件
docker run -d --name nginx -v /etc/nginx nginx
# 指定主机路径挂载 :ro只读 :rw读写
docker run -d --name nginx -v /nginx:/etc/nginx nginx
docker run -d --name nginx -v /nginx:/etc/nginx:ro nginx
docker run -d --name nginx -v /nginx:/etc/nginx:rw nginx