docker

第一问:什么是容器

容器就是在一个隔离的环境中运行的一个进程。注意关键词,隔离和进程。如果进程停止,那么容器就销毁。因为具有隔离的特点,所以每个容器都拥有自己的文件系统:包括IP地址、主机名等。什么是进程呢?简单理解就是一个程序。一条命令就是一个程序,比如ls,df -h

既然是隔离的环境,那docker容器和虚拟化有啥区别呢??

第二问:容器和虚拟化的区别

Linux容器技术,容器虚拟化和KVM虚拟化的区别

KVM虚拟化:需要硬件的支持,需要模拟硬件(qemu),可以运行不同的操作系统,启动时间分钟级,需要一个正常的开机流程(1、BIOS开机硬件自检;2、根据BIOS设置的开机启动顺序;3、读取MBR引导(分区、内核)。。。。)

容器:容器属于Linux内核的一个技术,不需要硬件支持,公用宿主机内核,所以容器内的系统只能是Linux,启动时间秒级(公用宿主机的内核,不用前面BIOS自检的过程)

容器和虚拟化的优缺点总结:

容器:性能好,轻量化,启动快,只能运行在Linux上;

虚拟机:性能损耗多,启动慢,能够运行多个操作系统平台;

3、容器的发展

其实容器的早期雏形是chroot技术,新建一个子系统,改变根目录来运行多个系统,并实现一定程度隔离;

Linux container(LXC)

然后发展出了Linux container(LXC),Lxc是最接近虚拟机的容器技术,基于ubantu,对centos的支持不是特别友好:

1、拥有独立的namespace命名空间,可提供隔离环境;

2、cgroup,用来用限制一个进程能使用的系统资源或计算资源;

LXC容器创建过程:

1、修改base yum源,wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

2、添加epel源,wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

3、安装LXC需要的软件,yum install lxc-* -y && yum install libcgroup* -y && yum install bridge-utils.x86_64 -y 因为是centos系统,所以后面两个包是需要安装的;

4、创建桥接网卡,这是因为在lxc的初始配置文件中要求的网卡信息有规定:

  1. [root@LXC-10 yum.repos.d]# cat /etc/lxc/default.conf
  2. lxc.network.type = veth
  3. lxc.network.link = virbr0
  4. lxc.network.flags = up
  5. [root@LXC-10 yum.repos.d]#

其实在ubantu系统里面,安装完LXC后,自己就会建立相应的桥接网卡,只不过在centos系统中,需要自己去创建

  1. echo 'TYPE=Ethernet
  2. BOOTPROTO=none
  3. NAME=ens32
  4. DEVICE=ens32
  5. ONBOOT=yes
  6. BRIDGE=virbr0' > /etc/sysconfig/network-scripts/ifcfg-ens32
  7.  
  8. echo 'TYPE=Bridge
  9. BOOTPROTO=none
  10. NAME=virbr0
  11. DEVICE=virbr0
  12. ONBOOT=yes
  13. IPADDR=10.0.0.16
  14. NETMASK=255.255.255.0
  15. GATEWAY=10.0.0.2
  16. DNS=114.114.114.114' >/etc/sysconfig/network-scripts/ifcfg-virbr0

5、启动cgroup、lxc服务,systemctl start cgconfig.service && systemctl start lxc.service && systemctl enable cgconfig.service && systemctl enable lxc.service

6、创建LXC容器,可参见https://mirror.tuna.tsinghua.edu.cn/help/lxc-images/,lxc-create -t download -n my-container -- --server mirrors.tuna.tsinghua.edu.cn/lxc-images -d centos -r 6 -a amd64这几个参数用来指定发行版、版本号和架构三个参数

Downloading the image index   会在/tmp下生成临时目录

  1. [root@LXC-10 ~]# cd /tmp/
  2. [root@LXC-10 tmp]# ll
  3. total 0
  4. drwx------. 2 root root 24 Sep 6 05:06 ssh-3rTdYpl9wI
  5. drwx------. 3 root root 17 Sep 6 06:29 systemd-private-f3a031d2b1c24651a394c24d0e6fae8c-vmtoolsd.service-nKQxYw
  6. drwx------. 3 root root 47 Sep 6 06:59 tmp.MDcEY1DJq4
  7. [root@LXC-10 tmp]# cd tmp.MDcEY1DJq4/
  8. [root@LXC-10 tmp.MDcEY1DJq4]# ll
  9. total 32
  10. drwx------. 2 root root 83 Sep 6 06:59 gpg
  11. -rw-r--r--. 1 root root 28139 Aug 26 18:16 index
  12. -rw-r--r--. 1 root root 833 Aug 26 18:16 index.asc

Downloading the rootfs 下载文件系统,并保存在临时目录下

  1. [root@LXC-10 tmp.MDcEY1DJq4]# ll
  2. total 44
  3. drwx------. 2 root root 83 Sep 6 07:06 gpg
  4. -rw-r--r--. 1 root root 28139 Aug 26 18:16 index
  5. -rw-r--r--. 1 root root 833 Aug 26 18:16 index.asc
  6. -rw-r--r--. 1 root root 924 Aug 25 15:30 meta.tar.xz
  7. -rw-r--r--. 1 root root 833 Aug 25 15:30 meta.tar.xz.asc
  8. -rw-r--r--. 1 root root 833 Aug 25 15:30 rootfs.tar.xz.asc

Downloading the metadata
The image cache is now ready
Unpacking the rootfs             #解压,缓存在缓存目录中 /var/cashe,解压后存放在/var/lib/lxc

---
You just created a Centos 6 x86_64 (20200825_07:08) container.

下载完成后就会删除/tmp下的目录

  1. [root@LXC-10 default]# pwd
    /var/cache/lxc/download/centos/6/amd64/default
    [root@LXC-10 default]# ls
    build_id config.2 config-user config-user.3 excludes-user templates
    config config.3 config-user.1 config-user.4 expiry
    config.1 config.4 config-user.2 create-message rootfs.tar.xz
  1. [root@LXC-10 rootfs]# pwd
  2. /var/lib/lxc/my-container/rootfs
  3. [root@LXC-10 rootfs]# ls
  4. bin dev home lib64 mnt proc run selinux sys usr
  5. boot etc lib media opt root sbin srv tmp var
  6. [root@LXC-10 rootfs]#
  1. [root@LXC-10 tmp]# ll
  2. total 0
  3. drwx------. 2 root root 24 Sep 6 05:06 ssh-3rTdYpl9wI
  4. drwx------. 3 root root 17 Sep 6 06:29 systemd-private-f3a031d2b1c24651a394c24d0e6fae8c-vmtoolsd.service-nKQxYw
  5. [root@LXC-10 tmp]#

这样一个容器就创建完成了!

常用命令可通过table补全

  1. [root@LXC-10 yum.repos.d]# lxc-
  2. lxc-attach lxc-config lxc-execute lxc-snapshot lxc-unfreeze
  3. lxc-autostart lxc-console lxc-freeze lxc-start lxc-unshare
  4. lxc-cgroup lxc-create lxc-info lxc-start-ephemeral lxc-usernsexec
  5. lxc-checkconfig lxc-destroy lxc-ls lxc-stop lxc-wait
  6. lxc-clone lxc-device lxc-monitor lxc-top
  7. [root@LXC-10 yum.repos.d]# lxc-ls
  8. centos7 my-container
  9. [root@LXC-10 yum.repos.d]# lxc-ls --help
  10. usage: lxc-ls [-h] [-1] [-P PATH] [--active] [--frozen] [--running]
  11. [--stopped] [-f] [-F FANCY_FORMAT] [--nesting] [--version]
  12. [FILTER]
  13.  
  14. LXC: List containers

由于lxc容器连用户名和密码都没配置,需要手动进行配置

  1. [root@LXC-10 ~]# cd /var/lib/lxc/centos7/
  2. [root@LXC-10 centos7]# ll
  3. total 4
  4. -rw-r--r--. 1 root root 560 Sep 5 23:26 config
  5. drwxr-xr-x. 18 root root 259 Sep 5 23:37 rootfs
  6. lrwxrwxrwx. 1 root root 34 Sep 5 23:37 rootfs.dev -> /dev/.lxc/centos7.f29e3af285394b5f
  7. [root@LXC-10 centos7]# cd rootfs
  8. [root@LXC-10 rootfs]# chroot . passwd
  9. Changing password for user root.
  10. New password:
  11. BAD PASSWORD: The password is shorter than 8 characters
  12. Retype new password:
  13. Sorry, passwords do not match.
  14. New password:
  15. BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
  16. Retype new password:
  17. passwd: all authentication tokens updated successfully.

  1. [root@LXC-10 ~]# lxc-start -n centos7
  2. systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN)
  3. Detected virtualization lxc.
  4. Detected architecture x86-64.
  5.  
  6. Welcome to CentOS Linux 7 (Core)!
  7.  
  8. Cannot add dependency job for unit display-manager.service, ignoring: Unit not found.
  9. [ OK ] Reached target Remote File Systems.
  10. [ OK ] Created slice Root Slice.
  11. [ OK ] Created slice System Slice.
  12. [ OK ] Listening on /dev/initctl Compatibility Named Pipe.
  13. [ OK ] Listening on Delayed Shutdown Socket.
  14. [ OK ] Started Forward Password Requests to Wall Directory Watch.
  15. [ OK ] Started Dispatch Password Requests to Console Directory Watch.
  16. [ OK ] Reached target Paths.
  17. [ OK ] Reached target Swap.
  18. [ OK ] Reached target Local Encrypted Volumes.
  19. [ OK ] Created slice User and Session Slice.
  20. [ OK ] Reached target Slices.
  21. [ OK ] Created slice system-getty.slice.
  22. [ OK ] Listening on Journal Socket.
  23. [ OK ] Reached target Local File Systems (Pre).
  24. Starting Configure read-only root support...
  25. Starting Read and set NIS domainname from /etc/sysconfig/network...
  26. Starting Journal Service...
  27. Mounting Huge Pages File System...
  28. Mounting POSIX Message Queue File System...
  29. [ OK ] Mounted POSIX Message Queue File System.
  30. [ OK ] Started Read and set NIS domainname from /etc/sysconfig/network.
  31. [ OK ] Mounted Huge Pages File System.
  32. [ OK ] Started Journal Service.
  33. Starting Flush Journal to Persistent Storage...
  34. [ OK ] Started Configure read-only root support.
  35. Starting Load/Save Random Seed...
  36. [ OK ] Reached target Local File Systems.
  37. [ OK ] Started Load/Save Random Seed.
  38. <46>systemd-journald[16]: Received request to flush runtime journal from PID 1
  39. [ OK ] Started Flush Journal to Persistent Storage.
  40. Starting Create Volatile Files and Directories...
  41. [ OK ] Started Create Volatile Files and Directories.
  42. Starting Update UTMP about System Boot/Shutdown...
  43. [ OK ] Started Update UTMP about System Boot/Shutdown.
  44. [ OK ] Reached target System Initialization.
  45. [ OK ] Listening on D-Bus System Message Bus Socket.
  46. [ OK ] Reached target Sockets.
  47. [ OK ] Started Daily Cleanup of Temporary Directories.
  48. [ OK ] Reached target Timers.
  49. [ OK ] Reached target Basic System.
  50. [ OK ] Started D-Bus System Message Bus.
  51. Starting Turn off network device...
  52. Starting LSB: Bring up/down networking...
  53. Starting Permit User Sessions...
  54. Starting Login Service...
  55. Starting Cleanup of Temporary Directories...
  56. [ OK ] Started Turn off network device.
  57. [ OK ] Started Permit User Sessions.
  58. [ OK ] Started Cleanup of Temporary Directories.
  59. [ OK ] Started Console Getty.
  60. [ OK ] Reached target Login Prompts.
  61. [ OK ] Started Command Scheduler.
  62. [ OK ] Started Login Service.
  63.  
  64. CentOS Linux 7 (Core)
  65. Kernel 3.10.0-514.el7.x86_64 on an x86_64
  66.  
  67. centos7 login:

docker是个啥?的更多相关文章

  1. docker——容器安装tomcat

    写在前面: 继续docker的学习,学习了docker的基本常用命令之后,我在docker上安装jdk,tomcat两个基本的java web工具,这里对操作流程记录一下. 软件准备: 1.jdk-7 ...

  2. Docker笔记一:基于Docker容器构建并运行 nginx + php + mysql ( mariadb ) 服务环境

    首先为什么要自己编写Dockerfile来构建 nginx.php.mariadb这三个镜像呢?一是希望更深入了解Dockerfile的使用,也就能初步了解docker镜像是如何被构建的:二是希望将来 ...

  3. Docker 第一篇--初识docker

    已经多年不写博客, 看完<晓松奇谈>最后一期猛然觉醒, 决定仔细梳理下自己这几年的知识脉络. 既然决定写, 那么首先就从最近2年热门的开源项目Docker开始.Docker 这两年在国内很 ...

  4. 在docker中运行ASP.NET Core Web API应用程序(附AWS Windows Server 2016 widt Container实战案例)

    环境准备 1.亚马逊EC2 Windows Server 2016 with Container 2.Visual Studio 2015 Enterprise(Profresianal要装Updat ...

  5. docker for mac 学习记录

    docker基本命令 docker run -d -p 80:80 --name webserver nginx 运行容器并起别名 docker ps 展示目前启动的容器 docker ps -a 展 ...

  6. scrapy爬虫docker部署

    spider_docker 接我上篇博客,为爬虫引用创建container,包括的模块:scrapy, mongo, celery, rabbitmq,连接https://github.com/Liu ...

  7. [原][Docker]特性与原理解析

    Docker特性与原理解析 文章假设你已经熟悉了Docker的基本命令和基本知识 首先看看Docker提供了哪些特性: 交互式Shell:Docker可以分配一个虚拟终端并关联到任何容器的标准输入上, ...

  8. 开发者的利器:Docker 理解与使用

    困扰写代码的机器难免会被我们安装上各种各样的开发工具.语言运行环境和引用库等一大堆的东西,长久以来不仅机器乱七八糟,而且有些相同的软件还有可能会安装不同的版本,这样又会导致一个项目正常运行了,却不小心 ...

  9. 使用python自动生成docker nginx反向代理配置

    由于在测试环境上用docker部署了多个应用,而且他们的端口有的相同,有的又不相同,数量也比较多,在使用jenkins发版本的时候,不好配置,于是想要写一个脚本,能在docker 容器创建.停止的时候 ...

  10. 微服务与Docker介绍

    什么是微服务 微服务应用的一个最大的优点是,它们往往比传统的应用程序更有效地利用计算资源.这是因为它们通过扩展组件来处理功能瓶颈问题.这样一来,开发人员只需要为额外的组件部署计算资源,而不需要部署一个 ...

随机推荐

  1. SpringBoot项目整合Retrofit最佳实践,这才是最优雅的HTTP客户端工具!

    大家都知道okhttp是一款由square公司开源的java版本http客户端工具.实际上,square公司还开源了基于okhttp进一步封装的retrofit工具,用来支持通过接口的方式发起http ...

  2. redis入门指南(七)—— 安全、协议、管理工具及命令属性

    写在前面 学习<redis入门指南>笔记,结合实践,只记录重要,明确,属于新知的相关内容. 安全 1.可以使用bind参数绑定一个地址,使redis只接受这个地址的连接. 2.使用requ ...

  3. 【转】Postgres SQL sort 操作性能调优

    这篇文章将以实战的方式结合笔者在项目中真实遇到的情况来讲解.说到SQL,大家可能会遇到一些写法稍微复杂的写法.比如SQL中遇到的有聚合函数sum等,也有遇到使用group by / order by的 ...

  4. MySQL必知必会(1-12章)

    第一章:了解SQL 数据库基础:(概念) 数据库软件: DBMS(数据库管理系统) 数据库: 通过DBMS创建和操纵的容器: 保存有组织的数据的容器-->通常是一个文件或者一组文件: 表: 某种 ...

  5. AOP参数校验

      1.切面依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  6. 【Processing-日常3】等待动画1

    之前在CSDN上发表过: https://blog.csdn.net/fddxsyf123/article/details/79755976

  7. (转)HttpServletResquest对象

    HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息. 1 ...

  8. Helm部署和体验jenkins

    如何快速且简单的部署 通过helm可以快速且简单的部署多种应用,关于helm的安装和使用请参考 环境信息 本次实战的环境信息如下: kubernetes集群:三台CentOS7.7服务器 kubern ...

  9. 探讨JVM运行机制和执行流程

    JVM是什么 概述 JVM是Java Virtual Machine的缩写.它是一种基于计算设备的规范,是一台虚拟机,即虚构的计算机. JVM屏蔽了具体操作系统平台的信息(显然,就像是我们在电脑上开了 ...

  10. 免费开源工作流Smartflow-Sharp v2.0

    @font-face { font-family: 宋体 } @font-face { font-family: "Cambria Math" } @font-face { fon ...