zoukankan      html  css  js  c++  java
  • Docker的学习第二天

    Docker架构图

    镜像:image,类似于模板的意思,通过这个模板创建容器服务,如tomcat 镜像,---》run--->tomcat1容器,提供给服务器

      通过这个镜像可以创建多个容器,最终服务运行或者项目就是在容器中

    容器:container:

    Docker利用容器技术,独立运行一个或者一个应用,通过镜像来创建的

    启动、删除、停止 、基本命令

    目前可以把容器理解为一个简易的linux系统,项目就放在这个微型的linux系统

    仓库:respository:用来存放镜像的地方,仓库分为公有仓库和私有仓库,docker hub,阿里云、华为云等,默认配置国外的,需配置镜像加速

     基础环境配置以下:

    [root@fengfang ~]# uname -r
    3.10.0-957.el7.x86_64
    [root@fengfang ~]# cat /etc/os-release
    NAME="CentOS Linux"
    VERSION="7 (Core)"
    ID="centos"
    ID_LIKE="rhel fedora"
    VERSION_ID="7"
    PRETTY_NAME="CentOS Linux 7 (Core)"
    ANSI_COLOR="0;31"
    CPE_NAME="cpe:/o:centos:centos:7"
    HOME_URL="https://www.centos.org/"
    BUG_REPORT_URL="https://bugs.centos.org/"

    CENTOS_MANTISBT_PROJECT="CentOS-7"
    CENTOS_MANTISBT_PROJECT_VERSION="7"
    REDHAT_SUPPORT_PRODUCT="centos"
    REDHAT_SUPPORT_PRODUCT_VERSION="7"

    第一步,卸载旧的版本Docker各版本,命令如下:

    $ sudo yum remove docker 
                      docker-client 
                      docker-client-latest 
                      docker-common 
                      docker-latest 
                      docker-latest-logrotate 
                      docker-logrotate 
                      docker-engine
    第二步:安装所需环境的命令
    sudo yum install -y yum-utils
    第三步:设置镜像的仓库,默认使用国外的,会比较慢
    yum-config-manager 
        --add-repo 
        https://download.docker.com/linux/centos/docker-ce.repo

    yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #推荐使用阿里云
    第四步,更新yum
    yum makecache fast
    第五步:安装Docker相关的配置 docker-ce 社区版本  ee 企业版本,一般使用社区版本
    yum install docker-ce docker-ce-cli containerd.io
    安装过程中可能要提示是否确认,输入Y就行
    第六步:启动docker
    systemctl start docker
    第七步:查看docker版本
    docker version
    
    

     第八步:测试hello world

    docker run hello-world
    
    

     第九步:查看一下下载的的hello-world 镜像

    docker images

     了解:卸载docker

    第一步:yum remove docker-ce docker-ce-cli containerd.io

    第二步:删除资源:rm -rf /var/lib/docker

    #/var/lib/docker默认工作空间

    阿里云镜像加速方法

    1、登陆阿里云,找到镜像加速器

    sudo mkdir -p /etc/docker

    sudo tee /etc/docker/daemon.json <<-'EOF'

    {

    "registry-mirrors": ["https://hcqmmgyf.mirror.aliyuncs.com"]

    }

    EOF

    sudo systemctl daemon-reload

    sudo systemctl restart docker

     按上图,四个步骤执行

    完成后就完成了阿里云镜像加速

     run的运行流程图:

     docker的工作原理

    docker是一个client-server结构的系统,docker的守护进程运行在主机上,通过socket从客户端访问

    dockerServer接收到dockerClient的指令就会执行这个命令

     docker为什么比VM快

    1、docker有着比VM更少的抽象层

    2、docker利用的是宿主机的内核,VM需要的的guestOs

                             Vm                                                                docker

    所以说,新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核,避免引导操作,虚拟机是加载guest OS,分钟级别

    而docker是利用宿主的操作系统,省略了了这个复杂的过程

    学完完所有的命令,再回头来看这段理论,就会更清晰

    docker的常用命令

     1、帮助命令

    docker version  #显示docker的版本信息

    docker info   #显示docker的系统信息,包括镜像和容器的数量

    docker 命令 --help    

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

    镜像命令

    1、docker images  #查看所有的镜像

     perpository:镜像的仓库源

    tag:镜像的标签

    image id:镜像的ID

    created:镜像的创建时间

    size:镜像的大小

    -a,--all    #列出所有镜像

    -q,-quiet  #只显示镜像的ID

     如使用命令:docker images -aq

    2、docker search mysql 命令,搜索镜像

     -f  过滤条件,搜索stars大于3000的

    docker search mysql -f=stars=3000

    docker search mysql --limit 5   #取前五条数据

    3、docker pull 下载镜像

    下载mysql镜像

    [root@fengfang ~]# docker pull mysql
    Using default tag: latest    #如果不写tag,默认是latest
    latest: Pulling from library/mysql    
    8559a31e96f4: Pull complete    #分层下载,docker image的核心,联合文件系统
    d51ce1c2e575: Pull complete
    c2344adc4858: Pull complete
    fcf3ceff18fc: Pull complete
    16da0c38dc5b: Pull complete
    b905d1797e97: Pull complete
    4b50d1c6b05c: Pull complete
    571e8a282156: Pull complete
    e7cc823c6090: Pull complete
    89f6364a8689: Pull complete
    55e5b10f33a4: Pull complete
    68129e7a0316: Pull complete
    Digest: sha256:c455bbcaa8b9c5c636c45f6184f970caeb3d8b545a0390e1b72a253e07aef8fd  #签名
    Status: Downloaded newer image for mysql:latest    
    docker.io/library/mysql:latest     #真实地址

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

    指定版本下载mysql 5.7版本

    [root@fengfang ~]# docker pull mysql:5.7
    5.7: Pulling from library/mysql
    8559a31e96f4: Already exists    #已经存在的,就不会再下载
    d51ce1c2e575: Already exists
    c2344adc4858: Already exists
    fcf3ceff18fc: Already exists
    16da0c38dc5b: Already exists
    b905d1797e97: Already exists
    4b50d1c6b05c: Already exists
    0a52a5c57cd9: Pull complete
    3b816a39d367: Pull complete
    13ee22d6b3bb: Pull complete
    e517c3d2ba35: Pull complete
    Digest: sha256:ea560da3b6f2f3ad79fd76652cb9031407c5112246a6fb5724ea895e95d74032
    Status: Downloaded newer image for mysql:5.7
    docker.io/library/mysql:5.7

    4、删除镜像docker rmi 

    docker rmi -f 容器Id    #指定容器ID删除

    docker rmi -f 容器Id 容器Id 容器Id     #删除多个容器

    docker rmi -f $(docker images -aq)   #删除所有容器

    [root@fengfang ~]# docker rmi --help

    Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]

    Remove one or more images

    Options:
    -f, --force Force removal of the image
    --no-prune Do not delete untagged parents

    [root@fengfang ~]# docker rmi -f 5ac22cccc3ae   #指定ID删除镜像
    Untagged: mysql:latest
    Untagged: mysql@sha256:c455bbcaa8b9c5c636c45f6184f970caeb3d8b545a0390e1b72a253e07aef8fd
    Deleted: sha256:5ac22cccc3ae67ca42ed92b55c8fa7c68967ec6b875d15d761467d40097368b6
    Deleted: sha256:86c56823286628a66aa344188924e576c85b94b4734c418a6a0e123068170c4f
    Deleted: sha256:28f36292125ccc90319e3153c5eb83d374d2875c9e46a7538568fc32458ec034
    Deleted: sha256:291205778291931aee087875f7d1c30382db776809095e30260079f998025426
    Deleted: sha256:620e22d166022716e2d8984417abecdd637b30e3815e9f3b543e93cdde1d09e9
    Deleted: sha256:f67b13ad42222e45e0eaf18adbc9f408e2dc32cdb814be4f3243a6ab0b5bcd0a
    [root@fengfang ~]# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    mysql 5.7 d05c76dbbfcf 7 days ago 448MB
    hello-world latest bf756fb1ae65 6 months ago 13.3kB
    [root@fengfang ~]# docker rmi -f $(docker images -aq)   #删除所有镜像
    Untagged: mysql:5.7
    Untagged: mysql@sha256:ea560da3b6f2f3ad79fd76652cb9031407c5112246a6fb5724ea895e95d74032
    Deleted: sha256:d05c76dbbfcf3e1d969eecc04d0aa461e0f95204f6833f62edb73cca7b62fcd4
    Deleted: sha256:840de3dd46e7b6203f85075d3bbc784b151e0de21f77dfa37744fc8edeaf766b
    Deleted: sha256:7438475d185887f36358065f793c5699b459bc13271aee0b606b9d68230452ef
    Deleted: sha256:323859cca1e70d6db23f15dd1d6c0c66e08f116147477663f3be2d94c89ba3a8
    Deleted: sha256:14a1c67348bc38b8f7316be7993dea0c03c5375e7728dcffb2da90ceed74c1f0
    Deleted: sha256:09687cd9cdf4c704fde969fdba370c2d848bc614689712bef1a31d0d581f2007
    Deleted: sha256:b704a4a65bf536f82e5d8b86e633d19185e26313de8380162e778feb2852011a
    Deleted: sha256:c37206160543786228aa0cce738e85343173851faa44bb4dc07dc9b7dc4ff1c1
    Deleted: sha256:12912c9ec523f648130e663d9d4f0a47c1841a0064d4152bcf7b2a97f96326eb
    Deleted: sha256:57d29ad88aa49f0f439592755722e70710501b366e2be6125c95accc43464844
    Deleted: sha256:b17c024283d0302615c6f0c825137da9db607d49a83d2215a79733afbbaeb7c3
    Deleted: sha256:13cb14c2acd34e45446a50af25cb05095a17624678dbafbcc9e26086547c1d74
    Untagged: hello-world:latest
    Untagged: hello-world@sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202
    Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b

    容器命令

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

    docker pull centos    #下载linux容器

    1、新建容器并启动

    docker run [可选参数]   image

    参数说明:

    --name ="name"    #给容器命名 如tomcat1 /tomcat2

    -d                           #后台方式运行

    -i/-t                         #使用交互方式运行,进入容器查看内容

    -p        #指定容器端口

     #测试/启动并进入容器

    [root@fengfang ~]# docker images
    REPOSITORY TAG IMAGE ID CREATED SIZE
    centos latest 831691599b88 4 weeks ago 215MB
    [root@fengfang ~]# docker run -it centos /bin/bash
    [root@3c8a42e17e53 /]# ls    #进入容器
    bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
    [root@3c8a42e17e53 /]# exit   #退出容器
    exit

    2、列出所有在运行的容器:docker ps

       #列出当前正在运行的容器

    -a     #列出当前正在运行的容器+带出历史运行过的容器

    -n=?    #列出最近创建的容器,?表示容器个数

    -q       #只显示容器的编号

    3、退出容器

    exit     #直接窗口停止并退出

    ctrl+p+Q   #容器不停止退出

    4、删除容器

    docker rm   容器id       #删除指定的容器,不能删除正在运行的容器,如果要强制删除使用:rm -f

    docker rm -f $(docker ps -aq)    #删除所有容器

    docker rm -a -q|xargs docker rm    #删除所有容器

     5、启动或停止容器的操作

    docker start 容器Id           #启动容器

    docker restart 容器Id  #重启容器

    docker stop 容器Id        #停止容器

    docker Kill 容器Id            #强制停止当前容器

    6、后台启动命令

    #    命令    docker run -d 容器

    docker run -d centos      #后台启动centos容器      

    #问题:docker ps 发现centos 停止了

    常见的坑:docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止

    #nginx    容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了

    7、查看日志

    docker logs -tf  --tail   容器Id

     #自己编写一段shell脚本,并在后台执行

    命令:docker run -d centos /bin/sh -c "while true;do echo ok;sleep 1;done;"

    #显示指定行数日志的命令

    docker logs -tf --tail 10 5207c831e6fc

    8、查看容器中的进程信息:docker top 容器Id

    [root@fengfang ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    5207c831e6fc centos "/bin/sh -c 'while t…" 4 minutes ago Up 4 minutes beautiful_satoshi
    [root@fengfang ~]# docker top 5207c831e6fc
    UID PID PPID C STIME TTY TIME CMD
    root 27638 27620 0 10:59 ? 00:00:00 /bin/sh -c while true;do echo ok;sleep 1;done;
    root 28077 27638 0 11:03 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
    [root@fengfang ~]#

    9、查看镜像的元数据:docker inspect 容器Id

    docker inspect 90f6e62833a2
    [
    {
    "Id": "90f6e62833a20d21d1bb6ae6fcf008d1dfaa6847ce0c3e9d8974480f5401054d",
    "Created": "2020-07-22T01:51:22.216515504Z",
    "Path": "/bin/bash",
    "Args": [],
    "State": {
    "Status": "exited",
    "Running": false,
    "Paused": false,
    "Restarting": false,
    "OOMKilled": false,
    "Dead": false,
    "Pid": 0,
    "ExitCode": 0,
    "Error": "",
    "StartedAt": "2020-07-22T01:51:22.648729597Z",
    "FinishedAt": "2020-07-22T02:54:29.039817622Z"
    },
    "Image": "sha256:831691599b88ad6cc2a4abbd0e89661a121aff14cfa289ad840fd3946f274f1f",
    "ResolvConfPath": "/var/lib/docker/containers/90f6e62833a20d21d1bb6ae6fcf008d1dfaa6847ce0c3e9d8974480f5401054d/resolv.conf",
    "HostnamePath": "/var/lib/docker/containers/90f6e62833a20d21d1bb6ae6fcf008d1dfaa6847ce0c3e9d8974480f5401054d/hostname",
    "HostsPath": "/var/lib/docker/containers/90f6e62833a20d21d1bb6ae6fcf008d1dfaa6847ce0c3e9d8974480f5401054d/hosts",
    "LogPath": "/var/lib/docker/containers/90f6e62833a20d21d1bb6ae6fcf008d1dfaa6847ce0c3e9d8974480f5401054d/90f6e62833a20d21d1bb6ae6fcf008d1dfaa6847ce0c3e9d8974480f5401054d-json.log",
    "Name": "/great_aryabhata",
    "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,
    "Capabilities": null,
    "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/acf06b0e9661512964a44a9967550a22dc18541ace3285a9a37e360cad69f080-init/diff:/var/lib/docker/overlay2/bf12b9b766266fb81c51f7af319f6e39c0315c70ad4967d4dd609e732e153803/diff",
    "MergedDir": "/var/lib/docker/overlay2/acf06b0e9661512964a44a9967550a22dc18541ace3285a9a37e360cad69f080/merged",
    "UpperDir": "/var/lib/docker/overlay2/acf06b0e9661512964a44a9967550a22dc18541ace3285a9a37e360cad69f080/diff",
    "WorkDir": "/var/lib/docker/overlay2/acf06b0e9661512964a44a9967550a22dc18541ace3285a9a37e360cad69f080/work"
    },
    "Name": "overlay2"
    },
    "Mounts": [],
    "Config": {
    "Hostname": "90f6e62833a2",
    "Domainname": "",
    "User": "",
    "AttachStdin": true,
    "AttachStdout": true,
    "AttachStderr": true,
    "Tty": true,
    "OpenStdin": true,
    "StdinOnce": true,
    "Env": [
    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    ],
    "Cmd": [
    "/bin/bash"
    ],
    "Image": "centos",
    "Volumes": null,
    "WorkingDir": "",
    "Entrypoint": null,
    "OnBuild": null,
    "Labels": {
    "org.label-schema.build-date": "20200611",
    "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": "4bb6f0a87d2e52f97ea9afdec457ace346421b318e511ae04dd52d39f681d8d2",
    "HairpinMode": false,
    "LinkLocalIPv6Address": "",
    "LinkLocalIPv6PrefixLen": 0,
    "Ports": {},
    "SandboxKey": "/var/run/docker/netns/4bb6f0a87d2e",
    "SecondaryIPAddresses": null,
    "SecondaryIPv6Addresses": null,
    "EndpointID": "",
    "Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "IPAddress": "",
    "IPPrefixLen": 0,
    "IPv6Gateway": "",
    "MacAddress": "",
    "Networks": {
    "bridge": {
    "IPAMConfig": null,
    "Links": null,
    "Aliases": null,
    "NetworkID": "f3f639d8384219f8537fd152bb9e368be7194e28733694f67340f259bb33804b",
    "EndpointID": "",
    "Gateway": "",
    "IPAddress": "",
    "IPPrefixLen": 0,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "",
    "DriverOpts": null
    }
    }
    }
    }
    ]

    11、进入当前正在运行的容器

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

    #命令

    方式1:docker exec -it 容器Id bashShell

     方式2:docker attach 容器Id

    备注:

    #docker exec #进入容器后开启一个新的终端,可以在里面操作(常用 )

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

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

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

    [root@fengfang ~]# docker run -it centos /bin/bash
    [root@72cab24d142f /]# [root@fengfang ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    72cab24d142f centos "/bin/bash" 14 seconds ago Up 13 seconds nifty_merkle
    [root@fengfang ~]# cd /home
    [root@fengfang home]# ls
    [root@fengfang home]# mkdir lxh
    [root@fengfang home]# ls
    lxh
    [root@fengfang home]# docker attach 72cab24d142f    #进入容器,并创建一个test.java文件
    [root@72cab24d142f /]# ls
    bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
    [root@72cab24d142f /]# cd /home
    [root@72cab24d142f home]# ls
    [root@72cab24d142f home]# touch test.java    #创建一个文件
    [root@72cab24d142f home]# ls
    test.java
    [root@72cab24d142f home]# exit
    exit
    [root@fengfang home]# docker ps -a
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    72cab24d142f centos "/bin/bash" 8 minutes ago Exited (0) 7 seconds ago nifty_merkle
    [root@fengfang home]# ls
    lxh
    [root@fengfang home]# cd /

    [root@fengfang /]# docker start 72cab24d142f

    72cab24d142f
    [root@fengfang /]# ls
    bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
    [root@fengfang /]# cd /home
    [root@fengfang home]# ls
    lxh

    [root@fengfang home]# docker cp 72cab24d142f:/home/test.java /home/lxh   #从容器中拷贝文件到主机上

    [root@fengfang home]# cd lxh
    [root@fengfang lxh]# ls    #查看文件
    test.java

    备注:拷贝是一个手动过程,未来我们使用  -V 卷的技术,可以实现容器的/home和主机的/home目录联动

     
    **************只要学不死就往死里学!************
  • 相关阅读:
    IE9不能在线打开InfoPath表单的解决办法
    在Word中使用Quick Parts功能
    .NET WinForm程序在Windows7下实现玻璃效果和任务栏进度条效果
    如何对已经发布过的InfoPath模板进行修改
    .NET WinForm中给DataGridView自定义ToolTip并设置ToolTip的样式
    有关SharePoint Client Object应用的笔记
    解决VS 2010中编译程序时弹出"Type universe cannot resolve assembly"的错误
    SharePoint客户端对象模型"(400) Bad Request"错误
    C# Method Attribute and Reflection
    .NET WinForm下一个支持更新ProgressBar进度的DataGridView导出数据到Excel的类
  • 原文地址:https://www.cnblogs.com/lxhaaron/p/13355159.html
Copyright © 2011-2022 走看看