Docker常用命令

Docker安装

docker的安装参考博客

阿里云镜像加速器

1.登录阿里云找到容器镜像服务
在这里插入图片描述
2.找到镜像加速地址
在这里插入图片描述
3.配置使用


sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://9hxyc96f.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

底层原理

Docker是怎么工作的?

Docker是一个client - Server结构的系统,Docker的守护进程运行在主机上。通过Socket从客户端访问
DockerServer接收到Docker - Client的指令,就会执行这个命令

Docker为什么比vm(虚拟机)快

1.Docker有着比虚拟机更少的抽象层
2.docker利用的是宿主机的内核,vm需要的是GuestOS
所以说,新建一个容器的时候。docker不需要虚拟机一样重新加载一个操作系统内核,避免引导。虚拟机是加载GuestOS,分钟级别的,而docker是利用宿主机的操作系统,省略了这个复杂的过程,秒级!

Docker的常用命令

docker version  # 显示docker的版本信息
docker info        # 显示docker的系统信息,包括镜像和容器的数量
docker  --help    # 万能命令

docker帮助文档地址:https://docs.docker.com/reference/

镜像命令

docker image 查看所有本地的主机上的镜像

[root@xiaotiantian /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   4 months ago   13.3kB

#解释
REPOSITORY   镜像的仓库源
TAG          镜像的标签
IMAGE ID     镜像的id
CREATED      镜像的创建时间
SIZE         镜像的大小

#可选项
  -a, --all             #列出所有镜像
  -q, --quiet           #只显示镜像的id

docker search 搜索镜像

[root@xiaotiantian /]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11171     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4242      [OK]     

#可选项,通过搜索来过滤
  --filter=STARS=3000    #搜索出来的镜像就是STARS大于3000的
[root@xiaotiantian /]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11171     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4242      [OK]       
[root@xiaotiantian /]# docker search mysql --filter=STARS=5000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11171     [OK] 

docker pull下载镜像

#下载镜像 docker pull 镜像名[:tag]
[root@xiaotiantian /]# docker pull mysql
Using default tag: latest    #如果不写 tag,默认就是latest
latest: Pulling from library/mysql
33847f680f63: Pull complete  #分层下载 docker image的核心,联合文件系统
5cb67864e624: Pull complete 
1a2b594783f5: Pull complete 
b30e406dd925: Pull complete 
48901e306e4c: Pull complete 
603d2b7147fd: Pull complete 
802aa684c1c4: Pull complete 
715d3c143a06: Pull complete 
6978e1b7a511: Pull complete 
f0d78b0ac1be: Pull complete 
35a94d251ed1: Pull complete 
36f75719b1a9: Pull complete 
Digest: sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd   #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址

#等价于
docker pull mysql
docker pull docker.io/library/mysql:latest
#指定版本下载
[root@xiaotiantian /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
33847f680f63: Already exists 
5cb67864e624: Already exists 
1a2b594783f5: Already exists 
b30e406dd925: Already exists 
48901e306e4c: Already exists 
603d2b7147fd: Already exists 
802aa684c1c4: Already exists 
5b5a19178915: Pull complete 
f9ce7411c6e4: Pull complete 
f51f6977d9b2: Pull complete 
aeb6b16ce012: Pull complete 
Digest: sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

在这里插入图片描述
docker rmi 删除镜像

[root@xiaotiantian /]# docker rmi -f 镜像id  #删除指定的镜像
[root@xiaotiantian /]# docker rmi -f  镜像id  镜像id  镜像id  #删除多个镜像
[root@xiaotiantian /]# docker rmi -f $(docker images -aq)   #删除全部的镜像

在这里插入图片描述

容器命令

说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习

docker pull centos

新建容器并启动

docker run [可选参数] image
#参数说明
--name="Name"    容器名字  tomcat01  tomgcat02,用来区分容器
-d                           后台方式运行
-it                           使用交互方式运行,进入容器查看内容
-p                           指定容器的端口  -p  8080:8080
     -p  ip:主机端口:容器端口
     -p  主机端口:容器端口
     -p  容器端口
     容器端口
 -P                         随机指定端口
 #测试,启动并进入容器
[root@xiaotiantian /]# docker run -it centos /bin/bash
[root@392b0f31dd3d /]# ls                     #查看容器内的centos,基础版本,很多命令都是不完善的
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@392b0f31dd3d /]# exit                 #从容器退回到主机
exit
[root@xiaotiantian /]# ls
bin   dev  home        lib    lost+found  mnt  patch  root  sbin  sys  usr  www
boot  etc  install.sh  lib64  media       opt  proc   run   srv   tmp  var
[root@xiaotiantian /]# 

列出所有运行的容器

#docker ps  命令
              #列出当前正在运行的容器
  -a         #列出当前正在运行的容器+带出历史运行过的容器
  -n=?    #显示最近创建的容器
  -q        #只显示容器的编号
[root@xiaotiantian /]# docker ps     #列出当前正在运行的容器
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@xiaotiantian /]# docker ps -a      #列出当前正在运行的容器+带出历史运行过的容器
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
392b0f31dd3d   centos         "/bin/bash"   5 minutes ago   Exited (0) 2 minutes ago             romantic_heisenberg
570c10b657ba   d1165f221234   "/hello"      23 hours ago    Exited (0) 23 hours ago              strange_nobel
[root@xiaotiantian /]# docker ps -a -n=1     #显示最近创建的容器
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
392b0f31dd3d   centos    "/bin/bash"   9 minutes ago   Exited (0) 7 minutes ago             romantic_heisenberg
[root@xiaotiantian /]# docker ps -aq     #只显示容器的编号
392b0f31dd3d
570c10b657ba
[root@xiaotiantian /]# 

退出容器

exit   #直接容器停止并退出
Ctrl+P+Q   #容器不停止退出

删除容器

docker rm  容器id               #删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -f
docker rm -f $(docker ps -aq)       #删除所有的容器
docker ps -a -q|xargs docker rm   #删除所有的容器

启动和停止容器的操作

docker start  容器id      #启动容器
docker restart 容器id    #重启容器
docker stop 容器id       #停止当前正在运行的容器
docker kill 容器id          #强制停止当前容器

常用其他命令

后台启动容器

#命令 docker run -d 镜像名
[root@xiaotiantian /]# docker run -d centos
#问题docker ps 发现centos停止了
#常见的坑,docker  容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
#nginx 容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了

查看日志

docker logs -f -t --tail    容器id     没有日志
#自己编写一段shell脚本
[root@xiaotiantian /]# docker run -d centos /bin/sh -c "while true;do echo xiaotiantian;sleep 1;done"
[root@xiaotiantian /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
c7d461417b5d   centos    "/bin/sh -c 'while t…"   3 seconds ago   Up 2 seconds             musing_chaplygin
#显示日志
-tf   #显示日志
--tail number  #要显示日志条数
[root@xiaotiantian /]# docker logs -tf --tail 10 c7d461417b5d

查看容器中进程信息 ps

#命令  docker top 容器id
[root@xiaotiantian /]# docker top c7d461417b5d
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                5848                5830                0                   12:04               ?                   00:00:01            /bin/sh -c while true;do echo xiaotiantian;sleep 1;done
root                23457               5848                0                   13:47               ?  

查看镜像的元数据

#命令
docker inspect 容器id
#测试
[root@xiaotiantian /]# docker inspect c7d461417b5d
[
    {
        "Id": "c7d461417b5d1896ea74890a7ebf6dde0fc8dc3b3dc0647ec974b2fc7ae3eb4a",
        "Created": "2021-07-27T04:04:33.682252626Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo xiaotiantian;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 5848,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-07-27T04:04:34.095709741Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/c7d461417b5d1896ea74890a7ebf6dde0fc8dc3b3dc0647ec974b2fc7ae3eb4a/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/c7d461417b5d1896ea74890a7ebf6dde0fc8dc3b3dc0647ec974b2fc7ae3eb4a/hostname",
        "HostsPath": "/var/lib/docker/containers/c7d461417b5d1896ea74890a7ebf6dde0fc8dc3b3dc0647ec974b2fc7ae3eb4a/hosts",
        "LogPath": "/var/lib/docker/containers/c7d461417b5d1896ea74890a7ebf6dde0fc8dc3b3dc0647ec974b2fc7ae3eb4a/c7d461417b5d1896ea74890a7ebf6dde0fc8dc3b3dc0647ec974b2fc7ae3eb4a-json.log",
        "Name": "/musing_chaplygin",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/da0540a24cc30662e11ec3aa2020c250f583335252dd549ade300f81de020a7f-init/diff:/var/lib/docker/overlay2/319c7bd695683809eac395ed68c06100bf425894aaa09ec1d64e24c74d933be6/diff",
                "MergedDir": "/var/lib/docker/overlay2/da0540a24cc30662e11ec3aa2020c250f583335252dd549ade300f81de020a7f/merged",
                "UpperDir": "/var/lib/docker/overlay2/da0540a24cc30662e11ec3aa2020c250f583335252dd549ade300f81de020a7f/diff",
                "WorkDir": "/var/lib/docker/overlay2/da0540a24cc30662e11ec3aa2020c250f583335252dd549ade300f81de020a7f/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "c7d461417b5d",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "while true;do echo xiaotiantian;sleep 1;done"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20201204",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "fbea413bee1268cd7cce26814c9b07ee16aecaa378f94d1b6afb3e9b619066b1",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/fbea413bee12",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "a847042a32bcb101959f9a4c04fc85cd98b84a4f9c07c80282105d55f754ec25",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "4355884d2a0626af1d548e204c5f638b9975904f1b4716b690190093fdb0a387",
                    "EndpointID": "a847042a32bcb101959f9a4c04fc85cd98b84a4f9c07c80282105d55f754ec25",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

进入当前正在运行的容器

#我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置
#命令  方式一
docker exec -it 容器id bashshell
[root@xiaotiantian /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS     NAMES
c7d461417b5d   centos    "/bin/sh -c 'while t…"   2 hours ago   Up 2 hours             musing_chaplygin
[root@xiaotiantian /]# docker exec -it c7d461417b5d /bin/bash
[root@c7d461417b5d /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@c7d461417b5d /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 04:04 ?        00:00:02 /bin/sh -c while true;do echo xiaotiantian;sleep 1;done
root      8911     0  0 06:33 pts/0    00:00:00 /bin/bash
root      8956     1  0 06:33 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sl
root      8957  8911  0 06:33 pts/0    00:00:00 ps -ef
#方式二
docker attach 容器id
#测试
[root@xiaotiantian /]# docker attach c7d461417b5d
正在执行的代码...
#docker exec     #进行容器后开启一个新的终端,可以在里面操作(常用)
#docker attach   #进入容器正在执行的终端,不含启动新的进程

从容器内拷贝文件到主机上

docker cp 容器id:容器路径  目的主机路径
#查看当前主机目录下
[root@xiaotiantian home]# ls
redis  www  xiaotian
#进入docker容器内部
[root@xiaotiantian home]# docker attach 983ccee6e02e
[root@983ccee6e02e /]# cd /home
#在容器内新建一个文件
[root@983ccee6e02e home]# touch test.java
[root@983ccee6e02e home]# exit
exit
[root@xiaotiantian home]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@xiaotiantian home]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                      PORTS     NAMES
983ccee6e02e   centos    "/bin/bash"   4 minutes ago   Exited (0) 18 seconds ago             funny_williams
#将这文件拷贝出来到主机上
[root@xiaotiantian home]# docker cp 983ccee6e02e:/home/test.java /home
[root@xiaotiantian home]# ls
redis  test.java  www  xiaotian
[root@xiaotiantian home]# 
#拷贝是一个手动过程,未来我们使用 -v卷的技术可以实现
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值