• docker容器使用


    一、安装docker

    1、安装依赖环境

    [root@jenkins ~]# yum -y install yum-utils device-mapper-persistent-data  lvm2

    2、准备镜像源

    [root@jenkins ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo

    3、安装docker-ce

    [root@jenkins ~]# yum -y install docker-ce

    4.启动docker

    [root@docker ~]# systemctl start docker
    [root@docker ~]# systemctl enable docker
    [root@docker ~]# docker version                     #查看docker版本
    Client: Docker Engine - Community
     Version:           19.03.5
     API version:       1.40
     Go version:        go1.12.12
     Git commit:        633a0ea
     Built:             Wed Nov 13 07:25:41 2019
     OS/Arch:           linux/amd64
     Experimental:      false
    Server: Docker Engine - Community
     Engine:
      Version:          19.03.5
      API version:      1.40 (minimum version 1.12)
      Go version:       go1.12.12
      Git commit:       633a0ea
      Built:            Wed Nov 13 07:24:18 2019
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          1.2.10
      GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
     runc:
      Version:          1.0.0-rc8+dev
      GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
     docker-init:
      Version:          0.18.0
      GitCommit:        fec3683
    [root@docker ~]# docker info                #查看docker信息
    Client:
     Debug Mode: false
    Server:
     Containers: 0
      Running: 0
      Paused: 0
      Stopped: 0
     Images: 0
     Server Version: 19.03.5
     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: b34a5c8af56e510852c35414db4c1f4fa6172339
     runc version: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
     init version: fec3683
     Security Options:
      seccomp
       Profile: default
     Kernel Version: 3.10.0-1062.el7.x86_64
     Operating System: CentOS Linux 7 (Core)
     OSType: linux
     Architecture: x86_64
     CPUs: 1
     Total Memory: 972.4MiB
     Name: docker
     ID: MJYZ:MXJU:3YP2:MZMX:U2C4:XPYQ:WH4Q:75AW:YYDW:X72L:NGGB:VBQX
     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
     Live Restore Enabled: false
    5、阿里云镜像加速器
    https://account.aliyun.com         #阿里云网站

    针对Docker客户端版本大于 1.10.0 的用户

    您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器

    [root@docker ~]# cat <<EOF>/etc/docker/daemon.json
    {
      "registry-mirrors": ["https://你的阿里云镜像加速器.com"]
    }
    EOF
    [root@docker ~]# systemctl daemon-reload    #重新加载配置 
    [root@docker ~]# systemctl restart docker    #重启服务
    二、docker关于镜像的基本操作
    #查看docker镜像
    [root@docker ~]# docker search nginx
    -s指定好评数
    #下载镜像
    [root@docker ~]#docker pull nginx
    注:如果不加标签默认下载latest,最新的镜像
    #查看以下载的镜像
    [root@docker ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              4152a9608752        7 hours ago         126MB
    #查看镜像ID号
    [root@docker ~]# docker inspect nginx
    #更改镜像名称和标签名
    [root@docker ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    mynginx             zhu                 4152a9608752        7 hours ago         126MB
    nginx               latest              4152a9608752        7 hours ago         126MB
    #删除镜像
    [root@docker ~]# docker rmi mynginx:zhu
    Untagged: mynginx:zhu
    [root@docker ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              4152a9608752        7 hours ago         126MB
    #保存镜像到本地
    [root@docker ~]# docker save -o nginx.tar nginx:latest
    [root@docker ~]# ls
    anaconda-ks.cfg    nginx.tar            
    #将本地文件导入容器
    [root@docker ~]# docker rmi nginx:latest
    Untagged: nginx:latest
    Untagged: nginx@sha256:9916837e6b165e967e2beb5a586b1c980084d08eb3b3d7f79178a0c79426d880
    Deleted: sha256:4152a960875253728e0ba43da37d023e4626c43a268ca0a9c6083119542119fb
    Deleted: sha256:1cdfdca2c291c5d20901fcedf580cfb67252d515e4d8bcbd5c9f0d60d7a1ee10
    Deleted: sha256:dd9ce25e5c89862300ac75fc6cd2071c572aabeff7e4685238a1d41066af6500
    Deleted: sha256:b67d19e65ef653823ed62a5835399c610a40e8205c16f839c5cc567954fcf594
    [root@docker ~]# docker load < nginx.tar
    b67d19e65ef6: Loading layer   72.5MB/72.5MB
    de1b802e9897: Loading layer  57.67MB/57.67MB
    c2d3130eb3f6: Loading layer  3.584kB/3.584kB
    Loaded image: nginx:latest
    [root@docker ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              4152a9608752        7 hours ago         126MB
    #将镜像上传到仓库
    [root@docker ~]# docker push nginx:latest
    The push refers to repository [docker.io/library/nginx]
    三、docker关于容器的基本操作
    #创建容器
    [root@docker ~]# docker create -it nginx /bin/bash
    #查看开启的容器
    [root@docker ~]# docker ps
    #查看所有容器
    [root@docker ~]# docker ps -a
    #创建并启动容器
    [root@docker ~]# docker run -it -p 80:80 -v /html:/usr/share/nginx/html nginx /bin/bash
    root@3a529257d0ec:/# exit
    exit
    [root@docker ~]#
    exit:退出并关闭容器 
    Ctrl+p+q:只退出不关闭容器
     
    #启动容器
    [root@docker ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
    3a529257d0ec        nginx               "/bin/bash"         2 minutes ago       Exited (0) 2 minutes ago                       modest_spence
    01f1b4af534f        nginx               "/bin/bash"         10 minutes ago      Created                                        intelligent_ride
    [root@docker ~]# docker start 01f1b4af534f
    01f1b4af534f
    [root@docker ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
    3a529257d0ec        nginx               "/bin/bash"         4 minutes ago       Exited (0) 3 minutes ago                       modest_spence
    01f1b4af534f        nginx               "/bin/bash"         11 minutes ago      Up 2 seconds               80/tcp              intelligent_ride
    #关闭容器
    [root@docker ~]# docker stop 01f1b4af534f
    01f1b4af534f
    [root@docker ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
    3a529257d0ec        nginx               "/bin/bash"         5 minutes ago       Exited (0) 4 minutes ago                       modest_spence
    01f1b4af534f        nginx               "/bin/bash"         12 minutes ago      Exited (0) 2 seconds ago                       intelligent_ride
    #关闭容器
    [root@docker ~]# docker kill 01f1b4af534f
    01f1b4af534f
    [root@docker ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
    3a529257d0ec        nginx               "/bin/bash"         6 minutes ago       Exited (0) 6 minutes ago                        modest_spence
    01f1b4af534f        nginx               "/bin/bash"         14 minutes ago      Exited (137) 1 second ago                       intelligent_ride
    #重启容器
    [root@docker ~]# docker restart 01f1b4af534f
    01f1b4af534f
    [root@docker ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
    3a529257d0ec        nginx               "/bin/bash"         6 minutes ago       Exited (0) 5 minutes ago                       modest_spence
    01f1b4af534f        nginx               "/bin/bash"         13 minutes ago      Up 3 seconds               80/tcp              intelligent_ride
    #删除容器
    [root@docker ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
    3a529257d0ec        nginx               "/bin/bash"         7 minutes ago       Exited (0) 7 minutes ago                          modest_spence
    01f1b4af534f        nginx               "/bin/bash"         15 minutes ago      Exited (137) 58 seconds ago                       intelligent_ride
    [root@docker ~]# docker rm -f 01f1b4af534f
    01f1b4af534f
    [root@docker ~]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
    3a529257d0ec        nginx               "/bin/bash"         8 minutes ago       Exited (0) 8 minutes ago                       modest_spence
    #查看容器输出信息
    [root@docker ~]# docker logs 3a529257d0ec(容器ID)
    #切入到容器内部
    [root@docker ~]# docker restart 3a529257d0ec
    3a529257d0ec
    [root@docker ~]# docker attach 3a529257d0ec
    root@3a529257d0ec:/#
    [root@docker ~]# docker exec -it 3a529257d0ec /bin/bash
    root@3a529257d0ec:/#
    #导出容器
    [root@docker ~]# docker export 6e328d9c3383 > nginx:zhu
    [root@docker ~]# ls
    anaconda-ks.cfg   nginx:zhu  nginx.tar          
    #导入容器
    [root@docker ~]# cat nginx:zhu | docker import - nginx:zhu
    sha256:ca8e688f8cc83400216bd1be54d1ed3f60455016b6ce95b469ddcfbc9d3c5c6b
    [root@docker ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               zhu                 ca8e688f8cc8        11 seconds ago      125MB
    nginx               latest              4152a9608752        8 hours ago         126MB
    #将真机文件复制到docker容器中
    [root@docker ~]# touch 123.txt
    [root@docker ~]# docker cp 123.txt 6e328d9c3383:/web
    [root@docker ~]# docker attach 6e328d9c3383
     
  • 相关阅读:
    第二次作业
    第一次作业
    第三次作业
    第二次作业
    第一次作业
    第五次作业
    第四次作业
    第三次作业
    第二次作业
    第一次作业
  • 原文地址:https://www.cnblogs.com/ZCQ123456/p/11899106.html
Copyright © 2020-2023  润新知