zoukankan      html  css  js  c++  java
  • docker安装及基本使用

    安装Docker

    #docker分为企业版和社区版
    	企业版	: docker 
    	社区版	: docker-ce 
    
    #安装依赖
    [root@docker ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
    
    #安装阿里云源仓库
    [root@docker ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
    [root@docker ~]# sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
    
    #查看docker版本
    [root@docker ~]# yum list docker-ce --showduplicates | sort -r
    
    #选择最新的19版本,进行安装
    [root@docker ~]# yum install -y docker-ce-19.03.9
    
    [root@docker ~]# mkdir /etc/docker
    [root@docker ~]# cat > /etc/docker/daemon.json <<EOF
    {
      "registry-mirrors": ["https://dp0vxr93.mirror.aliyuncs.com"]
    }
    EOF
    
    [root@docker ~]# systemctl daemon-reload
    [root@docker ~]# systemctl enable --now docker
    
    #测试启动
    [root@docker ~]# docker info
    Client:
     Context:    default
     Debug Mode: false
     Plugins:
      app: Docker App (Docker Inc., v0.9.1-beta3)
      buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
      scan: Docker Scan (Docker Inc., v0.8.0)
    
    Server:
     Containers: 16
      Running: 3
      Paused: 0
      Stopped: 13
     Images: 5
     Server Version: 19.03.9
     Storage Driver: overlay2
      Backing Filesystem: xfs
      Supports d_type: true
      Native Overlay Diff: true
     Logging Driver: json-file
     Cgroup Driver: cgroupfs
     Plugins:
      Volume: local
      Network: bridge host ipvlan macvlan null overlay
      Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
     Swarm: inactive
     Runtimes: runc
     Default Runtime: runc
     Init Binary: docker-init
     containerd version: 5b46e404f6b9f661a205e28d59c982d3634148f8
     runc version: v1.0.2-0-g52b36a2
     init version: fec3683
     Security Options:
      seccomp
       Profile: default
     Kernel Version: 3.10.0-1160.el7.x86_64
     Operating System: CentOS Linux 7 (Core)
     OSType: linux
     Architecture: x86_64
     CPUs: 1
     Total Memory: 972.3MiB
     Name: docker
     ID: RAN2:RN3T:UHRI:G6GF:5ZTZ:JGC5:YGXR:77IH:XWQM:7X33:TFCG:JMCW
     Docker Root Dir: /var/lib/docker
     Debug Mode: false
     Registry: https://index.docker.io/v1/
     Labels:
     Experimental: false
     Insecure Registries:
      127.0.0.0/8
     Registry Mirrors:
      https://dp0vxr93.mirror.aliyuncs.com/
     Live Restore Enabled: false
    
    

    镜像

    1.什么是镜像

    #主要用来启动容器的模板,镜像一般是存在镜像仓库中的。
    
    #镜像仓库地址:https://hub.docker.com/search?type=image
    
    #仓库分为三种:
     -- 官方仓库:	hub.docker.com
     -- 第三方仓库:  https://cr.console.aliyun.com/cn-hangzhou/instance/repositories
     -- 自建仓库:    hao
     
    

    2.镜像的相关命令

    1)搜索镜像

    [root@docker ~]# docker search centos
      NAME        DESCRIPTION      				 STARS     OFFICIAL    AUTOMATED
    镜像的名称     该镜像的介绍      				收藏量	    官方  		 自建
    centos       The official build of CentOS.     6809      [OK]
    
    

    2)下载镜像

    #格式:
    docker pull [仓库的URL]/[名称空间]/[仓库的名称]:[版本号]
    
    [root@docker ~]# docker pull centos
    Using default tag: latest
    latest: Pulling from library/centos
    a1d0c7532777: Pull complete 
    Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
    Status: Downloaded newer image for centos:latest
    docker.io/library/centos:latest
    				
    #镜像名称的构成:
    #docker.io  /library       /centos    :latest
    [仓库URL]/[仓库的名称空间]/[仓库的名称]:[镜像的版本号]
    默认的仓库URL:docker.io
    默认的仓库名称空间:library
    仓库的名称:没有默认
    镜像版本号:默认是latest
    
    

    3)查看本地镜像列表

    docker images 或者 docker image ls
    
    image子命令主要支持如下选项:
    -a : 列出所有(包括临时文件)镜像文件
    --digests=true|false:列出镜像的数字摘要值
    -q : 仅显示ID信息
    
    [root@docker ~]# docker images 
    REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
    centos       latest    5d0da3dc9764   5 weeks ago   231MB
    				
    REPOSITORY:[仓库URL]/[仓库的名称空间]/[仓库的名称]
    TAG	: 版本号
    IMAGE ID : 缩写版的镜像ID
    CREATED	:创建该容器到现在的时间
    SIZE	:镜像大小
    				
    [root@docker ~]# docker image ls
    REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
    centos       latest    5d0da3dc9764   5 weeks ago   231MB
    
    
    [root@docker ~]# docker images -a
    REPOSITORY      TAG            IMAGE ID       CREATED        SIZE
    mysql           5.7            938b57d64674   3 weeks ago    448MB
    nginx           latest         87a94228f133   4 weeks ago    133MB
    centos          latest         5d0da3dc9764   8 weeks ago    231MB
    
    [root@docker ~]# docker images --digests
    REPOSITORY      TAG            DIGEST                                                                    IMAGE ID       CREATED        SIZE
    mysql           5.7            sha256:2db8bfd2656b51ded5d938abcded8d32ec6181a9eae8dfc7ddf87a656ef97e97   938b57d64674   3 weeks ago    448MB
    nginx           latest         sha256:644a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36   87a94228f133   4 weeks ago    133MB
    centos          latest         sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177   5d0da3dc9764   8 weeks ago    231MB
    alvinos/php     wordpress-v2   sha256:f195eeebcc33422a7792888c469cd3a19e96c1b3fd06b0c4c8b6fd25f7692009   f79487654bc0   7 months ago   785MB
    alvinos/nginx   wordpress-v2   sha256:d2d3b0a77e3c2a98a6dc4ffd919f05e91361fd9443d87ca1d9722097bde65188   c5aeafcf528a   7 months ago   371MB
    
    [root@docker ~]# docker images -q
    938b57d64674
    87a94228f133
    5d0da3dc9764
    f79487654bc0
    c5aeafcf528a
    

    4)查看镜像的详情

    #获取镜像的详细信息,包括PID、作者、架构等等
    docker inspect [镜像名称或ID]
     
    [root@docker ~]# docker inspect centos
    [
        {
            "Id": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
            "RepoTags": [
                "centos:latest"
            ],
            "RepoDigests": [
                "centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177"
            ],
            "Parent": "",
            "Comment": "",
            "Created": "2021-09-15T18:20:05.184694267Z",
            "Container": "9bf8a9e2ddff4c0d76a587c40239679f29c863a967f23abf7a5babb6c2121bf1",
            "ContainerConfig": {
                "Hostname": "9bf8a9e2ddff",
                "Domainname": "",
    ---------- -------- --------- 省略 --------- ---------- ----------- ---------
    
    
    

    5)为镜像添加tag

    #为了方便后续工作中使用特定的镜像,还可以使用docker tag命令来为本地的镜像添加标签
    docker tag [原来的名称] [新名称]
      
    [root@docker ~]# docker images
    REPOSITORY      TAG            IMAGE ID       CREATED        SIZE
    nginx           latest         87a94228f133   4 weeks ago    133MB
    
    [root@docker ~]# docker tag nginx:latest nginx:1.21.1
    [root@docker ~]# docker images
    REPOSITORY      TAG            IMAGE ID       CREATED        SIZE
    nginx           1.21.1         87a94228f133   4 weeks ago    133MB
    
    [root@docker ~]# docker tag nginx:1.21.1 nginx-01:1.21.1
    [root@docker ~]# docker images
    REPOSITORY      TAG            IMAGE ID       CREATED        SIZE
    nginx-01        1.21.1         87a94228f133   4 weeks ago    133MB
    nginx           1.21.1         87a94228f133   4 weeks ago    133MB
    nginx           latest         87a94228f133   4 weeks ago    133MB
    
    

    6)上传镜像

    #将镜像上传至远程仓库。
    1、登录仓库
    [root@docker ~]# docker login --username=zzcwep registry.cn-hangzhou.aliyuncs.com
    Password: 
    WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
    Configure a credential helper to remove this warning. See
    https://docs.docker.com/engine/reference/commandline/login/#credentials-store
    
    Login Succeeded
    	
    2、修改镜像名称	
    [root@docker ~]# docker tag  registry.cn-hangzhou.aliyuncs.com/k8sos/centos:latest
    	
    3、上传镜像
    docker push [仓库URL]/[仓库的名称空间]/[仓库的名称]:[版本号]
    [root@docker ~]# docker push registry.cn-hangzhou.aliyuncs.com/kubernetes-zzc/centos:latest
    
    
    

    7)登陆仓库

    docker login [仓库URL]
    仓库URL默认是:docker.io
    
    

    8)删除镜像

     docker rmi [仓库的名称ID]		
    #参数:
      -f : 强制删除镜像。
    
    [root@docker ~]# docker rmi centos
    Untagged: centos:latest
    Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
    Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
    Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
    
    [root@docker ~]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
    nginx        latest    87a94228f133   13 days ago   133MB
    
    
    

    9)清理镜像

    #使用一段时间之后,docker会产生很多临时文件,以及一些没有被使用的镜像, 我们可以通过
     docker image prune 命令来进行清理。
    #参数:
     -a :删除所有无用的镜像,不光是临时镜像。
     -f :强制删除镜像,而不进行提示。
     
     
    

    10)查看镜像的历史

    [root@docker ~]# docker history centos
    IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
    5d0da3dc9764   5 weeks ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
    <missing>      5 weeks ago   /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
    <missing>      5 weeks ago   /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0…   231MB
    
    

    容器的使用

    1)查看本机容器列表

    docker ps # 查看本机容器列表
    
    [root@docker ~]# docker ps
    CONTAINER ID   IMAGE          COMMAND             CREATED  		 STATUS       PORTS      NAMES
     容器的ID		镜像         容器的启动命令        容器的创建时间   容器的状态    容器的端口  容器的名称
    e4c4fc806516   nginx    "/docker-entrypoint.…"   3 days ago      Up 3 days      80/tcp    nginx
    
    STATUS:
    UP : 		启动状态
    Exited :	停止状态
    Created :  容器已经创建,当时没有启动
    
    #参数:
     -a : 查看系统上所有的容器(包含未启动)
     -q : 只显示容器ID
    
    

    2)创建容器

    docker run		# docker run是创建并启动容器
    docker create 	# docker create是创建容器
    
    docker create 中的参数跟 docker run 类似 主要其本身的功能不同。
    
    -- docker run 
    #格式:
    docker run [参数] 镜像  [启动命令]
    
    参数和启动命令可以省略。
    
    容器的启动流程:
    1.查看本地是否存在正在使用的镜像。
    2.如果本地不存在使用的镜像,则去仓库下载
    3.根据镜像来启动容器
    
    #参数:
    -d 	: 以守护进程方式运行容器。
    --name : 自定义容器的名称
    -p 		: 指定一个端口映射
    -P		:随机一个端口映射
    -i		: 打开标准输出
    -t		:创建一个终端
    -e		:在容器内部增加一个环境变量
    -h		: 指定一个主机名(默认的主机名是容器的缩写版ID )
    -v 		: 指定一个挂载卷(将宿主主机的目录挂载到容器内,从而实现文件互通)
    --network	: 链接一个网桥
    --link		: 链接一个容器
    
    
    [root@docker ~]# docker run -d -it --name old  -p 80:80  nginx:1.21.1 bash
    48457e3ef84870380075efc935465785506904baad3010f60112d81b1aa582c4
    [root@docker ~]# docker ps
    CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                NAMES
    48457e3ef848   nginx:1.21.1   "/docker-entrypoint.…"   49 seconds ago   Up 49 seconds   0.0.0.0:80->80/tcp   old
    
    

    3)启动容器

    docker start [容器的名称|ID]
    
    [root@docker ~]# docker start nginx
    nginx
    
    

    4)停止容器

    docker stop [容器的名称|ID]
    
    [root@docker ~]# docker stop nginx
    nginx
    
    

    5)删除容器

    docker rm [容器的名称|ID]
    docker rm -f [容器的ID或名称]
    		
    #案例:
    删除所有容器:[root@docker ~]# docker rm -f $(docker ps -a -q)
    		
    

    6)查看容器详细状态

    docker inspect [容器的ID或名称]
    
    #案例:
    [root@docker ~]# docker inspect nginx
    [
        {
            "Id": "510bfd90e47641e930c16b2a069be333be9814f852b549ce0bf270e4e2f4071d",
            "Created": "2021-10-25T11:21:57.638679917Z",
            "Path": "/docker-entrypoint.sh",
            "Args": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "State": {
                "Status": "running",
                "Running": true,
                "Paused": false,
                "Restarting": false,
                "OOMKilled": false,
                "Dead": false,
                "Pid": 11103,
                "ExitCode": 0,
    -------- ------- -----------  省略  -  - ---------------------- -------------
    
    #要求判断指定容器是否正在运行?
    
    [root@docker ~]# docker inspect -f '{{ .State.Running }}' 510bfd90e476
    true
    
    

    7)复制容器中的内容

    1、将容器内的文件复制到宿主主机
    
    docker cp [容器的ID或名称]:[文件路径]  宿主主机路径
    
    [root@docker ~]# docker exec nginx ls /etc/nginx
    conf.d
    fastcgi_params
    mime.types
    modules
    nginx.conf
    scgi_params
    uwsgi_params
    
    [root@docker ~]# docker cp nginx:/etc/nginx/nginx.conf /root
    [root@docker ~]# ll
    total 12
    -rwxrwxr-x. 1 root root 1202 2021-10-12 10:03 docker-entrypoint.sh
    -rw-r--r--. 1 root root  497 2021-10-09 12:06 hostname_ip.sh
    -rw-r--r--. 1 root root  648 2021-09-07 23:38 nginx.conf
    
    
    2、将宿主主机上的内容复制到容器
    
    docker cp [宿主主机路径] [容器的ID]:[文件路径]
    
    [root@docker ~]# docker cp /root/hostname_ip.sh nginx:/root
    
    [root@docker ~]# docker exec nginx ls /root
    hostname_ip.sh
    
    

    8)进入容器

    1、attach
    attach是进入容器,原理是将容器内的PID为1的进程开辟一个管道,链接到宿主主机。
    当在宿主主机上退出时,容器也随即退出(结束了生命周期)
    
    
    2、exec(推荐)
    exec其本质是在宿主主机上执行一个容器内的命令,但是加上-it参数,可以达到进入容器的效果,其原理是在容器内部新创建一个bash进程。所以当exec退出时不影响容器的正常运行。
    
    #在宿主主机上执行一个容器内部的命令
    docker exec [容器的ID或名称] [需要在容器内部执行的命令]
    
    [root@docker ~]# docker exec nginx nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
    
    docker exec -it [容器的名称] [进入容器执行的命令]
    [root@docker ~]# docker exec -it nginx bash
    
    root@510bfd90e476:/# ls
    bin   docker-entrypoint.d   home   media  proc	sbin  tmp
    boot  docker-entrypoint.sh  lib    mnt	  root	srv   usr
    dev   etc		    lib64  opt	  run	sys   var
    
    			
    
    3、nsenter			
    nsenter的原理是建立一个管道进程,链接到容器的内部。
    nsenter --target $( docker inspect -f '{{.State.Pid }}'  ) --mount --uts --ipc --net --pid
    [root@docker ~]# nsenter --target $( docker inspect -f '{{.State.Pid }}' centos ) --mount --uts --ipc --net --pid
    
    
    4、ssh的方式(及其不推荐)
    
    

    9)使用 save 和 load保存镜像

    1、保存镜像(针对点是镜像)
    docker save # 将镜像保存成压缩包
    
    #参数:
    -o :输出到的文件
    [root@docker ~]# docker save nginx -o image.tar
    
    
    [root@docker ~]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
    nginx        latest    87a94228f133   13 days ago   133MB
    centos       latest    5d0da3dc9764   5 weeks ago   231MB
    
    # docker save [镜像名称|ID] > 压缩包名称
    [root@docker ~]# docker images
    REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
    
    [root@docker ~]# docker save nginx > image.tar
    [root@docker ~]# ll
    total 134332
    -rwxrwxr-x. 1 root root      1202 2021-10-12 10:03 docker-entrypoint.sh
    -rw-r--r--. 1 root root       497 2021-10-09 12:06 hostname_ip.sh
    -rw-r--r--. 1 root root 137544192 2021-10-25 19:40 image.tar
    
    #保存多个镜像
    # docker save [镜像名称|ID] [镜像名称|ID] > 压缩包名称
    [root@docker ~]# docker save nginx centos > docker.tar
    
    2. 导入镜像包针对与save使用
    # docker load # 将镜像包导入本地镜像中
    
    [root@docker ~]# docker load -i docker.tar
    [root@docker ~]# docker load < docker.tar
    
    

    10)使用 export 和 import

    1、保存容器为镜像(针对点是容器)
    docker export  #将容器打包成镜像
    #参数:
    -o :输出到的文件
    
    [root@docker ~]# docker export 2cea8378bc86 > nginx.tar
    [root@docker ~]# ll
    total 132600
    -rwxrwxr-x. 1 root root      1202 2021-10-12 10:03 docker-entrypoint.sh
    -rw-r--r--. 1 root root       497 2021-10-09 12:06 hostname_ip.sh
    -rw-r--r--. 1 root root 135770624 2021-10-25 20:06 nginx.tar
    
    
    2、docker import #将镜像包导入本地镜像中
     
    [root@docker ~]# docker import nginx.tar nginx:v1
    sha256:8048cab7ea12874922dcb063b55b4525eb51e71ed36aec1df679ba0849be21c2
    
    [root@docker ~]# docker images
    REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
    nginx        v1        8048cab7ea12   6 seconds ago   132MB
    nginx        latest    87a94228f133   13 days ago     133MB
    centos       latest    5d0da3dc9764   5 weeks ago     231MB
    
    

    11)将容器保存成本地镜像

    #主要格式:docker [container id] commit  [容器名称|ID] 
    #主要参数:
    -a	作者信息
    -m	提交信息
    -p	提交时,暂停容器运行
    
    docker commit 
    
    docker commit -a "ShanHe" -m "这是一个弟弟" -p  nginx   nginx:v2
    
    

    12)查看容器的运行日志

    docker logs [容器的ID或名称]
    #参数:
    -f : 持续监控
    
    [root@docker ~]# docker logs nginx 
    /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
    /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
    /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
    10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
    10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
    /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
    /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
    /docker-entrypoint.sh: Configuration complete; ready for start up
    2021/10/25 12:15:31 [notice] 1#1: using the "epoll" event method
    2021/10/25 12:15:31 [notice] 1#1: nginx/1.21.3
    2021/10/25 12:15:31 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 
    2021/10/25 12:15:31 [notice] 1#1: OS: Linux 3.10.0-1160.el7.x86_64
    2021/10/25 12:15:31 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
    2021/10/25 12:15:31 [notice] 1#1: start worker processes
    2021/10/25 12:15:31 [notice] 1#1: start worker process 30
    
    

    13)暂停容器和重新运行容器

    #容器将暂停服务
    	docker pause [容器名称|ID]
    	
    
    #重新运行容器,恢复提供和服务
    	docker unpause [容器名称|ID]
    	
    

    14)用容器搭建一个小游戏

    1、代码(宿主主机)
    [root@docker ~]# rz mario.zip
    [root@docker ~]# mkdir /code
    [root@docker ~]# mv mario.zip /code
    [root@docker ~]# cd /code
    [root@docker /code]# unzip mario.zip	
    [root@docker /code]# ll
    total 148
    drwxr-xr-x. 3 root root    118 2014-11-07 21:42 html5-mario
    -rw-r--r--. 1 root root 148142 2021-10-22 17:02 mario.zip
    
    2、将目录挂载到容器
    - v
    
    3、映射端口
    			
    [root@docker /code]# docker run -d --name mario -v /code/html5-mario:/usr/share/nginx/html -p 8080:80  nginx
    
    			
    4、浏览器浏览
    10.0.0.71:8080
    

    案例

  • 相关阅读:
    使用数据库时注意单引号、双引号和反引号的区别
    Mysql中的事件
    C#使用WindowsMediaPlayer实现视频播放
    Chart控件,鼠标选择区域,可以局部放大缩小
    C#自定义控件在添加引用后不显示在工具箱的解决方法(转)
    C# 中的#if、#elif、#else、#endif等条件编译符号
    若有派生类,则基类中的析构函数要用虚函数
    迭代器失效
    this指针
    VS调试快捷键
  • 原文地址:https://www.cnblogs.com/backz/p/15460562.html
Copyright © 2011-2022 走看看