Docker常用命令

该文详细介绍了Docker中镜像的操作,包括搜索、拉取、查看、导出、删除、导入和查看详细信息。此外,还涵盖了容器的启动、查看、关闭、启动、日志查询、命令执行、删除以及数据卷的创建、挂载、删除等管理。内容还包括了如何查看容器内进程及资源占用情况。
摘要由CSDN通过智能技术生成

目录

一、镜像操作

1、搜索仓库镜像

2、拉取镜像

3、查看主机目前有哪些镜像

4、导出镜像

5、镜像删除

6、导入镜像

7、查看镜像详细信息

二、容器操作

1、启动容器

1.1创建容器再启动

1.2直接运行

2、容器查看

3、容器关闭

4、容器启动

5、查看容器详细信息

6、查询容器日志

7、对容器进行指定命令

8、删除容器

8.1先停止在删除

8.2直接删除

8.3删除所有停止状态的容器

9、容器中管理里数据

9.1创建一个数据卷

9.2查看数据卷详细信息

9.3数据卷挂载

9.4删除数据卷

10、查看容器内进程

#查看镜像、容器、数据卷等所占所占内存大小


一、镜像操作

docker如果无法自动补全需要安装bash-completion,接着如果source /usr/share/bash-completion/bash_completion一遍tab报错就再source一遍即可

1、搜索仓库镜像

docker search NAME

[root@docker-learn etc]# docker search nginx

2、拉取镜像

docker pull [选项] [docker resgistry 地址[:端口号]/]仓库名[:标签]

[root@docker-learn docker]# docker pull httpd
#打标签
[root@docker-learn bash-completion]# docker tag nginx:1.14-alpine nginx1:1.14
[root@docker-learn bash-completion]# docker images
REPOSITORY    TAG           IMAGE ID       CREATED         SIZE
httpd         latest        dabbfbe0c57b   14 months ago   144MB
hello-world   latest        feb5d9fea6a5   17 months ago   13.3kB
nginx1        1.14          8a2fb25a19f5   3 years ago     16MB
nginx         1.14-alpine   8a2fb25a19f5   3 years ago     16MB

#查看镜像层数(dokcer history NAME)

[root@docker-learn bash-completion]# docker history nginx:1.14-alpine 
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
8a2fb25a19f5   3 years ago   /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B        
<missing>      3 years ago   /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B        
<missing>      3 years ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
<missing>      3 years ago   /bin/sh -c #(nop) COPY file:ebf4f0eb33621cc0…   1.09kB    
<missing>      3 years ago   /bin/sh -c #(nop) COPY file:4c82b9f10b84c567…   643B      
<missing>      3 years ago   /bin/sh -c GPG_KEYS=B0F4253373F8F6F510D42178…   10.5MB    
<missing>      3 years ago   /bin/sh -c #(nop)  ENV NGINX_VERSION=1.14.2     0B        
<missing>      3 years ago   /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B        
<missing>      3 years ago   /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B        
<missing>      3 years ago   /bin/sh -c #(nop) ADD file:2e3a37883f56a4a27…   5.53MB  

3、查看主机目前有哪些镜像

docker images

[root@docker-learn docker]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
httpd         latest    dabbfbe0c57b   14 months ago   144MB
hello-world   latest    feb5d9fea6a5   17 months ago   13.3kB
[root@docker-learn ~]# docker image ls
#查看指定镜像
[root@docker-learn ~]# docker image ls nginx
REPOSITORY   TAG           IMAGE ID       CREATED       SIZE
nginx        1.14-alpine   8a2fb25a19f5   3 years ago   16MB

4、导出镜像

多个镜像导出可以使用-o

[root@docker-learn ~]# docker image save -o ./nginx_alpine.tar.gz nginx:1.14-alpine alpine:latest 
[root@docker-learn ~]# docker image save nginx > ./nginx_test.tar.gz
[root@docker-learn ~]# ls
anaconda-ks.cfg  nginx_alpine.tar.gz  nginx_test.tar.gz  test.sh

5、镜像删除

docker image rm或者docker rmi两种方式,如果镜像在使用无法删除的情况下加上 -f

[root@docker-learn ~]# docker image rm nginx1:1.14 
[root@docker-learn ~]# docker rmi  nginx:1.14-alpine
[root@docker-learn ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
httpd         latest    dabbfbe0c57b   14 months ago   144MB
alpine        latest    c059bfaa849c   15 months ago   5.59MB
hello-world   latest    feb5d9fea6a5   17 months ago   13.3kB
#根据CONTAINER ID删除正在运行的容器
[root@docker-learn ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED        STATUS                    PORTS     NAMES
8fe9811a40e8   hello-world   "/hello"   44 hours ago   Exited (0) 44 hours ago             upbeat_heisenberg
4ba48781b3d0   hello-world   "/hello"   2 days ago     Exited (0) 2 days ago               angry_aryabhata
c2bdae3438e5   hello-world   "/hello"   2 days ago     Exited (0) 2 days ago               crazy_wright
2a9636592b10   hello-world   "/hello"   2 days ago     Exited (0) 2 days ago               beautiful_pascal
14c82d950d23   hello-world   "/hello"   2 days ago     Exited (0) 2 days ago               gracious_keller
[root@docker-learn ~]# docker rm 8fe
8fe
[root@docker-learn ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED      STATUS                  PORTS     NAMES
4ba48781b3d0   hello-world   "/hello"   2 days ago   Exited (0) 2 days ago             angry_aryabhata
c2bdae3438e5   hello-world   "/hello"   2 days ago   Exited (0) 2 days ago             crazy_wright
2a9636592b10   hello-world   "/hello"   2 days ago   Exited (0) 2 days ago             beautiful_pascal
14c82d950d23   hello-world   "/hello"   2 days ago   Exited (0) 2 days ago             gracious_keller

6、导入镜像

docker image load -i 包名

[root@docker-learn ~]# docker image load -i nginx_test.tar.gz 
a464c54f93a9: Loading layer [==================================================>]  5.796MB/5.796MB
5ac9a5170bf2: Loading layer [==================================================>]  11.43MB/11.43MB
b2cbae4b8c15: Loading layer [==================================================>]  3.584kB/3.584kB
076c58d2644f: Loading layer [==================================================>]  4.608kB/4.608kB
Loaded image: nginx:1.14-alpine
[root@docker-learn ~]# docker images
REPOSITORY    TAG           IMAGE ID       CREATED         SIZE
httpd         latest        dabbfbe0c57b   14 months ago   144MB
alpine        latest        c059bfaa849c   15 months ago   5.59MB
hello-world   latest        feb5d9fea6a5   17 months ago   13.3kB
nginx         1.14-alpine   8a2fb25a19f5   3 years ago     16MB

7、查看镜像详细信息

docker image inspect NAME

[root@docker-learn ~]# docker image inspect httpd:latest 

二、容器操作

1、启动容器

1.1创建容器再启动

docker create 镜像名

docker start 容器名

[root@docker-learn ~]# docker create nginx

1.2直接运行

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

-t :打开⼀个终端,像使⽤交换机⼀样使⽤容器
-i:交互式访问
--name:容器名字
--network:指定⽹络
--rm:容器⼀停,⾃动删除
-d:后台运⾏容器,返回容器ID;否则会⼀直占据着终端
-p : 端⼝映射,将容器内服务的端⼝映射在宿主机的指定端⼝,格式为:主机(宿主)端⼝:容器端⼝
-P: 随机端⼝映射,容器内部端⼝随机映射到主机的端
[root@docker-learn ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS                      PORTS                                     NAMES
7928288e5946   nginx         "/docker-entrypoint.…"   11 seconds ago   Up 10 seconds               0.0.0.0:32769->80/tcp, :::32769->80/tcp   elastic_brown
9abf2c24421a   nginx         "/docker-entrypoint.…"   3 minutes ago    Exited (0) 2 minutes ago                                              stoic_ramanujan
71f2a3be8842   nginx         "/docker-entrypoint.…"   31 minutes ago   Created                                                               intelligent_jemison
14c82d950d23   hello-world   "/hello"                 2 days ago       Exited (0) 27 minutes ago                                             gracious_keller
#启动容器自动分配端口
[root@docker-learn ~]# docker run -P nginx
#根据container id进入容器
[root@docker-learn ~]# docker exec -it 792 bash
root@7928288e5946:/# 
#指定本地端口和设置容器名称
[root@docker-learn ~]# docker run --name web1 -d -p 8888:80 nginx
cc11a9acd5138308b14a849a0bc28bfc4c6980e94fdfe806ad23718367b6b32b
[root@docker-learn ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS                   PORTS                                   NAMES
cc11a9acd513   nginx         "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes             0.0.0.0:8888->80/tcp, :::8888->80/tcp   web1
14c82d950d23   hello-world   "/hello"                 3 days ago      Exited (0) 2 hours ago                                           gracious_keller
                                             gracious_keller
#根据container id/names进入容器                                        
[root@docker-learn ~]# docker exec -it cc1 bash
root@cc11a9acd513:/# exit
exit

2、容器查看

docker ps = docker container ls

[root@docker-learn ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
cc11a9acd513   nginx     "/docker-entrypoint.…"   20 minutes ago   Up 20 minutes   0.0.0.0:8888->80/tcp, :::8888->80/tcp   web1
#-a会输出已经结束使用的容器
[root@docker-learn ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS                      PORTS                                   NAMES
d4d3472fa94d   nginx         "/docker-entrypoint.…"   50 seconds ago   Exited (0) 48 seconds ago                                           xenodochial_noyce
cc11a9acd513   nginx         "/docker-entrypoint.…"   20 minutes ago   Up 20 minutes               0.0.0.0:8888->80/tcp, :::8888->80/tcp   web1
14c82d950d23   hello-world

#查看所有容器的container id:  docker ps -aq

3、容器关闭

docker stop id/names docker kill id/names

[root@docker-learn ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS                   PORTS                                   NAMES
acfd118c86e6   nginx         "/docker-entrypoint.…"   3 seconds ago    Up 3 seconds             80/tcp                                  eloquent_poincare
4852d00aa141   nginx         "/docker-entrypoint.…"   11 seconds ago   Up 10 seconds            80/tcp                                  bold_fermi
cc11a9acd513   nginx         "/docker-entrypoint.…"   28 minutes ago   Up 28 minutes            0.0.0.0:8888->80/tcp, :::8888->80/tcp   web1
14c82d950d23   hello-world   "/hello"                 3 days ago       Exited (0) 2 hours ago                                           gracious_keller
[root@docker-learn ~]# docker stop acf
 id/names 
[root@docker-learn ~]# docker kill 485
485
[root@docker-learn ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
cc11a9acd513   nginx     "/docker-entrypoint.…"   29 minutes ago   Up 29 minutes   0.0.0.0:8888->80/tcp, :::8888->80/tcp   web1

4、容器启动

docker start id/names

docker restart id/names

[root@docker-learn ~]# docker start acf
acf
[root@docker-learn ~]# docker restart bold_fermi
bold_fermi
[root@docker-learn ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
acfd118c86e6   nginx     "/docker-entrypoint.…"   6 minutes ago    Up 33 seconds   80/tcp                                  eloquent_poincare
4852d00aa141   nginx     "/docker-entrypoint.…"   6 minutes ago    Up 18 seconds   80/tcp                                  bold_fermi
cc11a9acd513   nginx     "/docker-entrypoint.…"   34 minutes ago   Up 34 minutes   0.0.0.0:8888->80/tcp, :::8888->80/tcp   web1

5、查看容器详细信息

docker inspect id/names

[root@docker-learn ~]# docker inspect 14c

6、查询容器日志

docker logs id/names

[root@docker-learn ~]# curl 192.168.157.10:8888
[root@docker-learn ~]# docker logs web1

7、对容器进行指定命令

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

-d, :后台运行

-e, :设置环境变量

-i, :交互式

-t, :打开一个终端

#查看容器所分配的ip
[root@docker-learn ~]# docker exec -it web1 hostname -I
172.17.0.2 

8、删除容器

8.1先停止在删除

docker kill /stop id/names

doker rm id/names

#可以使用 docker kill -s 9 CONTAINER ID进行停止进程

8.2直接删除

docker rm -f id/names

8.3删除所有停止状态的容器

docker container

prune Remove all stopped containers

[root@docker-learn ~]# docker container prune 
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
acfd118c86e673a8ada0a780f1622447798455a9dea753237437edab73083ab5
4852d00aa1416f48f3767d819930210505867e9ae1b369b301fc156a4abad81f
14c82d950d234862447e8a5b55aa35124a121d33be66d718d29f959a192fd1ac
​
Total reclaimed space: 2.188kB
[root@docker-learn ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS                                   NAMES
cc11a9acd513   nginx     "/docker-entrypoint.…"   3 hours ago   Up 3 hours   0.0.0.0:8888->80/tcp, :::8888->80/tcp   web1

9、容器中管理里数据

9.1创建一个数据卷

docker volume create NAME

[root@docker-learn ~]# docker volume create d_vol
d_vol

9.2查看数据卷详细信息

docker volume inspect NAME

[root@docker-learn ~]# docker volume inspect d_vol 
[
    {
        "CreatedAt": "2023-03-09T19:33:30+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/d_vol/_data",
        "Name": "d_vol",
        "Options": null,
        "Scope": "local"
    }
]

9.3数据卷挂载

创建容器nginx设置名称为web2,并将数据卷挂载到/usr/share/nginx/html

[root@docker-learn ~]# docker run -d -P --name web2 -v d_vol:/usr/share/nginx/html nginx:latest 
ca2de26cd649fa0157a5d99bdb19708c1776eea336570d523f454354f2ad3dae
[root@docker-learn ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                     NAMES
ca2de26cd649   nginx:latest   "/docker-entrypoint.…"   14 seconds ago   Up 12 seconds   0.0.0.0:32772->80/tcp, :::32772->80/tcp   web2
cc11a9acd513   nginx          "/docker-entrypoint.…"   4 hours ago      Up 4 hours      0.0.0.0:8888->80/tcp, :::8888->80/tcp     web1

#此时可以在虚拟机中修改容器的index.html也就是欢迎界面,同时无论是在容器中还是虚拟机中修改index.html文件都是相应会发生改变的,并且当容器删除之后数据卷依旧存在,包括如下例子中的_data/的数据会进行保留。

​​​​​​​[root@docker-learn d_vol]# ll _data/
total 8
-rw-r--r--. 1 root root 497 Dec 28  2021 50x.html
-rw-r--r--. 1 root root 615 Dec 28  2021 index.html
[root@docker-learn d_vol]# pwd
/var/lib/docker/volumes/d_vol

9.4删除数据卷

docker volume rm -f NAME

#必须在没有容器使用数据卷的情况下才可以删除,否则会失败。
[root@docker-learn d_vol]# docker volume rm -f d_vol 
#如果有容器的使用情况,先删除容器再删除数据卷
[root@docker-learn d_vol]# docker rm -f web2
web2
[root@docker-learn d_vol]# docker volume rm d_vol 
d_vol

#删除容器时同时删除数据卷:docker rm -v id/names
#删除所有不再使用的数据卷:docker volume prune

10、查看容器内进程

docker top id/names

[root@docker-learn volumes]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS                                   NAMES
cc11a9acd513   nginx     "/docker-entrypoint.…"   4 hours ago   Up 4 hours   0.0.0.0:8888->80/tcp, :::8888->80/tcp   web1
[root@docker-learn volumes]# docker top web1 
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                81922               81900               0                   16:02               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 81972               81922               0                   16:02               ?                   00:00:00            nginx: worker process
101                 81973               81922               0                   16:02               ?                   00:00:00            nginx: worker process

#查看镜像、容器、数据卷等所占所占内存大小

docker system df

[root@docker-learn d_vol]# docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          5         1         226.3MB   165.2MB (72%)
Containers      1         1         1.11kB    0B (0%)
Local Volumes   0         0         0B        0B
Build Cache     0         0         0B        0B

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值