01Docker常用命令

1. docker 常用命令
#1.查看docker相关信息
[root@hs-10-20-18-175 docker_fc]# docker info
Containers: 12
 Running: 1
 Paused: 0
 Stopped: 11
Images: 13
Server Version: 1.12.6
#2.查看当前机器所有镜像
[root@hs-10-20-18-175 docker_fc]# docker images
REPOSITORY                                            TAG                 IMAGE ID            CREATED                  SIZE
192.168.44.132/uc20/uc-zgv20190204000                 uc20                5eba8fd7d56f        Less than a second ago   20.89 GB
192.168.44.132/o4uft/pf1.0v201801.01.000_20200515fb   o4uft               309dd2cc4018        Less than a second ago   25.06 GB
192.168.44.132/uc20/ucv20180104000                    uc20                b70848ddab55        7 months ago        21.58 GB
#3.删除镜像文件
[root@hs-10-20-18-175 docker_fc]# docker rmi b7
Untagged: 192.168.44.132/uc20/ucv20180104000:uc20
Untagged: 192.168.44.132/uc20/ucv20180104000@sha256:738c347b34e91fce1d21d6e9c88310e93423543b2fba6ddba39110c23528d938
Deleted: sha256:b70848ddab5566f18c64c15c435802b13366020d43e640277d7acb84beebb02d
Deleted: sha256:a29a209f71bab2550de85a847a698914f583cf37bbd8c6578be0fa48682690a3
Deleted: sha256:86618bf944d184a7758f9552ac66c2c4f719b528a92216d2b213b5abab13c513
#4.其他镜像操作命令
4.1-# docker pull REPOSITORY:TAG 下载镜像文件
4.2-# docker push REPOSITORY:TAG 上传镜像文件
4.3-# docker tag IMAGEID REPOSITORY:TAG 重命名镜像文件
4.4-# docker import pf20170331.tar 通过压缩包制作镜像
#5.查看容器信息
#5.1.查看运行中的容器
[root@hs-10-20-18-175 docker_fc]# docker ps
CONTAINER ID        IMAGE                                        COMMAND             CREATED             STATUS              PORTS                                                           NAMES
fa6b43edfb6b        192.168.44.132/uc20/uc-zgv20190204000:uc20   "bash"              11 years ago        Up 11 years         0.0.0.0:1521->1521/tcp, 0.0.0.0:5123->5123/tcp, 0.0.0.0:5127->5127/tcp, 0.0.0.0:5224->5224/tcp, 0.0.0.0:9003->9003/tcp, 0.0.0.0:9009->9009/tcp, 0.0.0.0:9898->9898/tcp, 0.0.0.0:15827->15827/tcp, 0.0.0.0:15841->15841/tcp, 0.0.0.0:15844-15845->15844-15845/tcp, 0.0.0.0:15858-15859->15858-15859/tcp, 0.0.0.0:15861->15861/tcp, 0.0.0.0:15864->15864/tcp, 0.0.0.0:2024->22/tcp, 0.0.0.0:1524->1522/tcp   uc-zgv20190204000

#5.2.查看所有容器,包括没有运行的
[root@hs-10-20-18-175 docker_fc]# docker ps -a
CONTAINER ID        IMAGE                                        COMMAND             CREATED             STATUS              PORTS                                                           NAMES
c0ebd191999f        192.168.44.132/o4uft/pf1.0v201801.01.000_20200515fb:o4uft   "dumb-init bash"    11 years ago        Exited (137) 11 years ago                                             pf1.0v201801.01.000_20200515fb
fa6b43edfb6b        192.168.44.132/uc20/uc-zgv20190204000:uc20                  "bash"              11 years ago        Up 11 years                  0.0.0.0:1521->1521/tcp, 0.0.0.0:5123->5123/tcp, 0.0.0.0:5127->5127/tcp, 0.0.0.0:5224->5224/tcp, 0.0.0.0:9003->9003/tcp, 0.0.0.0:9009->9009/tcp, 0.0.0.0:9898->9898/tcp, 0.0.0.0:15827->15827/tcp, 0.0.0.0:15841->15841/tcp, 0.0.0.0:15844-15845->15844-15845/tcp, 0.0.0.0:15858-15859->15858-15859/tcp, 0.0.0.0:15861->15861/tcp, 0.0.0.0:15864->15864/tcp, 0.0.0.0:2024->22/tcp, 0.0.0.0:1524->1522/tcp   uc-zgv20190204000
cb822aa012a7        192.168.44.132/o4uft/pf1.0v201801.01.000_20191227fb:o4uft   "dumb-init bash"    11 years ago        Exited (137) 11 years ago                                        pf1.0v201801.01.000_20191227fb

#5.3.查看运行中的容器(只显示容器id)
[root@hs-10-20-18-175 docker_fc]# docker ps -q
fa6b43edfb6b

#5.4.查看所有容器,包括没有运行的(只显示容器id)
[root@hs-10-20-18-175 docker_fc]# docker ps -aq
fa6b43edfb6b
cb822aa012a7
#6.1.停止容器运行
#查看运行中的容器id
[root@hs-10-20-18-175 docker_fc]# docker ps -q
fa6b43edfb6b
#使用容器id停止运行中的容器
[root@hs-10-20-18-175 docker_fc]# docker stop fa
fa
#6.2.启动容器
#查看所有容器id
[root@hs-10-20-18-175 docker_fc]# docker ps -aq
fa6b43edfb6b
cb822aa012a7
#使用容器id启动运行中的容器
[root@hs-10-20-18-175 docker_fc]# docker start fa
fa
##查看运行中的容器id
[root@hs-10-20-18-175 docker_fc]# docker ps -q
fa6b43edfb6b
#7.删除容器(删除容器前需要先停止容器)
#查看所有容器id
[root@hs-10-20-18-175 docker_fc]# docker ps -aq
fa6b43edfb6b
cb822aa012a7
#根据容器id删除容器
[root@hs-10-20-18-175 docker_fc]# docker rm fa
fa
[root@hs-10-20-18-175 docker_fc]# docker ps -aq
cb822aa012a7
#删除全部容器
[root@hs-10-20-18-175 docker_fc]# docker rm $(docker ps -qa)
#8.容器操作
#8.1.进入容器内部
[root@hs-10-20-18-175 docker_fc]# docker exec -it f74fe392e9132042a51fc4c51896e8051fc59a3585e2576580e147cb4ad11b54  /bin/bash
[root@f74fe392e913 /]# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
#8.2.退出容器内部
[root@hs-10-20-18-175 docker_fc]# exit
#9.1.修改容器名称(可以用于一个镜像搭建两套容器)
[root@hs-10-20-18-175 docker_fc]# docker ps -a
CONTAINER ID        IMAGE                                                       COMMAND             CREATED             STATUS                       PORTS                                                                                                                NAMES
f74fe392e913        192.168.44.132/uc20/uc-zgv20190204000:uc20                  "bash"              27 hours ago        Exited (137) 6 minutes ago                                                                                                                       uc-zgv20190204000
b9eb76fdd09b        192.168.44.132/o4uft/of20170630c_20200312fb:o4uft           "dumb-init bash"    3 weeks ago         Exited (137) 11 years ago                                                                                                                              of20170630c_20200312fb

[root@hs-10-20-18-175 docker_fc]# docker rename uc-zgv20190204000 uc-zgv20190204000_old
[root@hs-10-20-18-175 docker_fc]# docker ps -a
CONTAINER ID        IMAGE                                                       COMMAND             CREATED             STATUS                       PORTS                                                                                                                NAMES
f74fe392e913        192.168.44.132/uc20/uc-zgv20190204000:uc20                  "bash"              27 hours ago        Exited (137) 6 minutes ago                                                                                                                       uc-zgv20190204000_old
b9eb76fdd09b        192.168.44.132/o4uft/of20170630c_20200312fb:o4uft           "dumb-init bash"    3 weeks ago         Exited (137) 11 years ago                                                                                                                              of20170630c_20200312fb

2.docker 制作自定义文件
#1.创建一个 Dockerfile 文件,并指定自定义镜像信息。
# Dockerfile 文件中常用的内容
from: 指定当前自定义镜像依赖的环境
copy: 将相对路径下的内容复制到自定义镜像中
workdir: 声明镜像的默认工作目录
cmd: 需要执行的命令(在workdir下执行,cmd可以写多个,只以最后一个为准)
# 举个栗子,自定义一个tomcat镜像,并且将ssm.war部署到tomcat中
from daocloud.io/library/tomcat:8.5.15-jre8
copy ssm.war /usr/local/tomcat/webapps
#2.将准备好的 Dockerfile 和相应的文件拖拽到 Linux 操作系统中,通过 Docker 的命令制作镜像
docker build -t 镜像名称:[tag] .
3. Docker-Compose
之前运行一个镜像,需要添加大量的参数。
可以通过 Docker-Compose 编写这些参数。
Docker-Compose 可以帮助我们批量地管理容器。
只需要一个 docker-compose.yml 文件去维护即可。
4.将容器制作成镜像
#格式:docker  commit  -m  '镜像描述'  -a  '制作者'  容器名  镜像名
#1.查询当前镜像
[root@hs-10-20-18-175 docker_fc]# docker ps
CONTAINER ID        IMAGE                                     COMMAND             CREATED             STATUS              PORTS                                                                                                                                                                                                                                                                                                                                                                                                      NAMES
7dcefc7a1c85        192.168.44.132/uc20/ucv20190207000:uc20   "bash"              6 minutes ago       Up 6 minutes        0.0.0.0:1521->1521/tcp, 0.0.0.0:5123->5123/tcp, 0.0.0.0:5127->5127/tcp, 0.0.0.0:5224->5224/tcp, 0.0.0.0:9003->9003/tcp, 0.0.0.0:9009->9009/tcp, 0.0.0.0:9898->9898/tcp, 0.0.0.0:15827->15827/tcp, 0.0.0.0:15841->15841/tcp, 0.0.0.0:15844-15845->15844-15845/tcp, 0.0.0.0:15858-15859->15858-15859/tcp, 0.0.0.0:15861->15861/tcp, 0.0.0.0:15864->15864/tcp, 0.0.0.0:2024->22/tcp, 0.0.0.0:1524->1522/tcp   ucv20190207000
[root@hs-10-20-18-175 docker_fc]# 


#格式:docker  commit  -m  '镜像描述'  -a  '制作者'  容器名  镜像名
#2.将容器制作成镜像
[root@hs-10-20-18-175 docker_fc]# docker commit -m 'ucv20190207000_fc' -a 'fc' ucv20190207000 ucv20190207000_fc:uc20
sha256:69872616774af10ef582bc1b3241d499ef21df0422094fb490fcd319de703c34
#查看一下当前的镜像(新制作的镜像为ucv20190207000_fc)
[root@hs-10-20-18-175 docker_fc]# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
ucv20190207000_fc                                 latest              69872616774a        49 seconds ago      23.72 GB
192.168.44.132/uc20/uc-zgv20190204000             uc20                5eba8fd7d56f        8 months ago        20.89 GB
192.168.44.132/uc20/ucv20190207000                uc20                da5f5aed9eee        10 months ago       17.72 GB
192.168.44.132/uc20/ucv20200103000_20201130auto   uc20                93ef245f40fd        4 years ago         26.12 GB

#3.修改镜像名
[root@hs-10-20-18-175 docker_fc]# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
ucv20210100000-fc                                 uc20                2f04a95bd278        22 hours ago        38.13 GB
[root@hs-10-20-18-175 docker_fc]# docker tag 2f04a95bd278 ucv20210100000-fc-1:uc201
[root@hs-10-20-18-175 docker_fc]# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
ucv20210100000-fc-1                               uc201               2f04a95bd278        22 hours ago        38.13 GB

#4.将制作好的镜像打包成 tar 包
[root@hs-10-20-18-175 docker_fc]# docker save -o ucv20190207000_fc.tar  ucv20190207000_fc 
[root@hs-10-20-18-175 docker_fc]# ls
dockerCreate_0.sh  dockerCreate.sh  dockerPullImage.sh  dockerStartOracle.sh  env.db  ucv20190207000_fc.tar

#5.从 tar 包中加载镜像
#查看当前镜像
[root@hs-10-20-18-175 docker_fc]# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
ucv20200100000-fc-bak                             uc20                f5bf1b4b0551        18 hours ago        30.71 GB
192.168.44.132/uc20/uc-zgv20190204000             uc20                5eba8fd7d56f        8 months ago        20.89 GB
192.168.44.132/uc20/ucv20190207000                uc20                da5f5aed9eee        10 months ago       17.72 GB
192.168.44.132/uc20/ucv20200103000_20201130auto   uc20                93ef245f40fd        4 years ago         26.12 GB
[root@hs-10-20-18-175 docker_fc]# ls
dockerCreate_0.sh  dockerCreate_fc.sh  dockerCreate.sh  dockerPullImage.sh  dockerStartOracle.sh  ucv20200100000-fc.tar  ucv20210100000-fc.tar
#加载镜像
[root@hs-10-20-18-175 docker_fc]# docker load < ucv20200100000-fc.tar
Loaded image: ucv20200100000-fc:uc20
#查看镜像
[root@hs-10-20-18-175 docker_fc]# docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
ucv20200100000-fc-bak                             uc20                f5bf1b4b0551        18 hours ago        30.71 GB
ucv20200100000-fc                                 uc20                f5bf1b4b0551        18 hours ago        30.71 GB
192.168.44.132/uc20/uc-zgv20190204000             uc20                5eba8fd7d56f        8 months ago        20.89 GB
192.168.44.132/uc20/ucv20190207000                uc20                da5f5aed9eee        10 months ago       17.72 GB
192.168.44.132/uc20/ucv20200103000_20201130auto   uc20                93ef245f40fd        4 years ago         26.12 GB



参考资料:https://www.cnblogs.com/pjcd-32718195/p/11762079.html
posted @ 2021-04-15 20:09  洛克十年  阅读(84)  评论(0编辑  收藏  举报