docker

1.类似于 虚拟机 阔以虚拟出很多容器 在容器里的 运行各种环境, 每个容器之间互不干扰,且容器占用资源很少,阔以充分利用 硬件资源


Docker帮助命令


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

官方 api :https://docs.docker.com/engine/reference/builder/

镜像命令


[root@controller ~]# docker images
REPOSITORY                     TAG               IMAGE ID       CREATED         SIZE
ticket                         v1.2.004          7063389943a6   13 hours ago    699MB

# 解释
REPOSITORY 镜像的仓库源
TAG        镜像的版本信息
IMAGE ID   镜像的ID
CREATED    镜像的创建时间
SIZE       镜像的占用大小

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

docker仓库搜索主页:https://hub.docker.com/

docker search 搜索镜像

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

docker pull 下载镜像

#下载镜像 docker pull 镜像名[:TAG]
[root@controller ~]# docker pull mysql 
Using default tag: latest #如果不写 tag, 默认就是latest
latest: Pulling from library/mysql
f7ec5a41d630: Already exists 
9444bb562699: Pull complete #分层下载 docker images的核心 联合文件系统
6a4207b96940: Pull complete 
181cefd361ce: Pull complete 
8a2090759d8a: Pull complete 
15f235e0d7ee: Pull complete 
d870539cd9db: Pull complete 
5726073179b6: Pull complete 
eadfac8b2520: Pull complete 
f5936a8c3f2b: Pull complete 
cca8ee89e625: Pull complete 
6c79df02586a: Pull complete 
Digest: sha256:6e0014cdd88092545557dee5e9eb7e1a3c84c9a14ad2418d5f2231e930967a38 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址

#等价于他
docker pull mysql
docker.io/library/mysql:latest

##指定版本下载
[root@controller ~]# docker pull mysql:5.6
5.6: Pulling from library/mysql
62deabe7a6db: Pull complete 
81ba35f9cdf1: Pull complete 
5c8f6a545aa1: Pull complete 
9ced99c328da: Pull complete 
1983b7d65f8f: Pull complete 
2fc89d436e20: Pull complete 
6cd56cc7fc71: Pull complete 
ff26e0e5f66f: Pull complete 
cf5586a4456d: Pull complete 
53d17e698ea2: Pull complete 
b5c84062b3c4: Pull complete 
Digest: sha256:5d63153bf35070c230b9d41a754651d99053cbf19a9eec70ebeba926a304761f
Status: Downloaded newer image for mysql:5.6
docker.io/library/mysql:5.6
Shell

docker rmi 删除镜像

[root@controller ~]# docker rmi -f 镜像id #删除指定的镜像
[root@controller ~]# docker rmi -f 镜像id 镜像id 镜像id 镜像id #删除多个镜像 空格 隔开
[root@controller ~]# docker rmi -f $(docker images 镜像名称 -aq) #删除指定的名称的所有镜像
[root@controller ~]# docker rmi -f $(docker images -aq)#删除所有的镜像
Shell

容器命令


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

docker pull centos 
Shell

新建容器并启动


docker run [可选参数] images

#参数说明
--name="Name" #容器名字 tomcat 01 tomcat 02 ,用来区分容器 
-d            #后台方式运行
-it           #使用交互方式运行,进入容器查看内容
-p            #指定容器的端口 -p 8080:8080
    -p #ip:主机端口:容器端口(常用)
    -p #主机端口:容器端口(常用)
    -p #容器端口
-P(大写)      #随机指定端口   

#测试 ,启动并进入容器
[root@controller ~]# docker run -it centos /bin/bash
[root@be3162ff8495 /]# 
[root@be3162ff8495 /]# ls   #查看容器内的centos 基础版本 ,很多命令都是不完善的
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
#从容器中退回主机
[root@be3162ff8495 /]# exit
exit
[root@controller ~]# ls
anaconda-ks.cfg  bak  index.html.1  QiangPiao
Shell

列出所有的运行中容器


#docker ps 命令
     #列出当前正在运行的容器
-a   #列出当前正在运行的容器 + 带出历史运行过的容器
-n=? #列出最近创建的容器
-q #只显示容器的编号
[root@controller ~]# docker ps
CONTAINER ID   IMAGE                                COMMAND                  CREATED        STATUS        PORTS     NAMES
74bdd8e4bf1c   ticket:v1.2.004                      "sh -c 'python run.p…"   13 hours ago   Up 13 hours             ticket
1c35fec0aa37   yinaoxiong/12306_code_server:amd64   "gunicorn app:app -c…"   13 hours ago   Up 13 hours   80/tcp    captcha
[root@controller ~]# docker ps -a
CONTAINER ID   IMAGE                                COMMAND                  CREATED         STATUS                     PORTS     NAMES
be3162ff8495   centos                               "/bin/bash"              7 minutes ago   Exited (0) 3 minutes ago             elastic_kepler
74bdd8e4bf1c   ticket:v1.2.004                      "sh -c 'python run.p…"   13 hours ago    Up 13 hours                          ticket
1c35fec0aa37   yinaoxiong/12306_code_server:amd64   "gunicorn app:app -c…"   13 hours ago    Up 13 hours                80/tcp    captcha
87bf14180ae9   d76e13c67caa                         "/bin/sh -c 'pip ins…"   14 hours ago    Exited (1) 14 hours ago              heuristic_euler
2c595523607f   d76e13c67caa                         "/bin/sh -c 'pip ins…"   14 hours ago    Exited (1) 14 hours ago              kind_hofstadter
8788a0a702aa   d76e13c67caa                         "/bin/sh -c 'pip ins…"   15 hours ago    Exited (1) 15 hours ago              suspicious_rubin
74aa75d1e057   d76e13c67caa                         "/bin/sh -c 'pip ins…"   15 hours ago    Exited (1) 15 hours ago              jovial_lichterman
7a83b8abafa6   d76e13c67caa                         "/bin/sh -c 'pip ins…"   15 hours ago    Exited (1) 15 hours ago              bold_golick
12167e7c08d7   d76e13c67caa                         "/bin/sh -c 'pip ins…"   15 hours ago    Exited (1) 15 hours ago              epic_antonelli
Shell

退出容器


exit #退出容器停止退出
Ctrl + P + Q #容器不停止退出
Shell

重新进入容器

#重新进入已经 运行中的容器
[root@controller ~]# docker exec -it centos 容器id /bin/bash
Shell

删除容器


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

启动和停止容器的操作

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

常用其他命令


后台启动

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

查看日志

docker logs #查看日志
[root@controller ~]# docker logs -f -t --tail 次数(all)  容器id
#编写一段shell脚本
docker run -d centos /bin/sh -c "while true;do echo wumengxiang;sleep 1 ;done"
#显示日志
-tf           #显示日志 跟踪日志输出
--tail number #要显示的次数
[root@controller ~]# docker logs -ft --tail 10 26722b7802b6
Shell

查看容器中进程信息 PS

#docker top 容器id
[root@controller ~]# docker top 26722b7802b6

Shell

查看镜像的元数据

#查看镜像元数据 配置信息 
#命令
#docker inspect 容器id
[root@controller ~]# docker inspect 26722b7802b6
[
    {
        "Id": "26722b7802b62dfb0d9fbb5f2a61ebbab13cdc9d6055184cd6a10d6465167bb7",
        "Created": "2021-04-17T11:08:50.849988313Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo wumengxiang;sleep 1 ;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 9180,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-04-17T11:08:51.187033823Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/26722b7802b62dfb0d9fbb5f2a61ebbab13cdc9d6055184cd6a10d6465167bb7/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/26722b7802b62dfb0d9fbb5f2a61ebbab13cdc9d6055184cd6a10d6465167bb7/hostname",
        "HostsPath": "/var/lib/docker/containers/26722b7802b62dfb0d9fbb5f2a61ebbab13cdc9d6055184cd6a10d6465167bb7/hosts",
        "LogPath": "/var/lib/docker/containers/26722b7802b62dfb0d9fbb5f2a61ebbab13cdc9d6055184cd6a10d6465167bb7/26722b7802b62dfb0d9fbb5f2a61ebbab13cdc9d6055184cd6a10d6465167bb7-json.log",
        "Name": "/fervent_lewin",
        "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/88a13f7ac3637c3016862cfa00644a1ad59ae0a85ae4f1200824125e2ba3d4b3-init/diff:/var/lib/docker/overlay2/9a2e43421200bf74f4c1be365d8ac917353bdc97443beca702dff9564d43b2ba/diff",
                "MergedDir": "/var/lib/docker/overlay2/88a13f7ac3637c3016862cfa00644a1ad59ae0a85ae4f1200824125e2ba3d4b3/merged",
                "UpperDir": "/var/lib/docker/overlay2/88a13f7ac3637c3016862cfa00644a1ad59ae0a85ae4f1200824125e2ba3d4b3/diff",
                "WorkDir": "/var/lib/docker/overlay2/88a13f7ac3637c3016862cfa00644a1ad59ae0a85ae4f1200824125e2ba3d4b3/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "26722b7802b6",
            "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 wumengxiang;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": "c6041bb5db9503603faad81fc3e54b30cd284cb9c58ad844b91053cd78534c7e",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/c6041bb5db95",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "229d7811b1c3ddc5dc782ab742ffe4d2f2b77ebbe81e4cbac7bc57e1bf37f668",
            "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": "f8e5e9e96cceb83aa520710e9d403fff8c22fc9663bb93567a94664a73b27f4c",
                    "EndpointID": "229d7811b1c3ddc5dc782ab742ffe4d2f2b77ebbe81e4cbac7bc57e1bf37f668",
                    "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
                }
            }
        }
    }
]
Shell

进入当前正在运行的容器

#我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置

#命令
docker exec -it 容器id  bashShell

[root@controller ~]# docker exec -it 254403079f5d /bin/bash
[root@254403079f5d /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@254403079f5d /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 14:39 pts/0    00:00:00 /bin/bash
root         22      0  0 14:40 pts/1    00:00:00 /bin/bash
root         38     22  0 14:40 pts/1    00:00:00 ps -ef

#方式二
docker attach 容器id
[root@controller ~]# docker attach 254403079f5d
正在执行当前的代码...

#docker exec    #进入容器开启一个新的终端,阔以在里边操作(常用)
#docker attach  #进入容器正在执行的终端,不会启动新的进程!
Shell

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

docker cp #容器id:容器内路径 目的的主机路径
[root@controller ~]# docker cp 254403079f5d:/root/1.txt /root

#进入容器内
[root@controller ~]# docker attach 254403079f5d
[root@254403079f5d /]# cd /root
[root@254403079f5d ~]# ls
anaconda-ks.cfg  anaconda-post.log  original-ks.cfg
#在容器内创建文件
[root@254403079f5d ~]# touch 1.txt
[root@254403079f5d ~]# vi 1.txt 
[root@254403079f5d ~]# touch 1.txtread escape sequence
[root@controller ~]# docker ps
CONTAINER ID   IMAGE                                COMMAND                  CREATED          STATUS          PORTS     NAMES
254403079f5d   centos                               "/bin/bash"              11 minutes ago   Up 11 minutes             silly_johnson
1c35fec0aa37   yinaoxiong/12306_code_server:amd64   "gunicorn app:app -c…"   24 hours ago     Up 24 hours     80/tcp    captcha
#拷贝容器内的文件到主机
[root@controller ~]# docker cp 254403079f5d:/root/1.txt /root
[root@controller ~]# cd /root
[root@controller ~]# ls
1.txt  74bdd8e4bf1c329da59c8e61f29305c8d0d4f12de16a8989c24af940312cedee-json.log  anaconda-ks.cfg  bak  index.html.1  log.txt  QiangPiao
[root@controller ~]# cat 1.txt 
䚲oot@controller ~]# docker attach 254403079f5d


#拷贝是一个手动过程,未来我们使用 -v 卷的技术实现

Shell

image-20210417230055219

image-20210417230055219

作业练习

#1,搜索镜像 search 建议取docker搜索,罗伊看到帮助
#2,下载镜像 pull
#3,运行测试
[root@controller ~]# docker images
REPOSITORY                     TAG               IMAGE ID       CREATED         SIZE
ticket                         v1.2.004          7063389943a6   26 hours ago    699MB
<none>                         <none>            093c1084dd98   26 hours ago    699MB
<none>                         <none>            91b3e3f0dcd9   26 hours ago    699MB
<none>                         <none>            cade8fef7277   26 hours ago    699MB
<none>                         <none>            d76e13c67caa   27 hours ago    677MB
nginx                          latest            62d49f9bab67   3 days ago      133MB
python                         3.7-slim-buster   30a42f143b4e   7 days ago      113MB
centos                         latest            300e315adb2f   4 months ago    209MB
yinaoxiong/12306_code_server   amd64             6438c6eddbe0   10 months ago   355MB
# -d 后台运行
# --name 给容器命名
# -p 宿主机端口:容器内部端口
[root@controller ~]# docker run -d --name nginx01 -p 3344:80 nginx
feec69ca5f0e35b9d18c7500eb6690664754139d256941cb4c942682eef18497
Shell
tomcat
#官方的使用
docker run -it --rm tomcat:9.0

#之前的启动都是后台,停止了容器之后,容器还是阔以查到 docker run -it --rm,一般用来测试 用完就删除了

# 下载在启动
docker pull tomcat

#启动运行
[root@controller ~]# docker run -d -p 3366:8080 --name tomcat01 tomcat
512d2385037059264c14307a6c22fbcfbc1306d9984f2b11d20da67370393db8

#测试访问没有问题
#进入容器
[root@controller ~]# docker exec -it tomcat01 /bin/bash

#发现问题 1.Linux 命令少了 2,没有webapps 。阿里云镜像的问题 默认是最小的 所有不必要的都剔除了 。
#保证最小可运行环境
Shell

思考问题:我们以后要部署环境,如果每次都要进入容器是不是十分麻烦?我们要是阔以在容器外部提供一个映射路径 webapps我们在外部放置项目,就自动同步到内部就好了

docker 容器 tomcat + 网站 docker mysql

作业:部署es +kibana
#es 暴漏的端口很多!
#es 十分的耗内存
#es 的数据一般需要放置到安全目录!挂载
#--net somenetwork ? 网络配置
#启动
docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:

#启动 就卡住了 dicker stats 查看cpu的状态
#es是 十分消耗内存的
#测试一下 es是不是成功了
#增加一下内存限制

{
  "name" : "c309e30fa307",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "zaVJda3bS2e5c5voJMNy-g",
  "version" : {
    "number" : "7.12.0",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "78722783c38caa25a70982b5b042074cde5d3b3a",
    "build_date" : "2021-03-18T06:17:15.410153305Z",
    "build_snapshot" : false,
    "lucene_version" : "8.8.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "d0 -e "discovery.type=single-node" -e   ES_JAVA_OPTS="-xms64m -xmx512m"  elasticsearch:7.12.0
Shell
最后修改:2022 年 03 月 29 日
如果觉得我的文章对你有用,请随意赞赏