掌柜
docker常用操作
03/16
本文最后更新于2023年09月06日,已超过454天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
常用命令
镜像
# 拉取镜像
[root@test:~]$ docker pull nginx:alpine
alpine: Pulling from library/nginx
Digest: sha256:eb05700fe7baa6890b74278e39b66b2ed1326831f9ec3ed4bdc6361a4ac2f333
Status: Image is up to date for nginx:alpine
docker.io/library/nginx:alpine
# 查看所有镜像
[root@test:~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-nginx ubuntu 5ffe8602f0cc 3 hours ago 165MB
nginx alpine cc44224bfe20 4 weeks ago 23.5MB
harbor.opstea.com/nginx alpine cc44224bfe20 4 weeks ago 23.5MB
ubuntu latest ba6acccedd29 3 months ago 72.8MB
# 通过 IMAGE ID 确定镜像唯一
# 导出镜像到文件中
[root@test:~]$ docker save -o nginx-alpine.tar nginx:alpine
# 从文件中加载镜像
[root@test:~]$ docker load -i nginx-alpine.tar
Loaded image: nginx:alpine
[root@test:~]$
# 删除镜像
[root@test:~]$ docker rmi nginx:alpine
Untagged: nginx:alpine
Untagged: nginx@sha256:eb05700fe7baa6890b74278e39b66b2ed1326831f9ec3ed4bdc6361a4ac2f333
[root@test:~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-nginx ubuntu 5ffe8602f0cc 3 hours ago 165MB
172.16.3.132:5000/nginx alpine cc44224bfe20 4 weeks ago 23.5MB
harbor.opstea.com/nginx alpine cc44224bfe20 4 weeks ago 23.5MB
localhost:5000/nginx alpine cc44224bfe20 4 weeks ago 23.5MB
registry 2 b8604a3fe854 2 months ago 26.2MB
ubuntu latest ba6acccedd29 3 months ago 72.8MB
[root@test:~]$
仓库
# 部署仓库。使用docker镜像启动镜像仓库服务
[root@test:~]$ docker run -d -p 5000:5000 --restart always --name registry registry:2
Unable to find image 'registry:2' locally
# 默认仓库不带认证,若需要认证,参考https://docs.docker.com/registry/deploying/#restricting-access
# 推送本地镜像到仓库
[root@test:~]$ docker tag nginx:alpine localhost:5000/nginx:alpine
[root@test:~]$ docker push localhost:5000/nginx:alpine
The push refers to repository [localhost:5000/nginx]
419df8b60032: Pushed
0e835d02c1b5: Pushed
5ee3266a70bd: Pushed
3f87f0a06073: Pushed
1c9c1e42aafa: Pushed
8d3ac3489996: Pushed
alpine: digest: sha256:544ba2bfe312bf2b13278495347bb9381ec342e630bcc8929af124f1291784bb size: 1568
# 查看仓库内元数据
[root@test:~]$ curl -X GET http://172.16.3.132:5000/v2/_catalog
{"repositories":["nginx"]}
[root@test:~]$ curl -X GET http://172.16.3.132:5000/v2/nginx/tags/list
{"name":"nginx","tags":["alpine"]}
# 镜像仓库给外部访问,不能通过localhost。尝试使用内网ip进行推送镜像:
[root@test:~]$ docker tag nginx:alpine 172.16.3.132:5000/nginx:alpine
[root@test:~]$ docker push 172.16.3.132:5000/nginx:alpine
The push refers to repository [172.16.3.132:5000/nginx]
Get https://172.16.3.132:5000/v2/: http: server gave HTTP response to HTTPS client
# docker默认不允许向http的仓库地址推送,如何做成https的,参考:https://docs.docker.com/registry/deploying/#run-an-externally-accessible-registry
# 我们没有可信证书机构颁发的证书和域名,自签名证书需要在每个节点中拷贝证书文件,比较麻烦,因此我们通过配置daemon的方式,来跳过证书的验证:
[root@test:~]$ vim /etc/docker/daemon.json
{
"registry-mirrors" : [
"https://docker.mirrors.ustc.edu.cn"
],
"insecure-registries": [
"172.16.3.132:5000"
]
}
[root@test:~]$ systemctl restart docker
[root@test:~]$ docker push 172.16.3.132:5000/nginx:alpine
The push refers to repository [172.16.3.132:5000/nginx]
......
alpine: digest: sha256:544ba2bfe312bf2b13278495347bb9381ec342e630bcc8929af124f1291784bb size: 1568
[root@test:~]$
[root@test:~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-nginx ubuntu 5ffe8602f0cc 3 hours ago 165MB
nginx alpine cc44224bfe20 4 weeks ago 23.5MB
harbor.opstea.com/nginx alpine cc44224bfe20 4 weeks ago 23.5MB
localhost:5000/nginx alpine cc44224bfe20 4 weeks ago 23.5MB
172.16.3.132:5000/nginx alpine cc44224bfe20 4 weeks ago 23.5MB
registry 2 b8604a3fe854 2 months ago 26.2MB
ubuntu latest ba6acccedd29 3 months ago 72.8MB
[root@test:~]$
# IMAGE ID相同,等于起别名或者加快捷方式
# 在其他机器上 pull 拉取镜像
[root@test2:~]$ docker pull 172.16.3.132:5000/nginx:alpine
alpine: Pulling from nginx
59bf1c3509f3: Pull complete
f3322597df46: Pull complete
d09cf91cabdc: Pull complete
3a97535ac2ef: Pull complete
919ade35f869: Pull complete
40e5d2fe5bcd: Pull complete
Digest: sha256:544ba2bfe312bf2b13278495347bb9381ec342e630bcc8929af124f1291784bb
Status: Downloaded newer image for 172.16.3.132:5000/nginx:alpine
172.16.3.132:5000/nginx:alpine
[root@test2:~]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
172.16.3.132:5000/nginx alpine cc44224bfe20 4 weeks ago 23.5MB
[root@test2:~]$
容器
# -d 后台运行
[root@test:~]$ docker run --name nginx -d nginx:alpine
# 映射端口,,把容器的端口映射到宿主机中,-p <host_port>:
[root@test:~]$ docker run --name my-nginx -d -p 8080:80 nginx:alpine
b4b8474b82df489e6bdaaa8d418e7d194f30dcf3dcd0d84903ce5edb478ac45c
[root@test:~]$ docker run --memory=500m nginx:alpine
# 容器数据持久化
[root@test:~]$ docker run --name nginx -d -v /opt:/opt nginx:alpine
61ac3a8172b29a84a61353ec7acf63f92a98f3f8233672ba4d4eb3af628ad8cb
[root@test:~]$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d -v /opt/mysql:/var/lib/mysql mysql:5.7
[root@test:~]$ ls /opt/mysql/
auto.cnf client-cert.pem ibdata1 ibtmp1 private_key.pem server-key.pem
ca-key.pem client-key.pem ib_logfile0 mysql public_key.pem sys
ca.pem ib_buffer_pool ib_logfile1 performance_schema server-cert.pem
[root@test:~]$
# 进入容器或者执行容器内命令
[root@test:~]$ docker exec -it mysql bash
root@dfc08c58bf32:/#
[root@test:~]$ docker exec mysql ls /var/lib/mysql/
auto.cnf
......
# 主机与容器之间拷贝数据
[root@test:~]$ echo '123' > /tmp/test.txt
[root@test:~]$ docker cp /tmp/test.txt nginx:/tmp
[root@test:~]$ docker exec -it nginx cat /tmp/test.txt
123
# 查看容器日志
[root@test:~]$ docker logs nginx # 查看全部日志
[root@test:~]$ docker logs -f nginx # 实时查看日志
[root@test:~]$ docker logs --tail=100 -f nginx # 从最新的 100 条日志开始,实时查看
# 查看容器详细信息
[root@test:~]$ docker inspect nginx
# 查看镜像详细信息
[root@test:~]$ docker inspect nginx:alpine