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的初始配置文件中要求的网卡信息有规定:

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

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

echo 'TYPE=Ethernet
BOOTPROTO=none
NAME=ens32
DEVICE=ens32
ONBOOT=yes
BRIDGE=virbr0' > /etc/sysconfig/network-scripts/ifcfg-ens32 echo 'TYPE=Bridge
BOOTPROTO=none
NAME=virbr0
DEVICE=virbr0
ONBOOT=yes
IPADDR=10.0.0.16
NETMASK=255.255.255.0
GATEWAY=10.0.0.2
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下生成临时目录

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

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

[root@LXC-10 tmp.MDcEY1DJq4]# ll
total 44
drwx------. 2 root root 83 Sep 6 07:06 gpg
-rw-r--r--. 1 root root 28139 Aug 26 18:16 index
-rw-r--r--. 1 root root 833 Aug 26 18:16 index.asc
-rw-r--r--. 1 root root 924 Aug 25 15:30 meta.tar.xz
-rw-r--r--. 1 root root 833 Aug 25 15:30 meta.tar.xz.asc
-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下的目录

[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
[root@LXC-10 rootfs]# pwd
/var/lib/lxc/my-container/rootfs
[root@LXC-10 rootfs]# ls
bin dev home lib64 mnt proc run selinux sys usr
boot etc lib media opt root sbin srv tmp var
[root@LXC-10 rootfs]#
[root@LXC-10 tmp]# ll
total 0
drwx------. 2 root root 24 Sep 6 05:06 ssh-3rTdYpl9wI
drwx------. 3 root root 17 Sep 6 06:29 systemd-private-f3a031d2b1c24651a394c24d0e6fae8c-vmtoolsd.service-nKQxYw
[root@LXC-10 tmp]#

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

常用命令可通过table补全

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

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

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

[root@LXC-10 ~]# lxc-start -n centos7
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)
Detected virtualization lxc.
Detected architecture x86-64. Welcome to CentOS Linux 7 (Core)! Cannot add dependency job for unit display-manager.service, ignoring: Unit not found.
[ OK ] Reached target Remote File Systems.
[ OK ] Created slice Root Slice.
[ OK ] Created slice System Slice.
[ OK ] Listening on /dev/initctl Compatibility Named Pipe.
[ OK ] Listening on Delayed Shutdown Socket.
[ OK ] Started Forward Password Requests to Wall Directory Watch.
[ OK ] Started Dispatch Password Requests to Console Directory Watch.
[ OK ] Reached target Paths.
[ OK ] Reached target Swap.
[ OK ] Reached target Local Encrypted Volumes.
[ OK ] Created slice User and Session Slice.
[ OK ] Reached target Slices.
[ OK ] Created slice system-getty.slice.
[ OK ] Listening on Journal Socket.
[ OK ] Reached target Local File Systems (Pre).
Starting Configure read-only root support...
Starting Read and set NIS domainname from /etc/sysconfig/network...
Starting Journal Service...
Mounting Huge Pages File System...
Mounting POSIX Message Queue File System...
[ OK ] Mounted POSIX Message Queue File System.
[ OK ] Started Read and set NIS domainname from /etc/sysconfig/network.
[ OK ] Mounted Huge Pages File System.
[ OK ] Started Journal Service.
Starting Flush Journal to Persistent Storage...
[ OK ] Started Configure read-only root support.
Starting Load/Save Random Seed...
[ OK ] Reached target Local File Systems.
[ OK ] Started Load/Save Random Seed.
<46>systemd-journald[16]: Received request to flush runtime journal from PID 1
[ OK ] Started Flush Journal to Persistent Storage.
Starting Create Volatile Files and Directories...
[ OK ] Started Create Volatile Files and Directories.
Starting Update UTMP about System Boot/Shutdown...
[ OK ] Started Update UTMP about System Boot/Shutdown.
[ OK ] Reached target System Initialization.
[ OK ] Listening on D-Bus System Message Bus Socket.
[ OK ] Reached target Sockets.
[ OK ] Started Daily Cleanup of Temporary Directories.
[ OK ] Reached target Timers.
[ OK ] Reached target Basic System.
[ OK ] Started D-Bus System Message Bus.
Starting Turn off network device...
Starting LSB: Bring up/down networking...
Starting Permit User Sessions...
Starting Login Service...
Starting Cleanup of Temporary Directories...
[ OK ] Started Turn off network device.
[ OK ] Started Permit User Sessions.
[ OK ] Started Cleanup of Temporary Directories.
[ OK ] Started Console Getty.
[ OK ] Reached target Login Prompts.
[ OK ] Started Command Scheduler.
[ OK ] Started Login Service. CentOS Linux 7 (Core)
Kernel 3.10.0-514.el7.x86_64 on an x86_64 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. vue 中后台 列表的增删改查同一解决方案

    查看 & 查询 常⻅业务列表⻚都是由 搜索栏 和 数据列表 组成. 其中: 搜索栏包含 搜索条件 . 新增 . 批量xx . 导出 等对 数据列表 全局操作功能项. 数据列表包含 分⻚ 和每条 ...

  2. day53:django:URL别名/反向解析&URL分发&命名空间&ORM多表操作修改/查询

    目录 1.URL别名&反向解析 2.URL分发&命名空间 3.ORM多表操作-修改 4.ORM多表操作-查询 4.1 基于对象的跨表查询 4.2 基于双下划线的跨表查询 4.3 聚合查 ...

  3. php Zookeeper使用踩坑

    用的是Zookeeper扩展,Php版本为7.2.17,下载地址: https://pecl.php.net/package/zookeeper 用的是0.6.4版本 创建节点官方给的示例如下: &l ...

  4. zookeeper源码之服务端

    zookeeper服务端主要包括一下几个模块:     1.启动模块. 2.核心执行模块 3.数据管理模块. 启动模块 读取配置文件,启动程序.详见:zookeeper源码之服务端启动模块. 核心执行 ...

  5. nacos快速安装

    一 什么是 Nacos 服务注册中心和配置中心. 二 使用 下载和启动 使用有两种方式 1.自己下载源码编译 2.下载编译好的压缩包 我比较懒选择了第二种方式. 最新稳定版本 下载地址:https:/ ...

  6. python-文本操作和二进制储存

    0x01 open方法 r read w write a append b byte test.txt内容为 yicunyiye wutang 读取test.txt f = open('test.tx ...

  7. spring-dao.xml通常写法

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  8. 关于bat/cmd中转义符的使用

    今天笔者在cmd中准备使用echo 输出<https://www.cnblogs.com/5201351> 发现直接就报错:命令语法不正确. 然后就想到可能是<和>在cmd中有 ...

  9. Docker数据卷和数据卷容器

    是什么 数据卷设计的目的,在于数据的永久化,他完全独立于容器的生存周期,因此,Docker不会在容器删除时删除其挂载的数据卷,也不会存在类似的垃圾收集机制对容器引用的数据卷进行处理.类似我们Redis ...

  10. Spring Boot学习(一)初识Spring Boot

    Spring Boot 概述 Spring Boot 是所有基于 Spring 开发的项目的起点.Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置 ...