跳到主要内容

Docker常用命令整理

· 阅读需 6 分钟
故事的程序猿👨🏻‍💻
一个后端打酱油程序猿

Docker常用镜像和容器的命令,包括:镜像增删改查导入导出、容器增删改查、网络配置。

镜像命令

[root@lch ~]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE
app v1.0 d45bee24a299 About an hour ago 839MB
registry 2 773dbf02e42e 5 days ago 24.1MB
192.168.1.240:5000/hello-world v1.0 feb5d9fea6a5 8 months ago 13.3kB

# 所有镜像,包括中间镜像
[root@lch ~]# docker images -a

# 只显示镜像 ID
[root@lch ~]# docker images -q

# 显示镜像的摘要信息
[root@lch ~]# docker images --digests

# 显示镜像的完整信息
[root@lch ~]# docker images --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
app v1.0 sha256:d45bee24a2994afb9032315c5f006dd6a3ddce304a3306726395ea274e7ba044 About an hour ago 839MB

搜索相关

# 查询镜像 
[root@lch ~]# docker search hello-world
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
hello-world Hello World! (an example of minimal Dockeriz… 1754 [OK]
kitematic/hello-world-nginx A light-weight nginx container that demonstr… 151

下载镜像

docker pull xxx:tag  不指定 tag就下载最新的。

[root@lch ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:80f31da1ac7b312ba29d65080fddf797dd76acfb870e677f390d5acba9741b17
Status: Image is up to date for hello-world:latest
docker.io/library/hello-world:latest

删除镜像

# docker rmi XXX (镜像名字或者唯一镜像ID)
[root@lch ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:80f31da1ac7b312ba29d65080fddf797dd76acfb870e677f390d5acba9741b17

# 强制删除(如果镜像正在运行)
[root@lch ~]# docker rmi -f XXX

# 删除多个
[root@lch ~]# docker rmi -f hello-world hello

# 删除全部镜像
[root@lch ~]# docker rmi -f $(docker images -qa)

导出导入镜像

# 导出
$ docker save -o calibre.tar lscr.io/linuxserver/calibre-web:0.6.21

# 导入
$ docker load < calibre.tar

容器命令

查看容器

# 查看运行的容器
[root@lch ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a4040d2b800 joxit/docker-registry-ui:static "/docker-entrypoint.…" 15 hours ago Up 15 hours 0.0.0.0:8280->80/tcp, :::8280->80/tcp registry-ui
487bc206b516 registry:2 "/entrypoint.sh /etc…" 15 hours ago Up 15 hours 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp registry2

# 查看所有的容器
[root@lch ~]# docker ps -a

# 查看最近运行的容器
[root@lch ~]# docker ps -a -l

启动容器

# 以ubuntu 镜像启动一个容器,参数为以命令行模式进入该容器:
[root@lch ~]# docker pull ubuntu
[root@lch ~]# docker run -it ubuntu /bin/bash
root@c8dfa687c0cd:/#

# 启动容器并且指定一个名字
[root@lch ~]# docker run -it --name u2 ubuntu /bin/bash
root@fb3261067505:/#
  • -i: 交互式操作。
  • -t: 终端。
  • ubuntu: ubuntu 镜像。
  • /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。
  • exit: 退出容器

启动已停止的容器

# 查询出容器的列表
[root@lch ~]# docker ps -a
# 启动停止的容器
[root@lch ~]# docker start c8dfa687c0cd

停止一个运行的容器

[root@lch ~]# docker stop c8dfa687c0cd 
c8dfa687c0cd

重启一个容器

[root@lch ~]# docker restart c8dfa687c0cd

后台运行一个容器

[root@lch ~]# docker run -itd --name ubuntu ubuntu /bin/bash
5f9d7d938e833ec82acc4c0cb832412b93007411b0933c900d322312c70aa4d5

进入一个后台运行中的容器

  • docker attach
  • docker exec:推荐使用 docker exec 命令,此命令退出容器终端,不会导致容器停止。

attach

# 退出后,容器停止
[root@lch ~]# docker attach 5f9d7d938e83
root@5f9d7d938e83:/# exit
exit

[root@lch ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5f9d7d938e83 ubuntu "/bin/bash" 3 minutes ago Exited (0) 20 seconds ago

exec

# 后台启动容器
[root@lch ~]# docker run -itd ubuntu /bin/bash
fddc7d0333a54eca6adce3495c97d1468a65210aaeafaf2efda66911a93d28c3
[root@lch ~]# docker ps -a -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fddc7d0333a5 ubuntu "/bin/bash" 24 seconds ago Up 22 seconds hopeful_volhard
# 进入容器
[root@lch ~]# docker exec -it fddc7d0333a5 /bin/bash
root@fddc7d0333a5:/# exit
exit
# 退出容器后,容器依然在运行
[root@lch ~]# docker ps -a -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fddc7d0333a5 ubuntu "/bin/bash" 49 seconds ago Up 48 seconds hopeful_volhard

运行容器,映射端口

  • -P: 随机端口映射,容器内部端口随机映射到主机的端口
  • -p: 指定端口映射,格式为:主机(宿主)端口:容器端口
[root@lch ~]# docker run -itd --name mynginx -p 8080:80 nginx
349b6b4e2ed53ced7771492f8634f94fa717f829689eeeddb20bc0b9b41dacd6

使用大 P,随机端口映射。

[root@lch ~]# docker run -itd --name mynginx -P nginx
d4bb332de7e4384d4568bc1692261bf11bdefab9f1f3c40c85bd9bbabb7481fb

[root@lch ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4bb332de7e4 nginx "/docker-entrypoint.…" 21 seconds ago Up 20 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp mynginx

运行容器,目录映射

[root@lch ~]# docker run -itd --name mynginx -p 8080:80 --privileged=true -v /root/html:/usr/share/nginx/html nginx

--privileged=true 关闭安全权限,否则你容器操作文件夹可能没有权限。

然后访问 ip:8080就会看到 nginx 的欢迎页。这个时候我们在宿主机中html目录下新增 index.html 内容为 hello world 。替换默认的欢迎页。

[root@lch html]# echo "hello world" > index.html

然后刷新网页,就会发现欢迎页变成了 hello world

容器自动重启

--restart=always:我们在重启docker时,自动启动相关容器 --restart=on-failure:10 : 表示最多重启10次

[root@lch ~]# docker run -itd --name myubuntu --restart=always ubuntu

unless-stopped,在容器退出时总是重启容器,但是不考虑在 Docker 守护进程启动时就已经停止了的容器。

删除容器

# docker rm 容器ID(或者名字)  只能删除未运行的容器
[root@lch ~]# docker rm fddc7d0333a5
Error response from daemon: You cannot remove a running container fddc7d0333a54eca6adce3495c97d1468a65210aaeafaf2efda66911a93d28c3. Stop the container before attempting removal or force remove

# docker rm -f 容器ID(或者名字) 强制删除
[root@lch ~]# docker rm -f fddc7d0333a5
fddc7d0333a5

# 删除多个容器
[root@lch ~]# docker rm -f 5f5965c92591 60f0738a8325
5f5965c92591
60f0738a8325

# 批量删除所有的容器
[root@lch ~]# docker rm -f $(docker ps -aq)

# 批量删除状态为 exited 容器
[root@lch ~]# docker rm $(sudo docker ps -qf status=exited)

# 例如删除包含"some"的镜像
[root@lch ~]# docker rmi -f $(docker images | grep some | awk '{print $3}')

查看容器日志

查看最近的 n 条日志 docker logs --tail=n 容器ID或者容器名称

# 查看最近的 1000 条日志
[root@lch digitalTwins]# docker logs --tail=1000 model-server

查看实时日志 docker logs -f 容器ID/容器名称

[root@lch digitalTwins]# docker logs -f model-server

网络配置

网络模式:

  • bridge
  • host
  • none
  • container:[容器名或容器ID]
  • 自定义网络

docker 默认的网络配置为 :–-network=bridge 即桥接网络,以桥接模式连接到宿主机,创建一个独立网络,每个容器的网络都是独立的,不互通的,和宿主机也是隔离的。比如你是集群环境,启动容器后注册到 Nacos 中的地址是 docker 都私网地址:比如可能是如下所示的样子:

IP端口临时实例权重健康状态元数据操作
172.17.0.28083true1truepreserved.register.source=SPRING_CLOUD编辑 下线
......

容器之间可以通过 --network 设置相同名称网络,来实现互通, 如:--network mynet。这样容器之间互通了,但是想和宿主机及宿主机之外的其他组件进行通信又成了问题。

--network host来为容器配置网络,配置 host 表示使用宿主机的网络。这个时候如果你的服务注册在Nacos上的话服务地址就是宿主机的Ip地址了。但是这样也同样带来了一个弊端,容器不存在自己的ip, 端口映射不生效:这也就意味着,你不能再同一个宿主机上进行伪集群了。

-p,–publish,-P,和–publish-all都将被忽略,并产生一个警告
WARNING: Published ports are discarded when using host network mode
文章标题:Docker常用命令整理
版权声明:内容遵守
许可协议。转载请注明出处!
侵权提示:部分信息可能来源于网络。如发现有侵权,请随时联系删除!

相关推荐

Nacos2.3.2开启服务端用户认证的坑

Nacos2.3.2开启服务端用户认证的坑

最近升级Nacos服务端到版本2.3.2。但是在开启服务端用户认证的时候出现了问题。

Docusaurus 静态博客系列(1)- 系统搭建

Docusaurus 静态博客系列(1)- 系统搭建

这里介绍使用 Docusaurus ,它是一个用于构建开源项目网站的静态网站生成器。它由 Facebook 开发,旨在简化创建、维护和发布技术文档和项目文档的过程。

使用rss-parser解析rss订阅「Docusaurus」

使用rss-parser解析rss订阅「Docusaurus」

多库龙🐲使用rss-parser解析订阅feed,对于一个后端打酱油来说,写前端代码还是有点费劲的。

Centos7 使用 Certbot 申请 Let`s Encrypt 免费SSL证书

Centos7 使用 Certbot 申请 Let`s Encrypt 免费SSL证书

腾讯云:关于免费 SSL 证书策略调整通知,免费 SSL 证书有效期由12个月缩短至3个月。


神评论