初学docker容器

闲聊 闲聊 1565 人阅读 | 0 人回复

<
初学docker容器



docker是什么



  • docker是一种轻量级的虚拟机
  • 在linux容器中运行应用而且开源
docker与虚拟机的区别

差别点容器虚拟机启动速度上秒级分钟级运行性能上接近原生(直接运行在内核中的90%)50%左右的损失数量依据进程,可以有很多进程成百上千个一般几十台(操作体系级别)隔离进程级别体系级别(更彻底的隔离)磁盘占用MBGB(操作体系的镜像一般在几个G左右)操作体系主要支持linux几乎全部体系封装水平只打包项目代码和依赖关系,共享宿主机内核完备的操作体系,与宿主机互相隔离同时docker解决了vm的环境孤岛问题,docker可以自定义传递参数
docker利用场景



  • 用来打包应用程序简化部署
  • 脱离底层硬件任意迁移
  • 持续集成和持续交付(CI/CD):开发到测试发布
  • 部署微服务
  • 提供PAAS产物
docker的原理

cgroup资源控制与namespace名称空间结合控制管理6个名称空间资源实现完备隔离/完全隔离


  • mount :文件体系,挂载点
  • user :操作进程的用户和用户组
  • pid :进程编号
  • uts :主机名和主机域
  • ipc:信号量,消息队列,共享内存(差别的应用调用内存资源时利用差别的内存空间)
  • net:网络设备,网络协议栈,端口等
    该mount命名空间:管理文件体系挂载点
    该pid命名空间:进程隔离(pid:进程id)
    该uts命名空间:隔离内核和版本标识符(uts:Unix时间共享体系)
    该ipc命名空间:管理访问ipc资源(ipc:进程间互相通信)
    该net命名空间:管理网络接口(net:网络)
docker三个统一和docker三大组件

docker把容器化技术做成了标准化平台


  • docker引擎统一了基础设施环境—》docker环境
  • docker引擎统一了程序打包方式—》docker镜像
  • docker引擎统一了程序部署方式—》docker容器–》基于镜像,运行为容器(可运行的环境)
    实现了一次构建,多次,多处利用
    三大组件:
  • 镜像:作为模板,一组资源的集合,包含了应用程序软件包,应用程序相关的依赖包,运行应用程序所需要的基础环境
  • 容器:运行状态/运行时状态,基于镜像的一种运行时状态
  • 堆栈:存放镜像模板的地方,堆栈分类:公共堆栈–》docker hub,私有堆栈–》registry harbor
docker引擎

docker引擎具有以下主要组件的C/S应用程序(客户端-服务器)
server端:服务器是一种长期运行的程序,成为守护进程
client端:REST API指定程序可以用来与守护程序进行通信并指示其操作的接口
docker-server配置文件

在/etc/docker/daemon.json中配置
  1. {
  2. "graph":"/data/docker",                        //数据目录
  3. "storage-driver":"overlay2",                //存储引擎
  4. "insecure-registries" ["registry.access.redhat.com","quary.io"]        //私有仓库
  5. "registry-mirrors": ["https://cn90fxk6.mirror.aliyuncs.com"]        //镜像加速
  6. "bip":"172.17.0.1/24",                        //docker网络
  7. "exec-opts":["native.cgroupdriver=systemd"],                //启动时的额外参数
  8. "live-restore":true                        //当docker容器存储引擎挂了后,使用docker跑起来的容器还能运行
  9. }
复制代码
docker的部署(20版)



  • 安装依赖包
  1. [root@node1 nginx]# yum install -y yum-utils device-mapper-persistent-data lvm2
复制代码


  • 设置阿里云镜像源
  1. [root@node1 yum.repos.d]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
复制代码


  • 安装docker-ce社区版
  1. [root@node1 yum.repos.d]# yum install -y docker-ce
复制代码


  • 然后启动docker
  1. [root@node1 yum.repos.d]# systemctl enable docker
  2. [root@node1 yum.repos.d]# systemctl start docker
复制代码


  • 设置镜像加快,去阿里云的镜像加快分类
    初学docker容器 145722p678r76om6bir7eu.jpg

  • 将底下的代码直接复制到命令行就可以了,要选对应的操作体系
    初学docker容器 145722i0scc7sm7vc0hrhb.jpg

  • 查看是否设置成功
  1. [root@node1 yum.repos.d]# vim /etc/docker/daemon.json
  2. {
  3.   "registry-mirrors": ["https://cn90fxk6.mirror.aliyuncs.com"]
  4. }
复制代码
docker镜像操作

  1. [root@node1 yum.repos.d]# docker run hello-world
  2. Hello from Docker!
  3. This message shows that your installation appears to be working correctly.
  4. To generate this message, Docker took the following steps:
  5. 1. The Docker client contacted the Docker daemon.                //客户端连接到了服务器
  6. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.                        //由服务端的守护进程从docker hub上下载了镜像
  7.     (amd64)
  8. 3. The Docker daemon created a new container from that image which runs the
  9.     executable that produces the output you are currently reading.                //服务端创建了一个新容器,然后从这个镜像启动了一个容器,容器执行了脚本
  10. 4. The Docker daemon streamed that output to the Docker client, which sent it
  11.     to your terminal.                //服务端把这些信息流返回到客户端展示出来
  12. To try something more ambitious, you can run an Ubuntu container with:
  13. $ docker run -it ubuntu bash
  14. Share images, automate workflows, and more with a free Docker ID:
  15. https://hub.docker.com/
  16. For more examples and ideas, visit:
  17. https://docs.docker.com/get-started/
复制代码


  • 查询docker版本
  1. [root@node1 yum.repos.d]# docker version
  2. Client: Docker Engine - Community
  3. Version:           20.10.8
  4. API version:       1.41
  5. Go version:        go1.16.6
  6. Git commit:        3967b7d
  7. Built:             Fri Jul 30 19:55:49 2021
  8. OS/Arch:           linux/amd64
  9. Context:           default
  10. Experimental:      true
  11. Server: Docker Engine - Community
  12. Engine:
  13.   Version:          20.10.8
  14.   API version:      1.41 (minimum version 1.12)
  15.   Go version:       go1.16.6
  16.   Git commit:       75249d8
  17.   Built:            Fri Jul 30 19:54:13 2021
  18.   OS/Arch:          linux/amd64
  19.   Experimental:     false
  20. containerd:
  21.   Version:          1.4.9
  22.   GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
  23. runc:
  24.   Version:          1.0.1
  25.   GitCommit:        v1.0.1-0-g4144b63
  26. docker-init:
  27.   Version:          0.19.0
  28.   GitCommit:        de40ad0
  29. [root@node1 yum.repos.d]# docker info
  30. Client:
  31. Context:    default
  32. Debug Mode: false
  33. Plugins:
  34.   app: Docker App (Docker Inc., v0.9.1-beta3)
  35.   buildx: Build with BuildKit (Docker Inc., v0.6.1-docker)
  36.   scan: Docker Scan (Docker Inc., v0.8.0)
  37. Server:
  38. Containers: 14
  39.   Running: 5
  40.   Paused: 0
  41.   Stopped: 9
  42. Images: 63
  43. Server Version: 20.10.8
  44. Storage Driver: overlay2
  45.   Backing Filesystem: xfs
  46.   Supports d_type: true
  47.   Native Overlay Diff: true
  48.   userxattr: false
  49. Logging Driver: json-file
  50. Cgroup Driver: cgroupfs
  51. Cgroup Version: 1
  52. Plugins:
  53.   Volume: local
  54.   Network: bridge host ipvlan macvlan null overlay
  55.   Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
  56. Swarm: inactive
  57. Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
  58. Default Runtime: runc
  59. Init Binary: docker-init
  60. containerd version: e25210fe30a0a703442421b0f60afac609f950a3
  61. runc version: v1.0.1-0-g4144b63
  62. init version: de40ad0
  63. Security Options:
  64.   seccomp
  65.    Profile: default
  66. Kernel Version: 3.10.0-957.el7.x86_64
  67. Operating System: CentOS Linux 7 (Core)
  68. OSType: linux
  69. Architecture: x86_64
  70. CPUs: 4
  71. Total Memory: 5.712GiB
  72. Name: node1
  73. ID: QL6Y:HC6L:E57G:UWHJ:E7FY:J47A:YF6Z:GLL2:DETH:DY4C:STNH:ZGFS
  74. Docker Root Dir: /var/lib/docker
  75. Debug Mode: false
  76. Registry: https://index.docker.io/v1/
  77. Labels:
  78. Experimental: false
  79. Insecure Registries:
  80.   127.0.0.0/8
  81. Registry Mirrors:
  82.   https://cn90fxk6.mirror.aliyuncs.com/
  83. Live Restore Enabled: false
  84. WARNING: bridge-nf-call-iptables is disabled
  85. WARNING: bridge-nf-call-ip6tables is disabled
复制代码


  • 搜索镜像
  1. [root@node1 yum.repos.d]# docker search nginx
  2. NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
  3. nginx                             Official build of Nginx.                        15420     [OK]      
  4. jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2063                 [OK]
  5. richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   816                  [OK]
  6. jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   240                  
  7. linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   152                  
  8. tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   141                  [OK]
  9. jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        135                  [OK]
  10. alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   106                  [OK]
  11. jasonrivers/nginx-rtmp            Docker images to host RTMP streams using NGI…   92                   [OK]
  12. nginxdemos/hello                  NGINX webserver that serves a simple page co…   72                   [OK]
  13. privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   56                   [OK]
  14. nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   55                  
  15. nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  47                  
  16. staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   24                   [OK]
  17. nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con…   20                  
  18. schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                   [OK]
  19. nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   19                  
  20. centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   15                  
  21. centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13                  
  22. bitwarden/nginx                   The Bitwarden nginx web server acting as a r…   11                  
  23. flashspys/nginx-static            Super Lightweight Nginx Image                   10                   [OK]
  24. mailu/nginx                       Mailu nginx frontend                            9                    [OK]
  25. sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   7                    [OK]
  26. ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          2                    [OK]
  27. wodby/nginx                       Generic nginx                                   1     
  28. [root@node1 yum.repos.d]# docker search centos:7
  29. NAME                                                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
  30. benwang6/tedu-jdk                                   oracle jdk 8u281 centos:7 JAVA_HOME=/usr/jdk…   5                    
  31. vikingco/python                                     Python Stack Docker Base Image: Based on cen…   1                    
  32. sndnvaps/docker-golang                              build latest golang in centos:7                 1                    [OK]
  33. legerete/nginx-php71                                LA[->]P - Centos:7 + Nginx + PHP 7.1            1                    [OK]
  34. peltikalle/basepython                               Base image with Centos:7 and Python 3.5.2       1                    [OK]
  35. mjstealey/mariadb-galera                            MariaDB Galera cluster in Docker - based fro…   1                    [OK]
  36. acktsw/java                                         oracle jdk 8u171 , centos:7,  timeZone:+8, e…   0                    [OK]
  37. macedigital/nodejs                                  Latest NodeJS for CentOS:7                      0                    [OK]
  38. grossws/nginx                                       nginx (mainline) on grossws/centos:7            0                    [OK]
  39. europeanspallationsource/oracle-jdk-maven-jenkins   ICS oracle-jdk + maven + jenkins users image…   0                    
  40. pbieberstein/acic-findr                             CentOS:7 with dependencies to run &#39;Findr&#39; (h…   0                    [OK]
  41. sjoeboo/rbenv                                       Simple base container from CentOS:7 w/ rbenv…   0                    [OK]
  42. alvintz/centos                                      centos:7.2.1511                                 0                    [OK]
  43. geomatikk/centos                                    FROM centos:7 with maven 3.6.1 and openjdk-1…   0                    
  44. waffleimage/centos7                                 Centos:7 with systemd and ssh running           0                    
  45. cristo/netacuity                                    Docker image on Centos:7 to run NetAcuity       0                    [OK]
  46. badwolf/centos                                      from official centos:7 add gcc,gcc++,make,vi    0                    [OK]
  47. mesosphere/freeipa-server                           A freeIPA v4.3 container based on centos:7. …   0                    
  48. acktsw/centos                                       centos:7                                        0                    [OK]
  49. bbania/centos                                       Build image based on centos:7                   0                    
  50. a2747/centos7                                       derivative images from centos:7                 0                    
  51. 21plus2/server-jre                                  Dockerimage base on centos:7 with server-jre    0                    [OK]
  52. europeanspallationsource/oracle-jdk-maven           ICS oracle-jdk + maven image based on centos…   0                    
  53. qiyue/mycat                                         centos:7 + jdk:1.8 + mycat                      0                    
  54. weihoop/mysql                                       基于weihoop/centos:7.4.1708制作         
复制代码


  • 下载镜像
  1. [root@node1 yum.repos.d]# docker pull nginx
  2. Using default tag: latest
  3. latest: Pulling from library/nginx
  4. a330b6cecb98: Pull complete
  5. 5ef80e6f29b5: Pull complete
  6. f699b0db74e3: Pull complete
  7. 0f701a34c55e: Pull complete
  8. 3229dce7b89c: Pull complete
  9. ddb78cb2d047: Pull complete
  10. Digest: sha256:a05b0cdd4fc1be3b224ba9662ebdf98fe44c09c0c9215b45f84344c12867002e
  11. Status: Downloaded newer image for nginx:latest
  12. docker.io/library/nginx:latest
  13. [root@node1 yum.repos.d]# docker images                        //查看镜像列表
  14. REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
  15. nginx         latest    822b7ec2aaf2   2 days ago     133MB
复制代码


  • 获取镜像信息
  1. [root@node1 yum.repos.d]# docker inspect 822b7ec2aaf2
  2. [
  3.     {
  4.         "Id": "sha256:822b7ec2aaf2122b8f80f9c7f45ca62ea3379bf33af4e042b67aafbf6eac1941",
  5.         "RepoTags": [
  6.             "nginx:latest"
  7.         ],
  8.         "RepoDigests": [
  9.             "nginx@sha256:a05b0cdd4fc1be3b224ba9662ebdf98fe44c09c0c9215b45f84344c12867002e"
  10.         ],
  11.         "Parent": "",
  12.         "Comment": "",
  13.         "Created": "2021-09-03T07:40:16.355730864Z",
  14.         "Container": "367d32086ac12447d36e75c9b7acbe1b5156a34a91370b9200e68783be75506c",
  15.         "ContainerConfig": {
  16.             "Hostname": "367d32086ac1",
  17.             "Domainname": "",
  18.             "User": "",
  19.             "AttachStdin": false,
  20.             "AttachStdout": false,
  21.             "AttachStderr": false,
  22.             "ExposedPorts": {
  23.                 "80/tcp": {}
  24.             },
  25.             "Tty": false,
  26.             "OpenStdin": false,
  27.             "StdinOnce": false,
  28.             "Env": [
  29.                 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  30.                 "NGINX_VERSION=1.21.1",
  31.                 "NJS_VERSION=0.6.1",
  32.                 "PKG_RELEASE=1~buster"
  33.             ],
  34.             "Cmd": [
  35.                 "/bin/sh",
  36.                 "-c",
  37.                 "#(nop) ",
  38.                 "CMD ["nginx" "-g" "daemon off;"]"
  39.             ],
  40.             "Image": "sha256:d4315787e4fec867791beba140dd0e44f657cb6e4a9d75c676c7946089c20da9",
  41.             "Volumes": null,
  42.             "WorkingDir": "",
  43.             "Entrypoint": [
  44.                 "/docker-entrypoint.sh"
  45.             ],
复制代码


  • 添加镜像标签
  1. [root@node1 yum.repos.d]# docker images
  2. REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
  3. nginx         123       822b7ec2aaf2   2 days ago     133MB
  4. nginx         latest    822b7ec2aaf2   2 days ago     133MB
复制代码


  • 删除镜像
  1. [root@node1 yum.repos.d]# docker rmi nginx:123
  2. Untagged: nginx:123
  3. [root@node1 yum.repos.d]# docker images
  4. REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
  5. nginx         latest    822b7ec2aaf2   2 days ago     133MB
复制代码


  • 导出镜像
  1. [root@node1 ~]# docker save -o nginx_images nginx:latest
  2. [root@node1 ~]# ls
  3. 12   1.sh   2333  45               apps         initial-setup-ks.cfg       ks.cfg  nginx_images  模板  图片  下载  桌面
  4. 123  1.txt  234   anaconda-ks.cfg  docker_home  jdk-8u91-linux-x64.tar.gz  nginx   公共          视频  文档  音乐
复制代码


  • 导入镜像,将原有的先删除再导入
  1. [root@node1 ~]# docker load < nginx_images
  2. d000633a5681: Loading layer [==================================================>]  72.53MB/72.53MB
  3. 63b5f2c0d071: Loading layer [==================================================>]  64.86MB/64.86MB
  4. 875b5b50454b: Loading layer [==================================================>]  3.072kB/3.072kB
  5. ed94af62a494: Loading layer [==================================================>]  4.096kB/4.096kB
  6. 8e58314e4a4f: Loading layer [==================================================>]  3.584kB/3.584kB
  7. d47e4d19ddec: Loading layer [==================================================>]  7.168kB/7.168kB
  8. Loaded image: nginx:latest
  9. [root@node1 ~]# docker images
  10. REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
  11. nginx         latest    822b7ec2aaf2   2 days ago     133MB
复制代码


  • 查询容器
  1. [root@node1 ~]# docker ps -a
  2. CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS       PORTS                                         NAMES
复制代码


  • 创建容器
  1. [root@node1 ~]# docker create -it nginx:latest /bin/bash
  2. 9eade02412f5ecc3e9e2006de2f59845ca50ed4a52741ad9f0a8fb43ce5086f3
  3. [root@node1 ~]# docker ps -a
  4. CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS       PORTS                                         NAMES
  5. 9eade02412f5   nginx:latest   "/docker-entrypoint.…"   3 seconds ago   Created                                                    keen_chatterjee
  6. //-i是让容器的标准输入保持打开
  7. //-t是分配一个伪终端
  8. //-d是后台守护进程的方式运行
复制代码


  • 启动容器
  1. [root@node1 ~]# docker start 9eade02412f5
  2. 9eade02412f5
  3. [root@node1 ~]# docker ps -a
  4. CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS         PORTS                                         NAMES
  5. 9eade02412f5   nginx:latest   "/docker-entrypoint.…"   About a minute ago   Up 2 seconds   80/tcp                                        keen_chatterjee
复制代码


  • 一次性执行来启动容器
  1. [root@node1 ~]# docker run centos:7 /usr/bin/bash -c ls /
  2. anaconda-post.log
  3. bin
  4. dev
  5. etc
  6. home
  7. lib
  8. lib64
  9. media
  10. mnt
  11. opt
  12. proc
  13. root
  14. run
  15. sbin
  16. srv
  17. sys
  18. tmp
  19. usr
  20. var
复制代码


  • 停止容器
  1. [root@node1 ~]# docker ps -a
  2. CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                       PORTS                                         NAMES
  3. 9eade02412f5   nginx:latest   "/docker-entrypoint.…"   4 minutes ago    Exited (137) 6 seconds ago                         //非0状态值都是停止状态,137是stop命令停止的指数                                        keen_chatterjee
复制代码


  • 进入容器,两种方式都可以,但是exec需要在容器运行时才能进入
  1. [root@node1 ~]# docker run -it nginx:latest /bin/bash
  2. root@d36a26b3e1d2:/#
  3. [root@node1 ~]# docker exec -it 9eade02412f5 /bin/bash
  4. root@9eade02412f5:/#
  5. docker run -it 会创建前台进程,但是会在输入exit后终止进程
  6. docker exec -it 会连接到容器,可以像ssh一样进入容器内部,进行操作,可以通过exit退出
复制代码


  • 容器导出
  1. [root@node1 ~]# docker export 9eade02412f5 > nginx_1
  2. [root@node1 ~]# ls
  3. 12   1.sh   2333  45               apps         initial-setup-ks.cfg       ks.cfg  nginx_1       公共  视频  文档  音乐
复制代码


  • 容器导入(生成镜像)
  1. [root@node1 ~]# cat nginx_1 | docker import - nginx:latest
  2. sha256:ae834c84afd17bb12708bd2dd4d53e8432c43d6378d0ed405e1fd580dd6f77ad
复制代码


  • 删除容器
  1. [root@node1 ~]# docker ps -a
  2. CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                     PORTS                                         NAMES
  3. 95b40e409895   nginx:latest   "/bin/bash"              9 seconds ago   Exited (0) 4 seconds ago                                                 sad_heyrovsky
  4. [root@node1 ~]# docker rm 95b40e409895
  5. 95b40e409895
  6. 强制删除正在运行中的容器可以加一个-f
复制代码


  • 批量删除容器
  1. [root@node1 ~]# docker ps -a | awk &#39;{print "docker rm "$1}&#39; | bash
  2. 3e0845eacc5c
  3. 5e0ecd151d57
  4. b8da82b16ba7
  5. 2930dd871f01
  6. 668103e78d6c
  7. 或者
  8. [root@node1 ~]# docker rm `docker ps -qa`
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
回复 关闭延时

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则