docker&镜像&容器--基本命令(二)

1.docker的 常用命令

包含docker的查看服务状态,启动,停止,开机自启动,重启。

1.1 查看docker服务的状态

systemctl status docker

docker处于启动的状态:Active: active (running) since 日 2023-09-24 18:11:50 PDT; 1 day 5h ago

docker处于停止的状态:Active: inactive (dead) since 一 2023-09-25 23:13:41 PDT; 4min 56s ago

1.2 启动docker服务

systemctl start docker(centos7.x的命令)
service docker start(centos6.x的命令)

1.3 停止docker服务

systemctl stop docker

1.4 重启docker服务

systemctl restart docker

1.5 禁止docker开机启动

 systemctl disable docker

1.6 设置docker开机自启动

systemctl enable docker

1.7 清理Docker环境的命令

docker system prune -a
该命令可以删除所有停止的容器、未被使用的网络、悬挂的镜像和所有不被使用的构建缓存。
-a 或者 --all 参数表示要删除所有未被使用的镜像,而不仅仅是悬挂的镜像。悬挂的镜像是指没有被任何标签引用的镜像。
请注意,这个命令会删除所有未被使用的数据,包括你可能还需要的容器和镜像,所以在运行这个命令之前,请确保你理解这个命令的效果,并且已经备份了任何需要保存的数据。

2.查看docker基本信息

2.1 查看docker版本信息

docker version 

2.2 显示 Docker 系统信息,包括镜像和容器数。

docker info

 2.3 查看指定容器的资源使用情况,包括 CPU、内存和网络

docker stats

2.4 查看容器详细信息,包括网络设置、端口映射等

docker inspect 容器id

3.镜像images

3.1 镜像的查看(docker images信息结果包括:镜像仓库、标签、镜像ID、创建时间、镜像大小

docker images

 3.2 镜像的导入(拉取)

docker pull : 从镜像仓库(Docker Hub)中拉取或者更新指定镜像

3.2.1 例如拉取tomcat镜像

[root@VM-4-13-centos awk]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
0e29546d541c: Pull complete 
9b829c73b52b: Pull complete 
cb5b7ae36172: Pull complete 
6494e4811622: Pull complete 
668f6fcc5fa5: Pull complete 
dc120c3e0290: Pull complete 
8f7c0eebb7b1: Pull complete 
77b694f83996: Pull complete 
0f611256ec3a: Pull complete 
4f25def12f23: Pull complete 
Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest

3.3 镜像的导出

[root@VM-4-13-centos awk]# docker save tomcat /home/tomcat.gz.tar
cowardly refusing to save to a terminal. Use the -o flag or redirect
[root@VM-4-13-centos awk]# docker save tomcat -o /home/tomcat.gz.tar
tomcta:镜像名
/home/tomcat.tar.gz:保存到的路径和起的文件名

3.4 镜像的删除

docker rmi container_id

镜像的删除(rmi后面可以跟多个id,用空格隔开)

3.4.1 实例-根据id删除镜像

[root@linux-node2 ~]# docker rmi 813e3731b203 

3.5 本地镜像的导入

[root@VM-4-13-centos home]# docker load < /home/tomcat.gz.tar
11936051f93b: Loading layer [==================================================>]  129.1MB/129.1MB
31892cc314cb: Loading layer [==================================================>]   11.3MB/11.3MB
8bf42db0de72: Loading layer [==================================================>]  19.31MB/19.31MB
26a504e63be4: Loading layer [==================================================>]  156.5MB/156.5MB
f9e18e59a565: Loading layer [==================================================>]  11.74MB/11.74MB
832e177bb500: Loading layer [==================================================>]  3.584kB/3.584kB
3bb5258f46d2: Loading layer [==================================================>]  343.2MB/343.2MB
59c516e5b6fa: Loading layer [==================================================>]  3.072kB/3.072kB
bd2befca2f7e: Loading layer [==================================================>]   20.8MB/20.8MB
3e2ed6847c7a: Loading layer [==================================================>]  2.048kB/2.048kB
Loaded image: tomcat:latest
[root@VM-4-13-centos home]# docker images
REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
nginx                    <none>    5d58c024174d   3 weeks ago     142MB
mongo                    latest    1cca5cf68239   5 weeks ago     695MB
portainer/portainer-ce   latest    500504ac663a   8 weeks ago     285MB
nginx                    latest    605c77e624dd   10 months ago   141MB
tomcat                   latest    fb5657adc892   10 months ago   680MB

3.6 镜像重命名

[root@VM-4-13-centos home]# docker images
REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
nginx                    <none>    5d58c024174d   3 weeks ago     142MB
mongo                    latest    1cca5cf68239   5 weeks ago     695MB
portainer/portainer-ce   latest    500504ac663a   8 weeks ago     285MB
nginx                    latest    605c77e624dd   10 months ago   141MB
tomcat                   latest    fb5657adc892   10 months ago   680MB
[root@VM-4-13-centos home]# docker tag fb5657adc892 mytomcat
[root@VM-4-13-centos home]# docker images
REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
nginx                    <none>    5d58c024174d   3 weeks ago     142MB
mongo                    latest    1cca5cf68239   5 weeks ago     695MB
portainer/portainer-ce   latest    500504ac663a   8 weeks ago     285MB
nginx                    latest    605c77e624dd   10 months ago   141MB
mytomcat                 latest    fb5657adc892   10 months ago   680MB
tomcat                   latest    fb5657adc892   10 months ago   680MB
[root@VM-4-13-centos home]# docker tag fb5657adc892 mytomcat:mytomcat
[root@VM-4-13-centos home]# docker images
REPOSITORY               TAG        IMAGE ID       CREATED         SIZE
nginx                    <none>     5d58c024174d   3 weeks ago     142MB
mongo                    latest     1cca5cf68239   5 weeks ago     695MB
portainer/portainer-ce   latest     500504ac663a   8 weeks ago     285MB
nginx                    latest     605c77e624dd   10 months ago   141MB
mytomcat                 latest     fb5657adc892   10 months ago   680MB
mytomcat                 mytomcat   fb5657adc892   10 months ago   680MB
tomcat                   latest     fb5657adc892   10 months ago   680MB

# docker tag IMAGEID(镜像id) REPOSITORY:TAG(仓库:标签)

 4.容器

4.1 容器的创建语法

docker run [option] 镜像名 [向启动容器中传入的命令]
-i 表示以“交互模式”运行容器
-t 表示容器启动后会进入其命令行。加入这两个参数后,容器创建就能登录进去。即 分配一个伪终端。
-name 为创建的容器命名
-v 表示目录映射关系(前者是宿主机目录,后者是映射到宿主机上的目录,即 宿主机目录:容器中目录),可以使 用多个-v 做多个目录或文件映射。注意:最好做目录映射,在宿主机上做修改,然后 共享到容器上。
-d 在run后面加上-d参数,则会创建一个守护式容器在后台运行(这样创建容器后不会自动登录容器,如果只加-i -t 两个参数,创建后就会自动进去容器)。
-p 表示端口映射,前者是宿主机端口,后者是容器内的映射端口。可以使用多个-p 做多个端口映射
-e 为容器设置环境变量
–network=host 表示将主机的网络环境映射到容器中,容器的网络与主机相同
--restart=always  参数能够使我们在重启docker时,自动启动相关容器。
   #Docker容器的重启策略如下:
   #no,默认策略,在容器退出时不重启容器
   #on-failure,在容器非正常退出时(退出状态非0),才会重启容器
   #on-failure:3,在容器非正常退出时重启容器,最多重启3次
   #always,在容器退出时总是重启容器
   #unless-stopped,在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器
--privileged=true 使用该参数,container内的root拥有真正的root权限。
  #不使用该参数,container内的root只是外部的一个普通用户权限。
  #大约在0.6版,privileged被引入docker
  #privileged启动的容器,可以看到很多host上的设备,并且可以执行mount。
  #甚至允许你在docker容器中启动docker容器。

4.1.1实例

docker run -d --name tomcat01 --restart unless-stopped -p 8080:8080 \
-v  /opt/logs:/usr/local/tomcat/logs  \
tomcat:latest

4.2 容器启动,停止,重启

停止容器:docker stop  容器id/容器名
启动容器:docker start  容器id/容器名
重启容器: docker restart  容器id/容器名

4.3 进入容器attachexec

4.3.1 docker attach:退出容器终端,会导致容器的停止

 

docker attach 容器ID/容器名

 

4.3.2 docker exec:推荐大家使用 docker exec 命令,因为此退出容器终端,不会导致容器的停止。

 

 docker exec -it b9ee3b70f26e容器ID) /bin/bash

 4.4 查看容器

docker ps:列出容器
常用参数:
-a:显示全部的容器,包括未运行的
-q:只显示容器的ID

输出详情介绍:

  • CONTAINER ID: 容器 ID。

  • IMAGE: 使用的镜像。

  • COMMAND: 启动容器时运行的命令。

  • CREATED: 容器的创建时间。

  • STATUS: 容器状态。

状态有7种:

  • created(已创建)

  • restarting(重启中)

  • running(运行中)

  • removing(迁移中)

  • paused(暂停)

  • exited(停止)

  • dead(死亡)

 

posted @ 2022-11-07 14:02  家乐福的搬砖日常  阅读(174)  评论(0编辑  收藏  举报