docker的简单使用

docker的简单使用

学习地址:https://www.bilibili.com/video/BV1og4y1q7M4

跟着视频学习的,一点点跟着视频打了一遍

帮助命令

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

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

镜像命令

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

root@kongzhou-virtual-machine:~# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
hello-world          latest    bf756fb1ae65   11 months ago   13.3kB

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

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

搜索镜像

root@kongzhou-virtual-machine:~# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10269     [OK]       
mariadb                           MariaDB is a community-developed fork of MyS…   3794      [OK] 

# 可选项,通过STAR过滤
docker search mysql --filter=STARs=3000

root@kongzhou-virtual-machine:~# docker search mysql --filter=STARs=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   10269     [OK]       
mariadb   MariaDB is a community-developed fork of MyS…   3794      [OK]   

下载镜像

# 下载镜像 docker pull 镜像名[:tag] 指定版本下载,但下载版本必须docker中有

root@kongzhou-virtual-machine:~# docker pull mysql 
Using default tag: latest # 默认下载最新版
latest: Pulling from library/mysql
6ec7b7d162b2: Pull complete # 分层下载,docker images的核心 联合文件系统
fedd960d3481: Pull complete 
7ab947313861: Pull complete 
64f92f19e638: Pull complete 
3e80b17bff96: Pull complete 
014e976799f9: Pull complete 
59ae84fee1b3: Pull complete 
ffe10de703ea: Pull complete 
657af6d90c83: Pull complete 
98bfb480322c: Pull complete 
9f2c4202ac29: Pull complete 
a369b92bfc99: Pull complete 
Digest: sha256:365e891b22abd3336d65baefc475b4a9a1e29a01a7b6b5be04367fcc9f373bb7  # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址

# 两者相同
docker pull mysql
docker.io/library/mysql:latest

# docker pull mysql:5.7 指定版本下载
root@kongzhou-virtual-machine:~# docker pull mysql:5.7
5.7: Pulling from library/mysql
6ec7b7d162b2: Already exists  # 很明显显示已经存在,因为上面下载的mysql中已经下载过这些的文件,可以共用,非常方便
fedd960d3481: Already exists 
7ab947313861: Already exists 
64f92f19e638: Already exists 
3e80b17bff96: Already exists 
014e976799f9: Already exists 
59ae84fee1b3: Already exists 
7d1da2a18e2e: Pull complete 
301a28b700b9: Pull complete 
979b389fc71f: Pull complete 
403f729b1bad: Pull complete 
Digest: sha256:d4ca82cee68dce98aa72a1c48b5ef5ce9f1538265831132187871b78e768aed1
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

删除镜像

docker rmi -f 容器id                 # 删除指定容器
docker rmi -f 容器id 容器id 容器id  	# 删除多个容器
docker rmi -f $(docker images -aq)  # 删除全部的容器

容器命令

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

docker pull centos

新建容器并启动

启动镜像

docker run [可选参数] image

# 参数说明
--name="Name"   容器名字 pwn1 pwn2 用来区分容器
-d              后台方式运行
-it             使用交互方式运行
-p              指定容器端口 -p 8080:8080
   -p ip:主机端口:容器端口
   -p 主机端口:容器端口(常用)
   -p 容器端口
   容器端口
-P               随机指定端口

# 测试,启动并进入容器
root@kongzhou-virtual-machine:~# docker run -it centos /bin/bash
[root@0b0a0a20f059 /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr

# 从容器这种返回主机
[root@0b0a0a20f059 /]# exit
exit
root@kongzhou-virtual-machine:~# 

列出所有的运行的容器

docker ps 命令 
-a    #列出当前正在运行的容器
-n=?  #显示最近?个创建的容器
-q    #只显示容器的编号

退出容器

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     # 强制停止当前容器

常用其他命令

后台启动容器

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xSAb4hBh-1611084797975)(C:%5CUsers%5Ckongzhou%5CAppData%5CRoaming%5CTypora%5Ctypora-user-images%5Cimage-20210118160535997.png)]

# 命令 docker run -d 镜像名

# 问题docker ps,发现centos停止了
# 常见的坑:docker使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就是没有程序了

查看日志

docker logs -f -t --tail 容器id  # 没有日志
# 自己编写一段 shell脚本
root@kongzhou-virtual-machine:~# docker run -d centos /bin/sh -C "while true;do echo kongzhou;sleep 1;done"

# root@kongzhou-virtual-machine:~# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
3cf6fc9bc61c   centos    "/bin/sh -c 'while t…"   15 seconds ago   Up 13 seconds             agitated_visvesvaraya

# 显示日志
 -tf           # 显示日志
 --tail number # 要显示日志条数
 
 root@kongzhou-virtual-machine:~# docker logs -f -t --tail 10 3cf6fc9bc61c 

查看容器中进程信息

# docker top 容器id
root@kongzhou-virtual-machine:~# docker top 3cf6fc9bc61c 
UID      PID        PPID        C         STIME      TTY        TIME       CMD
root    53105      53082        0        16:14        ?        00:00:00   /bin/sh -c while true;do echo kongzhou;sleep 1;done
root    53844      53105        0        16:25        ?        00:00:00   /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1        

查看镜像的元数据

# 命令 docker inspect 容器id
# 测试
root@kongzhou-virtual-machine:~# docker inspect 3cf6fc9bc61c
[
    {
        "Id": "3cf6fc9bc61c00647b95b88f6a8af9e35eabd06c7fdc46278ee38da237050410",
        "Created": "2021-01-18T08:14:46.818963923Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo kongzhou;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 53105,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-01-18T08:14:47.428211885Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/3cf6fc9bc61c00647b95b88f6a8af9e35eabd06c7fdc46278ee38da237050410/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/3cf6fc9bc61c00647b95b88f6a8af9e35eabd06c7fdc46278ee38da237050410/hostname",
        "HostsPath": "/var/lib/docker/containers/3cf6fc9bc61c00647b95b88f6a8af9e35eabd06c7fdc46278ee38da237050410/hosts",
        "LogPath": "/var/lib/docker/containers/3cf6fc9bc61c00647b95b88f6a8af9e35eabd06c7fdc46278ee38da237050410/3cf6fc9bc61c00647b95b88f6a8af9e35eabd06c7fdc46278ee38da237050410-json.log",
        "Name": "/agitated_visvesvaraya",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "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/ace00a02f3d7912e0a1880237c628ebe03694655b08d2414359cffad7d093123-init/diff:/var/lib/docker/overlay2/6046f44170c775dd8216fb38c64d14f468d165d0271a044c16cd7a1e09e5046d/diff",
                "MergedDir": "/var/lib/docker/overlay2/ace00a02f3d7912e0a1880237c628ebe03694655b08d2414359cffad7d093123/merged",
                "UpperDir": "/var/lib/docker/overlay2/ace00a02f3d7912e0a1880237c628ebe03694655b08d2414359cffad7d093123/diff",
                "WorkDir": "/var/lib/docker/overlay2/ace00a02f3d7912e0a1880237c628ebe03694655b08d2414359cffad7d093123/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "3cf6fc9bc61c",
            "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 kongzhou;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": "ff8484bea0cc6734e492713115ca7c2c3e224d0d88dde3bd864e031c18bf38b6",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/ff8484bea0cc",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "0adfef4b6d95c9578bdd03c16773fe4071cc0ad124f701eac4c546433442fc83",
            "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": "f989d926b61313d32ba7442e1e69f4d7498f0c8e28818de42af301fc823b0386",
                    "EndpointID": "0adfef4b6d95c9578bdd03c16773fe4071cc0ad124f701eac4c546433442fc83",
                    "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 # 进入容器后开启一个新的终端,可以在里面操作(常用)

docker attach 容器id  # 进入容器正在执行的终端,不会启动新的进程

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

docker cp 容器id:容器内路径 目的的主机路径

# 查看容器id
root@kongzhou-virtual-machine:/home# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
3e26eda4d0d9   centos    "/bin/bash"   4 minutes ago   Up 4 minutes             pensive_bassi

# 进入docker容器内部
root@kongzhou-virtual-machine:/home# docker attach 3e26eda4d0d9
[root@3e26eda4d0d9 /]#

# 在容器内新建一个文件
[root@3e26eda4d0d9 /]# touch test.java
[root@3e26eda4d0d9 /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  test.java  usr
dev  home  lib64  media       opt  root  sbin  sys  tmp        var

# 从文件拷贝到主机
root@kongzhou-virtual-machine:/home/kongzhou# docker cp 3e26eda4d0d9:/test.java /home
root@kongzhou-virtual-machine:/home/kongzhou# cd /home
root@kongzhou-virtual-machine:/home# ls
kongzhou  test.java
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 黑客帝国 设计师:白松林 返回首页