Docker 命令总结

docker search <关键词>
docker search -f is-official=true redis
docker search --limit 2 redis
docker search --no-truc

docker images
docker images ls

docker pull --help
docker pull -h
docker pull centos
docker pull ubuntu:18.04

docker images
docker images ubuntu:latest
docker images cen*

docker rmi 8cef1fa centos:6
docker rmi ubuntu  #默认删除 latest 版本
docker images rm ubuntu:18:04
docker rmi -f centos:lastest

docker save centos:7 > centos.tar
docker save centos ubuntu:14:04 centos:6 8ec52 -o docker-images.tar # 可以保存多个

docker load -i centos.tar

docker tag centos:latest centos:7.4
docker tag 452e95 ubuntu:lastest

docker image inspect -f "{{json .GraphDriver.Data}}" centos
docker images inspect -h

docker inspect -h # 可以查看镜像或容器

docker history centos

docker run --rm -dti centos
docker ps

ls /var/lib/docker/containers

docker create --help
docker create centos ls -a
docker create centos
docker create --name centos-test centos
docker ps -a
docker ps

docker create -it centos python

docker rm -h
docker rm centos-test b513 a746
docker ps -a

docker create --name test-centos centos ls -a
docker start test-centos
docker start --attach test-centos  # 或 -a

docker create -it --name python-centos python
docker start -a python-centos

docker start --help
docker start -ai python-centos

docker run --help
docker run centos ls  -a
docker ps -a
# docker run = docker create + docker start -a 前台模式
# docker run -a = docker create + docker start 后台模式
docker run -it centos python
docker run -d -it centos python

docker run -d -it centos python
docker ps
docker stop -h
docker stop -t 2 8515  # 默认 10 秒
docker ps

docker kill -h
docker kill 8515
docker kill -s 'SIGTERM'

docker pause -h
docker pause 8515
docker ps -a
# Paused
docker unpause 8515

docker restart -h
# docker restart = docker stop + docker start

docker run --rm -dti centos bash
docker ps -a


docker container inspect
docker inspect a35b
docker inspect -f "{{json .NetworkSettings.Networks.bridge}}"

docker logs -h
docker run -dti centos python
docker logs 69cb

docker rename -h
docker rename 68cb test
docker ps -a

docker attach -h
docker start 5332
docker attach 5332 # 绑定到容器的主进程(1 号进程)
ls
exit

docker run -dti centos python
docker ps -a
docker attach 2b34
print("hello python")
quit()
docker ps -a  # 容器也退出了

docker exec -h
docker run -dti centos python
docker ps -a
docker exec 9039 ps -A # 进到了容器的子进程
docker exec 9039 ls
docker exec -it 9039 python
quit()
docker ps -a # 容器还在运行


docker run -dti centos bash
docker exec 1b15 yum install -y net-tools
docker exec 1b15 ifconfig
docker ps -a
docker commit -h
docker commit -m 'install net-tools' 1b15 centos-net:v1.0
docker run -dti 001a bash
docker exec f92b ifconfig


docker run -dti centos bash
docker export -h
docker exec 35c8 yum -y install net-tools
docker exec 35c8 ifconfig
docker export -o centos-net.tar 35c8

docker import -h
docker import -m '(import)install net-tools' centos-net.tar centos-net2:v1.0


docker history centos-net:v1.0
docker history centos
docker history centos-net2:v1.0

docker inspect centos
docker inspect centos-net:v1.0
docker inspect centos-net2:v1.0


docker run -dti centos-net:v1.0 bash
docker commit -m 'nothing to do' 48ca centos-net:v2.0
docker history centos-net:v2.0
docker inspect centos-net:v2.0

docker run -dti centos-net:v1.0 touch a.txt
docker commit -m 'nothing to do' bd58 centos-net:v3.0
docker history centos-net:v3.0
docker inspect centos-net:v3.0

docker images




# Docker 中有五种网络驱动模式
# bridge network:默认的网络模式,类似虚拟机的 nat 模式
# host network:容器与宿主机之间的网络无隔离,即容器直接使用宿主机网络
# None network:容器禁用所有网络
# Overlay network:利用 VXLAN 实现的 bridge 模式
# Macvlan network:容器具备 Mac 地址,使其显示为网络上的物理设备


docker network ls
# swarm

docker network ls -h

docker network create -h
docker network create --driver bridge my-bridge

docker network create -d host my-host
# 报错,host 只能存在一个
docker network create -d null my-null
# 报错,null 只能存在一个
docker network create -d overlay my-overlay
# 报错,需要依赖 swarm
docker network create -d macvlan my-macvlan
docker network ls


docker network rm -h
docker network rm my-macvlan my-bridge 38b3b

docker network inspect -h
docker inspect -f "{{json .IPAM.Config}}" bridge 

docker run --network my-bridge -dti --name centos-bridge centos bash
docker run -dti centos bash # 默认使用 bridge
docker run --network host -dti centos bash
docker run --network none -dti --name centos-none centos bash

docker exec centos-none ping baidu.com
#报错 ping 不通
docker exec centos-bridge ping baidu.com
# 可以 ping 通


docker run -dti centos
docker inspect 162b78
docker network disconnect bridge 162b78
docker inspect 162b78
docker network connect bridge 162b78
docker network disconnect bridge 162b78

docker network connect host 162b78
# 出错
docker network connect none 162b78
# 出错
docker network connect my-bridge 162b78




# bridge
ifconfig
docker inspect bridge
docker network create my-bridge
docker inspect my-bridge # 172.18.0.1
ifconfig # 多了一个网上 # 172.18.0.1

docker run -dti --network my-bridge centos
ifconfig # 多了一个
docker run -dti --network my-bridge centos
ifconfig # 又多了一个
docker exec acd0 yum install -y net-tools && ifconfig # 172.18.0.3


docker inspect bridge # Subnet: 172.17.0.0/16 Gateway: 172.17.0.1

docker network create --subnet 192.168.0.0/16 --ip-range 192.168.8.0/24 --gateway 192.168.8.254 custom-bridge
docker inspect custom-bridge
docker run -dti --network custom-bridge centos
docker inspect 8343ba # Gateway, Ip


docker pull redis
docker run -dti -P redis
docker ps -a # 32768->6379 随机映射
docker run -dti -P centos

docker run -dti -p 127.0.0.1:6379:6379 centos
docker run -dti -p :8000:6379 centos
docker run -dti 10.211.55.7:8001:6379 centos



# host 网络模式
docker run --network host -dti centos
docker run --network host -dti redis



# 特殊 host 网络模式(Container 网络模式)
docker run -dti --network container:4565c centos
docker ps -a
docker run -dti --nework container:redis-container redis
docker ps -a # 迅速退出了(6379 已经被使用了)


# none 网络模式

# overlay 网络模式


docker run -dti -v /root/volume_dir:/root/c_dir centos
ls # volume_dir
ls volume_dir # 空
docker exec 8bb8 touch /root/c_dir/test.log
ls volume_dir # test.log


# src 对应的路径或文件需要先创建(发现存在)
# src, dst 需要是绝对路径
docker run -dti --mount type=bind,src=/root/mount_dir,dst=/root/c2_dir centos
# 报错
mkdir mount_dir
docker run -dti --mount type=bind,src=/root/mount_dir,dst=/root/c2_dir centos
docker exec d5c4 touch /root/c2_dir/test2.log
ls mount_dir # test2.log



docker run -dti -v volume-test:/root/c_dir centos
docker volume ls #volume-test 自动创建数据卷
docker run -dti -v volume-test2:/root/c_dir centos
docker volume ls #volume-test2

docker run -dti --mount type=volume,src=volume-test,dst=/root/c_dir centos
docker run -dti --mount type=volume,src=volume-test3,dst=/root/c_dir centos
docker volume ls #volume-test3 自动创建

docker volume create -h
docker volume create volume-test4
docker volume inspect -h
docker inspect volume-test
ls /var/lib/docker/volumes/volume-test/_data

docker volume prune #删除未被使用的数据卷对象
docker volume rm volume-test3


docker run -dti --mount=tmpfs,dst=/root/c_dir centos



#共享数据卷
docker run -dti --mount type=volume,src=volume-test,dst=/root/c_dir centos
# ffb4
docker inspect volume-test
cd /var/lib/docker/volumes/volume-test/_data
ls # 没有东西
docker run -dti --volumes-from ffb4 centos
# 502ff
docker exec 502f touch /root/c_dir/test.txt
ls
# test.txt



docker run -dti -v /root/c_dir centos
docker volume ls
# 27d0adsfdf 随机的数据卷名
docker run -dti -v /root/c_dir centos
# d93ewrd



# 如果宿主机数据卷为空目录,则容器中的目录数据会复制到卷中
# 如果宿主机数据卷为非空,则容器中的目录数据会被隐藏,数据卷中的数据会复制到容器当中的目录

docker run -dti centos
# b2d0
docker exec b2d0 ls /run
# console ...
docker run -dti --mount type=volume,dst=/run centos
# 6776
docker volume ls
# 76b5
docker inspect 76b5
ls /var/lib/docker/volumes/76b5/_data
# console ...
docker exec 6776 ls /run
# console ...


docker run -dti --mount type=volume,src=765b,dst=/root/test_dir centos
# 626f
docker exec 626f ls /root/test_dir
# console ...

docker run -dti --mount type=volume,src=765b,dst=/root centos
# d44d
docker exec d44d ls /root
# console ...(原先容器中的 /root 目录下内容被隐藏了)



### 搭建无认证的私有仓库
docker pull registry
docker run -dti --restart always\
  --name my-registry\
  -p 8000:5000
  -v /my-registry/registry:/var/lib/registry\
  registry
docker ps -a
curl 127.0.0.1/v2/_catalog
# {"resposiories":[]}

docker tag <image> <IP:PORT>/<image_name>
docker push <IP:PORT>/centos
# 如果出现类似 https 的错误,那么需要在客户端中,往配置文件 /etc/docker/daemon.json 添加 "insecure-registries": ["服务器 IP:端口"],然后重启 docker


docker tag centos 47.94.153.230:8000/centos-latest
docker push 47.94.53.230:8000/centos-latest
curl 47.94.53.230:8000/v2/_catalog
# {"respositories:["centos-lastest"]}

docker rm 47.94.53.230:8000/centos-latest
docker images
docker pull 47.94.53.230:8000/centos-latest
docker images
  



### 搭建带认证的私有仓库
docker rm -f my-registry # 删除先前无认证的仓库容器
# rm -rf my-registry 删除之前的数据镜像
mkdir /my-registry/auth -p
docker run --entrypoint htpasswd registry -Bbn <username> <password> >/my-registry/auth/htpasswd
docker run -d -p 8000:5000 --restart=always --name docker-registry \
  -v /my-registry/registry:/var/lib/registry \
  -v /my-registry/auth:/auth \
  -e "REGISTRY_AUTH=htpasswd" \
  -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
  -e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" \
  registry

# 浏览器访问 47.94.153.230:8000,需要输入用户名及密码

docker login -u <username> -p <password> 47.94.153.230:8000
# 然后 pull 或 push
docker logout 47.94.153.230:8000


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈挨踢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值