zoukankan      html  css  js  c++  java
  • Docker基础知识

    “什么是Docker“?,”为什么要使用Docker”?

    官方定义:

    Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用及依赖到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口;

    通俗解释:

    一款产品从开发到上线,到操作系统,到运行环境,再到应用配置,我们需要关注很多东西,这是大多数互联网不得不面对的问题,特别是版本的迭代,不同版本环境的兼容等等;Docker给出了一个标准化的解决方案,“从搬家,到搬楼”,即之前我们配置一个应用的运行环境,要安装很多软件,如:Java/Tomcat/Mysql/Jdbc驱动等等,安装配置麻烦,而且不能跨平台,换个服务器还要重新配置以便,移植性差,而Docker做到了“一次构建,处处运行”,将这些配置完的软件生成镜像,在别的服务器上直接调用即可,不需要重新去配置;

    Docker核心概念:

    docker主机(Host):安装了docker程序的机器(docker直接安装在操作系统之上的);

    docker客户端(Client):连接docker主机进行操作的程序;

    docker仓库(Registry):用来保存各种打包好的软件镜像;

    docker镜像(Images):软件打包好的镜像,放在docker仓库中;

    docker容器(Container):镜像启动后的实例称为一个容器;容器是一个独立运行的一个或一组应用;

    Docker安装:

    在Linux操作系统上安装docker,本人是通过虚拟机来完成的,涉及到的虚拟机的安装,本文中不做说明。

    1.检查Linux内核版本(docker安装要求内核版本是3.10及以上)

    [root@localhost /]# uname -r
    3.10.0-1160.el7.x86_64
    [root@localhost /]# 

    2.安装docker(CentOS7和CentOS8安装的结果好像是不一样的,此处以CentOS7来说明)

    yum install docker

    3.安装完成之后,可通过下面命令来操作

    #启动docker
    [root@localhost /]# systemctl start docker
    #查看docker版本
    [root@localhost /]# docker -v
    Docker version 1.13.1, build 7d71120/1.13.1
    #查看docker状态
    [root@localhost /]# systemctl status docker
    ● docker.service - Docker Application Container Engine
       Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
       Active: active (running) since 二 2021-10-19 18:01:03 CST; 20s ago
         Docs: http://docs.docker.com
     Main PID: 4984 (dockerd-current)
        Tasks: 17
       Memory: 22.5M
       CGroup: /system.slice/docker.service
               ├─4984 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver...
               └─4989 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /...
    
    10月 19 18:01:01 localhost.localdomain dockerd-current[4984]: time="2021-10-19T18:01:01.764522960+08:00" level=info msg="libcontainerd: new containerd process, pid: 4989"
    10月 19 18:01:03 localhost.localdomain dockerd-current[4984]: time="2021-10-19T18:01:03.169740757+08:00" level=info msg="Graph migration to content-addressabili...seconds"
    10月 19 18:01:03 localhost.localdomain dockerd-current[4984]: time="2021-10-19T18:01:03.171913151+08:00" level=info msg="Loading containers: start."
    10月 19 18:01:03 localhost.localdomain dockerd-current[4984]: time="2021-10-19T18:01:03.187520490+08:00" level=info msg="Firewalld running: false"
    10月 19 18:01:03 localhost.localdomain dockerd-current[4984]: time="2021-10-19T18:01:03.363977682+08:00" level=info msg="Default bridge (docker0) is assigned wi...address"
    10月 19 18:01:03 localhost.localdomain dockerd-current[4984]: time="2021-10-19T18:01:03.765296618+08:00" level=info msg="Loading containers: done."
    10月 19 18:01:03 localhost.localdomain dockerd-current[4984]: time="2021-10-19T18:01:03.862231739+08:00" level=info msg="Daemon has completed initialization"
    10月 19 18:01:03 localhost.localdomain dockerd-current[4984]: time="2021-10-19T18:01:03.862256098+08:00" level=info msg="Docker daemon" commit="7d71120/1.13.1" ...n=1.13.1
    10月 19 18:01:03 localhost.localdomain dockerd-current[4984]: time="2021-10-19T18:01:03.882963717+08:00" level=info msg="API listen on /var/run/docker.sock"
    10月 19 18:01:03 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
    Hint: Some lines were ellipsized, use -l to show in full.
    #停止docker
    [root@localhost /]# systemctl stop docker
    [root@localhost /]#

    Docker常用的操作命令

    1)、镜像操作命令

    ①检索镜像文件命令:docker search 关键字

    [root@localhost /]# docker search tomcat
    INDEX       NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    docker.io   docker.io/tomcat                        Apache Tomcat is an open source implementa...   3152      [OK]       
    docker.io   docker.io/tomee                         Apache TomEE is an all-Apache Java EE cert...   93        [OK]       
    docker.io   docker.io/dordoka/tomcat                Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba...   58                   [OK]
    docker.io   docker.io/consol/tomcat-7.0             Tomcat 7.0.57, 8080, "admin/admin"              18                   [OK]
    docker.io   docker.io/cloudesire/tomcat             Tomcat server, 6/7/8                            15                   [OK]
    docker.io   docker.io/aallam/tomcat-mysql           Debian, Oracle JDK, Tomcat & MySQL              13                   [OK]
    docker.io   docker.io/arm32v7/tomcat                Apache Tomcat is an open source implementa...   11                   
    docker.io   docker.io/andreptb/tomcat               Debian Jessie based image with Apache Tomc...   10                   [OK]
    docker.io   docker.io/rightctrl/tomcat              CentOS , Oracle Java, tomcat application s...   7                    [OK]
    docker.io   docker.io/arm64v8/tomcat                Apache Tomcat is an open source implementa...   6                    
    docker.io   docker.io/unidata/tomcat-docker         Security-hardened Tomcat Docker container.      5                    [OK]
    docker.io   docker.io/amd64/tomcat                  Apache Tomcat is an open source implementa...   3                    
    docker.io   docker.io/cfje/tomcat-resource          Tomcat Concourse Resource                       2                    
    docker.io   docker.io/fabric8/tomcat-8              Fabric8 Tomcat 8 Image                          2                    [OK]
    docker.io   docker.io/jelastic/tomcat               An image of the Tomcat Java application se...   2                    
    docker.io   docker.io/oobsri/tomcat8                Testing CI Jobs with different names.           2                    
    docker.io   docker.io/99taxis/tomcat7               Tomcat7                                         1                    [OK]
    docker.io   docker.io/camptocamp/tomcat-logback     Docker image for tomcat with logback integ...   1                    [OK]
    docker.io   docker.io/chenyufeng/tomcat-centos      tomcat基于centos6的镜像                              1                    [OK]
    docker.io   docker.io/i386/tomcat                   Apache Tomcat is an open source implementa...   1                    
    docker.io   docker.io/picoded/tomcat7               tomcat7 with jre8 and MANAGER_USER / MANAG...   1                    [OK]
    docker.io   docker.io/ppc64le/tomcat                Apache Tomcat is an open source implementa...   1                    
    docker.io   docker.io/s390x/tomcat                  Apache Tomcat is an open source implementa...   0                    
    docker.io   docker.io/secoresearch/tomcat-varnish   Tomcat and Varnish 5.0                          0                    [OK]
    docker.io   docker.io/softwareplant/tomcat          Tomcat images for jira-cloud testing            0                    [OK]
    [root@localhost /]# 

    ②拉取镜像命令:docker pull 镜像名:tag

    [root@localhost /]# docker pull tomcat
    Using default tag: latest             #默认不写tag标签时,拉取的就是最新版
    Trying to pull repository docker.io/library/tomcat ... 
    latest: Pulling from docker.io/library/tomcat
    bb7d5a84853b: Pull complete 
    f02b617c6a8c: Pull complete 
    d32e17419b7e: Pull complete 
    c9d2d81226a4: Pull complete 
    fab4960f9cd2: Pull complete 
    da1c1e7baf6d: Pull complete 
    79b231561270: Pull complete 
    7d337880d8b4: Pull complete 
    2df65a31be06: Pull complete 
    10cbf519de23: Pull complete 
    Digest: sha256:7c30d3c92b191ec2b84f64be99326f9c83a4a4e079c899ee4d0a9a41abca8406
    Status: Downloaded newer image for docker.io/tomcat:latest
    [root@localhost /]# 

    ③查看下载的所有镜像文件:docker images

    [root@localhost /]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    docker.io/mysql     latest              ecac195d15af        12 hours ago        516 MB
    docker.io/tomcat    latest              4ce9babdd885        5 days ago          680 MB
    [root@localhost /]# 

    ④删除下载的镜像文件:docker rmi 镜像名/镜像ID

    [root@localhost /]# docker rmi redis
    Untagged: redis:latest
    Untagged: docker.io/redis@sha256:a89cb097693dd354de598d279c304a1c73ee550fbfff6d9ee515568e0c749cfe
    Deleted: sha256:7faaec68323851b2265bddb239bd9476c7d4e4335e9fd88cbfcc1df374dded2f
    Deleted: sha256:e6deb90762475cda72e21895911f830ed99fd1cc6d920d92873270be91235274
    Deleted: sha256:2649acad13241d9c8d81e49357bc66cce459b352ded7f423d70ede7bd3bb7b89
    Deleted: sha256:64007bba5fc220df4d3da33cecdc2d55dd6a73528c138b0fa1acd79fd6a9c217
    Deleted: sha256:b2cc2f1bf8b1cca8ba7c19e1697f7b73755903ad8f880b83673fd6a697aca935
    Deleted: sha256:fbd1283ab782925be4d990bd4bebe9ad5e5cf9a525abfb6fa87465e072da9d31
    Deleted: sha256:e8b689711f21f9301c40bf2131ce1a1905c3aa09def1de5ec43cf0adf652576e
    [root@localhost /]# 

    2)、容器操作命令

    ①根据镜像启动容器: docker run

    [root@localhost /]# docker run --name tomcat -d -p 8888:8080 tomcat
    2a52a81aa35b0a030096a9ea8b81c91ef7edb8f931f2654581c5c9655f544734
    [root@localhost /]# 
    
    ●--name  容器自定义名称
    ● -d   后台运行
    ● -p   端口映射
    
    注意:容器创建之后可以停止运行的容器,但是下次启动的时候可以通过start来启动,除非容器删除了才通过run来启动

    ②查看容器命令: docker ps

    #查看运行中的容器
    [root@localhost /]# docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
    2a52a81aa35b        tomcat              "catalina.sh run"   8 minutes ago       Up 8 minutes        0.0.0.0:8888->8080/tcp   tomcat
    #查看所有的容器(包括运行中的和已经停止的)
    [root@localhost /]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
    2a52a81aa35b        tomcat              "catalina.sh run"   8 minutes ago       Up 8 minutes        0.0.0.0:8888->8080/tcp   tomcat
    [root@localhost /]# 

     ③停止运行中的容器:docker stop 容器名/容器ID

    [root@localhost /]# docker stop tomcat
    tomcat
    [root@localhost /]# 

    ④删除容器: docker rm 容器名/容器ID

    [root@localhost /]# docker rm tomcat
    tomcat
    [root@localhost /]# 

     ⑤查看容器日志:docker logs 容器名/容器ID

    [root@localhost /]# docker logs tomcat
    NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
    19-Oct-2021 10:29:37.143 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/10.0.12
    19-Oct-2021 10:29:37.146 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Sep 28 2021 13:34:21 UTC
    19-Oct-2021 10:29:37.162 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 10.0.12.0
    19-Oct-2021 10:29:37.162 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
    19-Oct-2021 10:29:37.168 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            3.10.0-1160.el7.x86_64
    19-Oct-2021 10:29:37.168 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
    19-Oct-2021 10:29:37.169 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/local/openjdk-11
    19-Oct-2021 10:29:37.169 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           11.0.12+7
    19-Oct-2021 10:29:37.169 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
    19-Oct-2021 10:29:37.169 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
    19-Oct-2021 10:29:37.169 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
    19-Oct-2021 10:29:37.200 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
    19-Oct-2021 10:29:37.200 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
    19-Oct-2021 10:29:37.201 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED
    19-Oct-2021 10:29:37.201 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED
    19-Oct-2021 10:29:37.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
    19-Oct-2021 10:29:37.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
    19-Oct-2021 10:29:37.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
    19-Oct-2021 10:29:37.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
    19-Oct-2021 10:29:37.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
    19-Oct-2021 10:29:37.202 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
    19-Oct-2021 10:29:37.203 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
    19-Oct-2021 10:29:37.203 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
    19-Oct-2021 10:29:37.203 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
    19-Oct-2021 10:29:37.204 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
    19-Oct-2021 10:29:37.222 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.31] using APR version [1.7.0].
    19-Oct-2021 10:29:37.222 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
    19-Oct-2021 10:29:37.231 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1k  25 Mar 2021]
    19-Oct-2021 10:29:38.652 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
    19-Oct-2021 10:29:38.749 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [2379] milliseconds
    19-Oct-2021 10:29:38.963 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
    19-Oct-2021 10:29:38.963 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/10.0.12]
    19-Oct-2021 10:29:38.999 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
    19-Oct-2021 10:29:39.037 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [288] milliseconds
    [root@localhost /]# 
  • 相关阅读:
    【洛谷】P2880 [USACO07JAN]平衡的阵容Balanced Lineup(st表)
    【洛谷】P1052 过河(状压dp)
    【洛谷】P1541 乌龟棋(四维背包dp)
    【BZOJ】4721: [Noip2016]蚯蚓 / 【洛谷】P2827 蚯蚓(单调队列)
    【洛谷】P1064 金明的预算方案(dp)
    【洛谷】P3908 异或之和(异或)
    【洛谷】P2434 [SDOI2005]区间(暴力)
    【洛谷】P2694 接金币(排序)
    【BZOJ】1012: [JSOI2008]最大数maxnumber /【洛谷】1198(线段树)
    【游记】noip2017酱油记
  • 原文地址:https://www.cnblogs.com/ammyblog/p/15425820.html
Copyright © 2011-2022 走看看