dcoker
docker简介
Docker的初衷也就是将各种应用程序和他们所依赖的运行环境打包成标准的container/image,进而发布到不同的平台上运行
底层使用LXC启动一个Linux Container,通过cgroup等机制对不同的container内运行的应用程序进行隔离,权限管理和quota分配等。
每个container拥有自己独立的各种命名空间(亦即资源)包括: PID 进程, MNT 文件系统, NET 网络, IPC , UTS 主机名 等
在LXC的基础上, Docker额外提供的Feature包括:标准统一的打包部署运行方案, 历史版本控制, Image的重用,Image共享发布等等
受目前AUFS文件系统的限制,默认Layer的层级最多只能达到127
使用Dockerfile构建Image时,每条指令都会给最终的Image增加一层layer依赖关系。使用Dockerfile时,尽可能合并多个操作:例如使用 "&&" 或 ";" 合并运行多个shell命令;将多个shell命令写成脚本,在dockerfile中添加并运行这个脚本
虽然Docker目前默认使用LXC和AUFS,但是Docker的核心思想本身,并不强制绑定这两者,0.8版本已经可以使用BTRFS,而整个Docker框架也改成了插件式的架构,便于添加替换各个功能模块

在RedHat工程师的帮助下在docker0.7版本中实现了driver机制, AUFS只是其中的一个driver, 在RHEL中采用的则是Device Mapper的方式实现的container文件系统。
Docker 的价值
一个 IT 系统大致可以分为:
- 应用程序
- 运行时平台(bin/framework/lib)
- 操作系统
- 硬件(基础设施)
开发人员的主要工作是应用程序的编码、构建、测试和发布,涉及应用程序和运行时平台这两层。而运维人员的工作则涉及从硬件、操作系统到运行时平台的安装、配置、运行监控、升级和优化等工作。
云计算最主要的价值体现在实现了自动化运维。提高资源的利用效率,是较为次要的价值。
要实现自动化运维,需要解除 IT 系统各层之间的耦合,使各层变成可编程控制,即「software defined」的。只有我们可以编程创建和控制各层对象,才能达到最大程度的运维自动化。
硬件虚拟化技术解除了硬件和操作系统的耦合,用户可以编程创建和控制虚拟机(包括虚拟硬件,运行自定义的操作系统镜像)、虚拟存储设备和虚拟网络。
操作系统虚拟化技术解除了操作系统和运行时平台的耦合,用户可以编程定义适合应用运行的平台,包括需要的二进制程序、框架和库等。操作虚拟化技术既可以用来创建和管理系统容器,也可以用来创建和管理应用容器。如果是用作系统容器,这相当于一个轻量级虚拟机,目的是解除操作系统与底层的耦合。例如, LXC/LXD 和 OpenVZ 属于这个路线的。而 Docker 容器主要是用作应用容器,包括两方面的功能:
静态打包:把应用程序及其运行时打包为一个镜像;
动态运行:以一个应用容器的形式运行这个应用。
Docker 本身只提供最基本的功能,如果用户需要构建一个复杂的 IT 系统,单靠 Docker 本身是不够的。
Docker 不仅提供了 API,还形成了一个庞大的社区和生态系统,基于 Docker 的解决方案具有非常大的灵活性
我个人觉得 Docker 是符合 Unix/Linux 哲学的:只做一件事,做到最好。还能与其它程序复合(compose),完成更复杂的功能。
dock安装操作
centos7 安装:
yum install -y docker
systemctl start docker
yum install -y docker-io [root@js-212 vhost]# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 3395 [OK] jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.3.1611 x8... 70 [OK] nimmis/java-centos This is docker images of CentOS 7 with dif... 26 [OK] gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 19 [OK] kinogmt/centos-ssh CentOS with SSH 13 [OK] egyptianbman/docker-centos-nginx-php A simple and highly configurable docker co... 9 [OK] torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK] nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 6 [OK] centos/mariadb55-centos7 5 [OK] darksheer/centos Base Centos Image -- Updated hourly 3 [OK] harisekhon/centos-java Java on CentOS (OpenJDK, tags jre/jdk7-8) 2 [OK] harisekhon/centos-scala Scala + CentOS (OpenJDK tags 2.10-jre7 - 2... 2 [OK] vorakl/centos CentOS7, EPEL, tools. Updated/Tested daily! 1 [OK] timhughes/centos Centos with systemd installed and running 1 [OK] freenas/centos Simple CentOS Linux interactive container 1 [OK] blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK] sgfinans/docker-centos CentOS with a running sshd and Docker 1 [OK] smartentry/centos centos with smartentry 0 [OK] vcatechnology/centos A CentOS Image which is updated daily 0 [OK] kz8s/centos Official CentOS plus epel-release 0 [OK] grayzone/centos auto build for centos. 0 [OK] grossws/centos CentOS 6 and 7 base images with gosu and l... 0 [OK] repositoryjp/centos Docker Image for CentOS. 0 [OK] aguamala/centos CentOS base image 0 [OK] januswel/centos yum update-ed CentOS image 0 [OK] [root@js-212 vhost]# docker pull centos latest: Pulling from centos 6b4988a50482: Pull complete 12bb2fef2ea5: Pull complete 3bea5ca0a170: Pull complete Digest: sha256:22dea1e8bdc68a5bdcebb3a634edaae8815d9feb120f25339523cb83ec050b0f Status: Downloaded newer image for centos:latest [root@js-212 vhost]# docker images 仓库 标签 镜像ID(唯一) 镜像大小 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos latest 3bea5ca0a170 7 days ago 192.5 MB docker search 搜索镜像 docker pull 获取镜像 docker images 查看镜像 docker rmi + 镜像ID号 删除镜像 [root@js-212 vhost]# docker run centos /bin/echo 'hello world!' hello world! [root@js-212 vhost]# docker ps -a 容器ID 镜像 运行的命令 创建的时间 状态 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 13ff0ed3865e centos "/bin/echo 'hello wo About a minute ago Exited (0) About a minute ago focused_hawking [root@js-212 vhost]# docker run --name mydocker -it centos /bin/bash [root@f8655f615858 /]# ls / anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@f8655f615858 /]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.1 0.0 11716 1884 ? Ss 08:14 0:00 /bin/bash root 16 0.0 0.0 47388 1688 ? R+ 08:15 0:00 ps aux [root@js-212 soft]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f8655f615858 centos "/bin/bash" About a minute ago Up About a minute mydocker 13ff0ed3865e centos "/bin/echo 'hello wo 22 minutes ago Exited (0) 22 minutes ago focused_hawking 删除所有停止的容器 docker rm $(docker ps -a -q) 挂载外部命令: docker run -v /sbin/ip:/sbin/ip --name testdocker01 -it centos 后台启动: [root@js-212 vhost]# docker run -d --name testdocker02 centos f93a63e45ab3a64b6f5449596a8389d6611f733a74f3b927be7f34c5bcabb9e4 [root@js-212 ~]# docker run -d --name mynginx nginx (如果没有nginx镜像,则会下载) 查看最后创建的一个容器: [root@js-212 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e9dfee4222b0 nginx "nginx -g 'daemon of 12 minutes ago Up 12 minutes 80/tcp mynginx docker参数: Commands: attach Attach to a running container
eg: [root@js-212 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e9dfee4222b0 nginx "nginx -g 'daemon of 14 minutes ago Up 14 minutes 80/tcp mynginx [root@js-212 ~]# docker attach e9dfee4222b0 ###这个参数慎用,因为在这里很多时候会卡主,当用ctrl+c 时,已运行的容器会把状态改为:Exited 进入容器的另外一个命令: yum install -y util-linux 获取容器PID: [root@js-212 ~]# docker inspect --format "{{.State.Pid}}" mynginx 2286 或者: [root@js-212 ~]# docker inspect --format "{{.State.Pid}}" e9dfee4222b0 2286 登录到容器: [root@js-212 ~]# nsenter --target 3556 --mount --uts --ipc --net --pid build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders from a container's filesystem to the host path create Create a new container diff Inspect changes on a container's filesystem events Get real time events from the server exec Run a command in a running container export Stream the contents of a container as a tar archive history Show the history of an image images List images import Create a new filesystem image from the contents of a tarball info Display system-wide information inspect Return low-level information on a container or image kill Kill a running container load Load an image from a tar archive login Register or log in to a Docker registry server logout Log out from a Docker registry server logs Fetch the logs of a container pause Pause all processes within a container port Lookup the public-facing port that is NAT-ed to PRIVATE_PORT ps List containers pull Pull an image or a repository from a Docker registry server push Push an image or a repository to a Docker registry server rename Rename an existing container restart Restart a running container rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save an image to a tar archive search Search for an image on the Docker Hub start Start a stopped container stats Display a stream of a containers' resource usage statistics stop Stop a running container tag Tag an image into a repository top Lookup the running processes of a container unpause Unpause a paused container version Show the Docker version information wait Block until a container stops, then print its exit code
查看docker在系统里面创建的网桥: [root@js-212 ~]# brctl show bridge name bridge id STP enabled interfaces docker0 8000.da462f1ad314 no veth3055e71 docker还会在nat表里面创建一个地址转换。 [root@js-212 ~]# iptables -t nat -L -n Chain PREROUTING (policy ACCEPT) target prot opt source destination DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL Chain POSTROUTING (policy ACCEPT) target prot opt source destination MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0 Chain OUTPUT (policy ACCEPT) target prot opt source destination DOCKER all -- 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL Chain DOCKER (2 references) target prot opt source destination 查看docker容器里面的路由表: root@e33491b1f13f:/# ip ro li 172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.6 default via 172.17.42.1 dev eth0 随机映射端口(-P参数): [root@js-212 ~]# docker run -d -P --name mynginx1 nginx 64d0c9fba9096023e70ca75bbe0b24342ab3487aa6ee839d9b40900d0a0d9856 [root@js-212 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 64d0c9fba909 nginx "nginx -g 'daemon of 17 seconds ago Up 16 seconds 0.0.0.0:10001->80/tcp mynginx1 随机映射端口的好处再于:端口不用冲突。难处再于:要获取这个容器的端口 指定映射端口(-p): [root@js-212 ~]# docker run -d -p 9191:80 --name mynginx2 nginx ac271c8dfa9373acbb3ff830c02521e37deaca86ab685f24e5ebed84a0a0bdf2 命令解析:-p用于指定映射端口,9191是本地机未被使用的端口,80是机器里面要被映射的端口 [root@js-212 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ac271c8dfa93 nginx "nginx -g 'daemon of 11 seconds ago Up 10 seconds 0.0.0.0:9191->80/tcp mynginx2 指定映射端口方式: 1. -p hostPort:containerPort 2. -p ip:hostPort:containerPort #主机有多个IP地址 3. -p ip::containerPort #绑定到某个IP地址,但是端口随机 4. -p hostPort:containerPort -p hostPort:containerPort #指定多个端口;例如: -p 8080:80 -p 9191:443 docker run -h hostname 可以指定容器主机名称 -------------------docker数据卷: [root@js-212 ~]# docker run -it --name volume-test1 -h nginx -v /data centos [root@nginx ~]# cd /data/ [root@nginx data]# ls -v 参数是添加一个数据卷 host机器上面查看 上面的data 数据卷在host机器上的什么位置 [root@js-212 ~]# docker inspect -f {{.Volumes}} volume-test1 map[/data:/var/lib/docker/volumes/81a8c0c510cf46d5d475776c9089afbc629630550ebf4f6c5929cc78d03575f0/_data] 在host机器上面创建一个文件: [root@js-212 _data]# touch test.txt [root@js-212 _data]# ls test.txt 然后到docker容器里面的/data 目录下查看: [root@nginx data]# ll /data/ total 0 -rw-r--r-- 1 root root 0 Jun 14 05:30 test.txt 在docker容器里面创建一个目录: [root@nginx data]# mkdir test_dict [root@nginx data]# ll total 4 -rw-r--r-- 1 root root 0 Jun 14 05:30 test.txt drwxr-xr-x 2 root root 4096 Jun 14 05:31 test_dict 然后到host机器里面查看: [root@js-212 _data]# ll total 4 drwxr-xr-x 2 root root 4096 Jun 14 13:31 test_dict -rw-r--r-- 1 root root 0 Jun 14 13:30 test.txt 说明挂载的数据卷 docker和 本地映射到的目录是相通的。 指定本地目录挂在到docker容器里面: 本地需要挂载的目录是opt,查看opt 目录下的内容 [root@js-212 /]# ls /opt/ logs maven model oracle profile.properties recommendList scripts soft tprofiler.jar webapps.zip 启动docker 并挂载数据卷,参数:-v + 本地的目录:docker容器里的目录; 注意:路径后面不能有斜线。 [root@js-212 ~]# docker run -it --name volume-test2 -h nginx -v /opt:/opt centos [root@nginx /]# ls /opt/ logs maven model oracle profile.properties recommendList scripts soft tprofiler.jar webapps.zip 只读 [root@js-212 ~]# docker run -it --name volume-test2 -h nginx -v /opt:/opt:ro centos -------------------------------docker数据卷容器 挂载一个容器里面挂载的目录,无论这个容器是 Exited或者Up 状态,只要添加 --volumes-from 这个参数即可挂载另外一个容器相同的目录。 [root@js-212 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4871c87f24df centos "/bin/bash" 9 minutes ago Exited (0) 3 minutes ago volume-test2 dd52a498037a centos "/bin/bash" 18 minutes ago Exited (0) 11 minutes ago volume-test1 ac271c8dfa93 nginx "nginx -g 'daemon of 3 hours ago Up 3 hours 0.0.0.0:9191->80/tcp mynginx2 64d0c9fba909 nginx "nginx -g 'daemon of 3 hours ago Up 3 hours 0.0.0.0:10001->80/tcp mynginx1 e33491b1f13f nginx "nginx -g 'daemon of 19 hours ago Up 19 hours 80/tcp mynginx [root@js-212 ~]# docker run -it --name volume-test4 --volumes-from volume-test1 centos [root@60bc26b110d4 /]# ls /data/ test.txt test_dict 镜像构建: 手动构建,dockfile 手动构建: [root@js-212 ~]# docker run --name ngin-man -it centos cd /usr/local/src/ yum install -y wget gcc gcc-c++ make openssl openssl-devel wget https://nginx.org/download/nginx-1.13.1.tar.gz wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz tar xf pcre-8.40.tar.gz tar xf nginx-1.13.1.tar.gz useradd -s /sbin/nologin -M www cd nginx-1.13.1 ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.40 make && make install /etc/rc.local里面添加开机启动: /usr/local/nginx/sbin/nginx /usr/local/nginx/conf/nginx.conf/nginx 添加 daemon off; 使nginx在前台运行。 退出,查看容器: [root@js-212 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 243cb37a2828 centos "/bin/bash" 23 minutes ago Exited (0) 6 seconds ago ngin-man [root@js-212 ~]# docker commit -m "my nginx" 243cb37a2828 ryan/my-nginx:v1 创建镜像:v1 表示v1版本 449cfdb0f351e7598f643226439a19f07dea0d78ed79c37e05def5b4ac8fc43b 查看: [root@js-212 ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ryan/my-nginx v1 449cfdb0f351 30 seconds ago 405.4 MB centos latest 3bea5ca0a170 8 days ago 192.5 MB nginx latest e6b35a35c4d4 2 weeks ago 109.4 MB [root@js-212 ~]# docker run -it -p 8082:80 --name mynginx-v1-01 ryan/my-nginx:v1 修改/etc/rc.local:把 /usr/local/nginx/sbin/nginx 去掉;然后退出。 然后新建一个v2 版本的镜像。 [root@js-212 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d7783409677d ryan/my-nginx:v1 "/bin/bash" 29 seconds ago Exited (0) 8 seconds ago mynginx-v1-01 [root@js-212 ~]# docker commit -m "v2" d7783409677d ryan/my-nginx:v2 fec587fd0ce484b0ef79ea0625417b669cccc2b68bbd9d557c595c632c32b28e You have new mail in /var/spool/mail/root [root@js-212 ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ryan/my-nginx v2 fec587fd0ce4 7 seconds ago 405.4 MB ryan/my-nginx v1 449cfdb0f351 9 minutes ago 405.4 MB centos latest 3bea5ca0a170 8 days ago 192.5 MB nginx latest e6b35a35c4d4 2 weeks ago 109.4 MB v2 版本没有开机启动nginx,所以,status为Exited ,然后加上 /usr/local/nginx/sbin/nginx 就可以看到容器已经Up了。看如下操作: [root@js-212 ~]# docker run -d -p 8083:80 ryan/my-nginx:v2 35775f2151c552147733b534b5345c03e5a2842dda2bfb4b425c8755aa51e2a1 [root@js-212 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 35775f2151c5 ryan/my-nginx:v2 "/bin/bash" 6 seconds ago Exited (0) 4 seconds ago lonely_thompson [root@js-212 ~]# docker run -d -p 8083:80 ryan/my-nginx:v2 /usr/local/nginx/sbin/nginx 559d73b4fb55e171357a8ccc48a9da1418b61c668c3765048b6e43411f8ee8df [root@js-212 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 559d73b4fb55 ryan/my-nginx:v2 "/usr/local/nginx/sb 2 seconds ago Up 1 seconds 0.0.0.0:8083->80/tcp stupefied_lovelace ----------------Dockerfile 镜像创建 4种类型: 1)基础镜像信息 2)维护者信息 3)镜像操作指令 4)容器启动时执行指令 From {它的妈妈是谁(基础镜像)} MAINTAINER {告诉别人,你创造了它(维护信息)} RUN {你想让它干啥(把命令前面加上run)} ADD {往肚子里放点文件(COPY文件,会自动解压)} WORKDIR {我是cd,今天刚化了妆(当前工作目录)} VOLUME {给我一个存放行李的地方(目录挂载)} EXPOSE {我要打开的门是啥(端口)} RUN {运行起来,(进程要一直运行下去)} 操作步骤: [root@js-212 ~]# mkdir /opt/docker-file [root@js-212 ~]# cd /opt/docker-file [root@js-212 docker-file]# [root@js-212 docker-file]# mkdir nginx [root@js-212 docker-file]# cd nginx/ [root@js-212 nginx]# pwd /opt/docker-file/nginx 下载nginx的包和pcre 的包: wget https://nginx.org/download/nginx-1.13.1.tar.gz wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz 创建Dockerfile文件: [root@js-212 nginx]# cat Dockerfile # This is My first Dockerfile #Version: 1.0 #Author: ryan #Base images(基础镜像) FROM centos #MAINTAINER (维护者) MAINTAINER ryan #ADD (tar包进入docker容器以后自动解压) ADD pcre-8.40.tar.gz /usr/local/src ADD nginx-1.13.1.tar.gz /usr/local/src #RUN (容器里面要使用的命令) RUN yum install -y wget gcc gcc-c++ make openssl openssl-devel RUN useradd -s /sbin/nologin -M www #WORKDIR (WORKDIR相当于cd,ENV是添加环境变量,CMD是启动nginx) WORKDIR /usr/local/src/nginx-1.13.1 RUN ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.40 && make && make install RUN echo "daemon off;" >>/usr/local/nginx/conf/nginx.conf ENV PATH /usr/local/nginx/sbin:$PATH EXPOSE 80 CMD ["nginx"] 构建: 构建参数解释:-t 指定名称:版本; /opt/docker-file/nginx/ 是Dockerfile的路径 [root@js-212 nginx]# docker build -t nginx-file:v1 /opt/docker-file/nginx/ Sending build context to Docker daemon 3.054 MB Sending build context to Docker daemon Step 0 : FROM centos ---> 3bea5ca0a170 Step 1 : MAINTAINER ryan ---> Running in e8d9c0253ec9 ---> 4d20a79cac1c Removing intermediate container e8d9c0253ec9 Step 2 : ADD pcre-8.40.tar.gz /usr/lcoal/src ---> 31a07507743c Removing intermediate container 4a0ce8abb006 Step 3 : ADD nginx-1.13.1.tar.gz /usr/local/src ---> 3a95b29074ad Removing intermediate container 6e666c4cb80c Step 4 : RUN yum install -y wget gcc gcc-c++ make openssl openssl-devel ---> Running in cecf5f69330f .......(此处是一些安装信息) Step 8 : RUN echo "daemon off;" >>/usr/local/nginx/conf/nginx.conf ---> Running in 4d70b00ed310 ---> 71e1d989ee08 Removing intermediate container 4d70b00ed310 Step 9 : ENV PATH /usr/local/nginx/sbin:$PATH ---> Running in 7674a5dde367 ---> e640c25fe072 Removing intermediate container 7674a5dde367 Step 10 : EXPOSE 80 ---> Running in fd459fb193b9 ---> f5aeda2c654a Removing intermediate container fd459fb193b9 Step 11 : CMD nginx ---> Running in f1e903e58bca ---> 9b3268e8400a Removing intermediate container f1e903e58bca Successfully built 9b3268e8400a 注释:每一步都有一个ID号,dockerfile 是一个层级关系。 查看dockerfile 构建的镜像 [root@js-212 nginx]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE nginx-file v1 9b3268e8400a About a minute ago 402.4 MB 启动,查看。 [root@js-212 nginx]# docker run -d -p 8084:80 nginx-file:v1 a52dd4ad7fd36b188da09985834eabd42b27225696cc5e901c19f605a4939356 [root@js-212 nginx]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a52dd4ad7fd3 nginx-file:v1 "nginx" 6 seconds ago Up 4 seconds 0.0.0.0:8084->80/tcp adoring_banach 页面输入:IP:8084 就可以看到nginx已经启动。 创建一个压力测试的镜像: [root@js-212 stress]# ll total 12 -rw-r--r-- 1 root root 2572 May 15 2015 Centos-6.repo -rw-r--r-- 1 root root 163 Jun 14 17:49 Dockerfile -rw-r--r-- 1 root root 1083 May 15 2015 epel-6.repo [root@js-212 stress]# cat Dockerfile FROM centos MAINTAINER ryan ADD epel-6.repo /etc/yum.repos.d/ ADD Centos-6.repo /etc/yum.repos.d/ RUN yum -y install stress && yum clean all ENTRYPOINT ["stress"] [root@js-212 stress]# docker build -t stress /opt/docker-file/stress/ 不给镜像命名一个TAG,默认是latest [root@js-212 nginx]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE stress latest cae8c86f7a1d 7 seconds ago 212.2 MB nginx-file v1 9b3268e8400a 2 hours ago 402.4 MB ryan/my-nginx v2 fec587fd0ce4 3 hours ago 405.4 MB docker 容器 cpu 默认有1024个配额 [root@js-212 ~]# docker run -it --rm -c 512 stress --cpu 2 --rm 表示 退出以后删除容器。 -c 表示docker 容器限制的cpu配额。 --cpu 是stress的参数,2 表示2个cpu。 还可以设置使用的那个cpu --cpuset-cpus docker提供了–cpu-period、–cpu-quota两个参数控制容器可以分配到的CPU时钟周期。–cpu-period是用来指定容器对CPU的使用要在多长时间内做一次重新分配,而–cpu-quota是用来指定在这个周期内,最多可以有多少时间用来跑这个容器。跟–cpu-shares不同的是这种配置是指定一个绝对值,而且没有弹性在里面,容器对CPU资源的使用绝对不会超过配置的值。 cpu-period和cpu-quota的单位为微秒(μs)。cpu-period的最小值为1000微秒,最大值为1秒(10^6 μs),默认值为0.1秒(100000 μs)。cpu-quota的值默认为-1,表示不做控制。 举个例子,如果容器进程需要每1秒使用单个CPU的0.2秒时间,可以将cpu-period设置为1000000(即1秒),cpu-quota设置为200000(0.2秒)。当然,在多核情况下,如果允许容器进程需要完全占用两个CPU,则可以将cpu-period设置为100000(即0.1秒),cpu-quota设置为200000(0.2秒)。 内存分配: [root@js-212 ~]# docker run -it --rm -m 512m stress --vm 1 --vm-bytes 256M --vm-hang 0 #后面stress参数的 256M不能 超过前面 512M ,不然会直接kill掉。 ---------------docker核心原理-资源隔离和限制 docker怎么实现资源隔离? docker资源隔离 LXC Kernel namespace namespace类型:pid隔离,net隔离,Ipc隔离,Mnt隔离,Uts隔离,User隔离, docker资源限制。(linux通过cgroup进行资源限制) docker也通过cgroup资源限制,配额。 docker支持:CPU,内存。
-----------docker 网络模式
docker默认使用桥接模式。
可以加参数实现:--net=bridge
---------------docker核心原理-资源隔离和限制
docker怎么实现资源隔离?
docker资源隔离 LXC Kernel namespace
namespace类型:pid隔离,net隔离,Ipc隔离,Mnt隔离,Uts隔离,User隔离,
docker资源限制。(linux通过cgroup进行资源限制)
docker也通过cgroup资源限制,配额。
docker支持:CPU,内存。
====================docker register docker的私有仓库
docker pull register
[root@js-212 /]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
registry latest bb682e0824bd 5 weeks ago 33.17 MB
[root@js-212 /]# docker run -d -p 5001:5000 registry #默认5000
559aa880e3b9619a345c7d70d4738afbe65c73e3d40fb66be03dc8028c622d90
[root@js-212 /]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
nginx-file v1 9b3268e8400a 21 hours ago 402.4 MB
registry latest bb682e0824bd 5 weeks ago 33.17 MB
[root@js-212 /]# docker tag nginx-file 192.168.20.212:5001/test/es:v1
Error response from daemon: could not find image: no such id: nginx-file
[root@js-212 /]# docker tag 9b3268e8400a 192.168.20.212:5001/test/es:v1
[root@js-212 /]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
nginx-file v1 9b3268e8400a 21 hours ago 402.4 MB
192.168.20.212:5001/test/es v1 9b3268e8400a 21 hours ago 402.4 MB
registry latest bb682e0824bd 5 weeks ago 33.17 MB
[root@js-212 /]# docker push 192.168.20.212:5001/test/es:v1
Error response from daemon: invalid registry endpoint https://192.168.20.212:5001/v0/: unable to ping registry endpoint https://192.168.20.212:5001/v0/
v2 ping attempt failed with error: Get https://192.168.20.212:5001/v2/: tls: oversized record received with length 20527
v1 ping attempt failed with error: Get https://192.168.20.212:5001/v1/_ping: tls: oversized record received with length 20527. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry 192.168.20.212:5001` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/192.168.20.212:5001/ca.crt
这里有报错:
1,解决办法用哪个nginx的https协议去做:
2,ssl证书
3,
[root@js-212 /]# vi /etc/sysconfig/docker #修改如下;看错误里面的信息就有。
other_args="--insecure-registry 192.168.20.212:5001"然后删除原来的registry的镜像,重新做一次:
docker run -d -p 5001:5000 registry
docker tag 9b3268e8400a 192.168.20.212:5001/test/es:v1
docker push 192.168.20.212:5001/test/es:v1
从别的机器pull register。也需要修改docker的 /etc/sysconfig/docker信息,如上
然后
docker pull 192.168.20.212:5001/test/es:v1
[root@openfire2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
192.168.20.212:5001/test/es v1 9b3268e8400a 23 hours ago 402.4 MB
然后启动:
[root@openfire2 ~]# docker run -it 9b3268e8400a
docker的页面管理:shipyard
https://www.shipyard-project.com/docs/deploy/manual/
使用Docker build 命令才生成一个镜像,有时候由于代码跟新频繁就会产生很多的none的image, 于是就写了以下脚本: docker ps -a | grep "Exited" | awk '{print $1 }'|xargs docker stop docker ps -a | grep "Exited" | awk '{print $1 }'|xargs docker rm docker images|grep none|awk '{print $3 }'|xargs docker rmi
浙公网安备 33010602011771号