Docker容器学习 --- Dockerfile的编写+Docker权限管理+Docker测试

Dockerfile的编写:

# vim Dcokerfile
FROM rhel7:yum   指定基础镜像
MAINTAINER [email protected]   作者信息
ENV HOSTNAME server1    设置容器主机名
EXPOSE 80    暴露容器端口
RUN yum install -y httpd && yum clean all   镜像操作命令
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]   镜像启动命令,默认只能启动一条。
# docker build -t rhel7:web .   创建镜像 rhel7:web

Dockerfile 常用指令:

1. 指定容器运行的用户
该用户将作为后续的 RUN 命令执行的用户。这个命令本实验不需要,但在一些需要指定用
户来运行的应用部署时非常关键,比如提供 hadoop 服务的容器通常会使用 hadoop 用户来启
动服务。
命令使用方式,例如使用 shiyanlou 用户来执行后续命令:
USER shiyanlou
2. 指定后续命令的执行目录
由于我们需要运行的是一个静态网站,将启动后的工作目录切换到/var/www/html 目录:WORKDIR /var/www/html
3. 对外连接端口号
由于内部服务会启动 Web 服务,我们需要把对应的 80 端口暴露出来,可以提供给容器间互
联使用,可以使用 EXPOSE 命令。
在镜像操作部分增加下面一句:
EXPOSE 80
4. 设置容器主机名
ENV 命令能够对容器内的环境变量进行设置:
ENV HOSTNAME sevrer1.example.com
5. 向镜像中增加文件
6. CMD 与 ENTRYPOINT。
7. 挂载数据卷
8. 设置容器内的环境变量
9. 使用 Supervisord

编写DockFile实现安装httpd服务:

[root@foundation38 docker]# ls
Dockerfile  web
[root@foundation38 docker]# touch yum.repo
[root@foundation38 docker]# vim yum.repo 
[root@foundation38 docker]# cat yum.repo   编写yum源
[source7.3]
name=source7.3
baseurl=http://172.25.38.250/source7.3
gpgcheck=0

[root@foundation38 docker]# ls
Dockerfile  web  yum.repo
[root@foundation38 docker]# cat Dockerfile   编写Dockfile
FROM rhel7docker run -it --name vm1 rhel7 bash
ENV HOSTNAME server1
MAINTAINER 1757159864@qq.com
EXPOSE 80
COPY yum.repo /etc/yum.repos.d/yum.repo
RUN yum install -y httpd && yum clean all
VOLUME ["/var/www/html"]
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
[root@foundation38 docker]# docker build -t rhel7:v1 .

这里写图片描述
创建容器进行环境的实践来保证Dockerfile可以正确执行:

[root@foundation38 netns]# docker run -it --name vm1 rhel7 bash  创建容器交互式运行rhel7的shell
bash-4.2# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
bash-4.2# cd /etc/yum.repos.d/
bash-4.2# ls
rhel7.repo
bash-4.2# vi yum.repo
bash-4.2# yum repolist
Skipping unreadable repository '///etc/yum.repos.d/rhel7.repo'
source7.3                                           | 4.1 kB     00:00     
(1/2): source7.3/group_gz                             | 136 kB   00:00     
(2/2): source7.3/primary_db                           | 3.9 MB   00:00     
repo id                           repo name                          status
source7.3                         source7.3                          4751
repolist: 4751
bash-4.2# cat yum.repo   配置yum源
[source7.3]
name=source7.3
baseurl=http://172.25.38.250/source7.3
gpgcheck=0

这里写图片描述

[root@foundation38 docker]# ls
Dockerfile  web  yum.repo
[root@foundation38 docker]# vim Dockerfile 
[root@foundation38 docker]# cat Dockerfile 
FROM rhel7
ENV HOSTNAME server1
MAINTAINER 1757159864@qq.com
EXPOSE 80
COPY yum.repo /etc/yum.repos.d/yum.repo
RUN rpmdb --rebuilddb && yum install -y httpd && yum clean all
VOLUME ["/var/www/html"]
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

这里写图片描述

[root@foundation38 docker]# docker build -t rhel7:v1 .   运行Dockerfile
Sending build context to Docker daemon 7.168 kB
Step 1/8 : FROM rhel7
 ---> 0a3eb3fde7fd
Step 2/8 : ENV HOSTNAME server1
 ---> Using cache
 ---> bbda1be07d9e
Step 3/8 : MAINTAINER [email protected]
 ---> Using cache
 ---> 9de9da7f6841
Step 4/8 : EXPOSE 80
 ---> Using cache
 ---> a4504786b603
Step 5/8 : COPY yum.repo /etc/yum.repos.d/yum.repo
 ---> Using cache
 ---> e8c13b6b6ffc
Step 6/8 : RUN rpmdb --rebuilddb && yum install -y httpd && yum clean all
 ---> Running in 830a02486077
Skipping unreadable repository '///etc/yum.repos.d/rhel7.repo'
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-45.el7 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-45.el7 for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libsystemd-daemon.so.0(LIBSYSTEMD_DAEMON_31)(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libsystemd-daemon.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-45.el7 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
---> Package redhat-logos.noarch 0:70.0.3-6.el7 will be installed
---> Package systemd-libs.x86_64 0:219-30.el7 will be installed
--> Processing Dependency: libdw.so.1()(64bit) for package: systemd-libs-219-30.el7.x86_64
--> Running transaction check
---> Package elfutils-libs.x86_64 0:0.166-2.el7 will be installed
--> Processing Dependency: elfutils-libelf(x86-64) = 0.166-2.el7 for package: elfutils-libs-0.166-2.el7.x86_64
--> Processing Dependency: libelf.so.1(ELFUTILS_1.7)(64bit) for package: elfutils-libs-0.166-2.el7.x86_64
--> Running transaction check
---> Package elfutils-libelf.x86_64 0:0.158-3.el7 will be updated
---> Package elfutils-libelf.x86_64 0:0.166-2.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package               Arch         Version               Repository       Size
================================================================================
Installing:
 httpd                 x86_64       2.4.6-45.el7          source7.3       1.2 M
Installing for dependencies:
 apr                   x86_64       1.4.8-3.el7           source7.3       103 k
 apr-util              x86_64       1.5.2-6.el7           source7.3        92 k
 elfutils-libs         x86_64       0.166-2.el7           source7.3       262 k
 httpd-tools           x86_64       2.4.6-45.el7          source7.3        84 k
 mailcap               noarch       2.1.41-2.el7          source7.3        31 k
 redhat-logos          noarch       70.0.3-6.el7          source7.3        13 M
 systemd-libs          x86_64       219-30.el7            source7.3       367 k
Updating for dependencies:
 elfutils-libelf       x86_64       0.166-2.el7           source7.3       208 k

Transaction Summary
================================================================================
Install  1 Package  (+7 Dependent packages)
Upgrade             ( 1 Dependent package)

Total download size: 15 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
--------------------------------------------------------------------------------
Total                                               63 MB/s |  15 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-3.el7.x86_64                                      1/10 
  Installing : apr-util-1.5.2-6.el7.x86_64                                 2/10 
  Installing : httpd-tools-2.4.6-45.el7.x86_64                             3/10 
  Updating   : elfutils-libelf-0.166-2.el7.x86_64                          4/10 
  Installing : elfutils-libs-0.166-2.el7.x86_64                            5/10 
  Installing : systemd-libs-219-30.el7.x86_64                              6/10 
  Installing : redhat-logos-70.0.3-6.el7.noarch                            7/10 
  Installing : mailcap-2.1.41-2.el7.noarch                                 8/10 
  Installing : httpd-2.4.6-45.el7.x86_64                                   9/10 
  Cleanup    : elfutils-libelf-0.158-3.el7.x86_64                         10/10 
  Verifying  : systemd-libs-219-30.el7.x86_64                              1/10 
  Verifying  : elfutils-libs-0.166-2.el7.x86_64                            2/10 
  Verifying  : apr-1.4.8-3.el7.x86_64                                      3/10 
  Verifying  : mailcap-2.1.41-2.el7.noarch                                 4/10 
  Verifying  : redhat-logos-70.0.3-6.el7.noarch                            5/10 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                 6/10 
  Verifying  : httpd-tools-2.4.6-45.el7.x86_64                             7/10 
  Verifying  : httpd-2.4.6-45.el7.x86_64                                   8/10 
  Verifying  : elfutils-libelf-0.166-2.el7.x86_64                          9/10 
  Verifying  : elfutils-libelf-0.158-3.el7.x86_64                         10/10 

Installed:
  httpd.x86_64 0:2.4.6-45.el7                                                   

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7               apr-util.x86_64 0:1.5.2-6.el7         
  elfutils-libs.x86_64 0:0.166-2.el7     httpd-tools.x86_64 0:2.4.6-45.el7     
  mailcap.noarch 0:2.1.41-2.el7          redhat-logos.noarch 0:70.0.3-6.el7    
  systemd-libs.x86_64 0:219-30.el7      

Dependency Updated:
  elfutils-libelf.x86_64 0:0.166-2.el7                                          

Complete!
Skipping unreadable repository '///etc/yum.repos.d/rhel7.repo'
Cleaning repos: source7.3
Cleaning up everything
 ---> ed82e9eb1581
Removing intermediate container 830a02486077
Step 7/8 : VOLUME /var/www/html
 ---> Running in f977d20283fb
 ---> 58b862c1c16a
Removing intermediate container f977d20283fb
Step 8/8 : CMD /usr/sbin/httpd -D FOREGROUND
 ---> Running in a5adb8068fcc
 ---> a6aec614557e
Removing intermediate container a5adb8068fcc
Successfully built a6aec614557e   可以正确执行

这里写图片描述
编写DockFile实现安装ssh服务:

[root@foundation38 docker]# mkdir ssh
[root@foundation38 docker]# ls
Dockerfile  ssh  web  yum.repo
[root@foundation38 docker]# cp Dockerfile ssh/
重新打开一个shell进行实验环境的搭建:
[root@foundation38 netns]# docker run -it --name vm1 rhel7 bash 进行测试
bash-4.2# yum install -y openssh-server openssh-clients 安装openssh软件
bash-4.2# ls -l /usr/sbin/sshd
-rwxr-xr-x 1 root root 819664 Sep  6  2016 /usr/sbin/sshd
bash-4.2# /usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
bash-4.2# cd /etc/ssh/
bash-4.2# ssh-keygen -q -t rsa -f /etc/ssh/ssh_host_rsa_key -N ""   挨个生成key
bash-4.2# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""
bash-4.2# ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
bash-4.2# ls
moduli          ssh_host_ecdsa_key.pub    ssh_host_rsa_key
ssh_config      ssh_host_ed25519_key      ssh_host_rsa_key.pub
ssh_host_ecdsa_key  ssh_host_ed25519_key.pub  sshd_config
bash-4.2# /usr/sbin/sshd

这里写图片描述

bash-4.2# ssh localhost
root@localhost's password: 
Permission denied, please try again.
root@localhost's password: 
Permission denied, please try again.
root@localhost's password: 

bash-4.2# echo root:westos | chpasswd   设置密码
bash-4.2# ssh localhost   可以用设置的密码登陆
root@localhost's password: 
-bash-4.2# exit
logout
Connection to localhost closed.

这里写图片描述
编写Dockerfile:

[root@foundation38 ssh]# ls
Dockerfile
[root@foundation38 ssh]# cd ..
[root@foundation38 docker]# ls
Dockerfile  ssh  web  yum.repo
[root@foundation38 docker]# cp yum.repo ssh/
[root@foundation38 docker]# cd ssh/
[root@foundation38 ssh]# ls
Dockerfile  yum.repo
[root@foundation38 ssh]# cat Dockerfile 
FROM rhel7
ENV HOSTNAME server2
MAINTAINER 1757159864@qq.com
EXPOSE 22
COPY yum.repo /etc/yum.repos.d/yum.repo
RUN rpmdb --rebuilddb && yum install -y openssh-server openssh-clients && yum clean all && ssh-keygen -q -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" && ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" && ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" && echo root:westos | chpasswd 
CMD ["/usr/sbin/sshd","-D"]

这里写图片描述

[root@foundation38 ssh]# docker build -t rhel7:v2 .  运行即可可以正确执行
Successfully built 0f3f4a1b0b2e

这里写图片描述
直接搭建httpd和ssh服务的框架:

[root@foundation38 ssh]# cd ..
[root@foundation38 docker]# pwd
/tmp/docker
[root@foundation38 docker]# ls
Dockerfile  ssh  web  yum.repo
[root@foundation38 docker]# vim yum.repo 配置yum源确保可以安装supervisor,写脚本之前可以自己进行测试
[root@foundation38 docker]# vim Dockerfile 
[root@foundation38 docker]# cat Dockerfile 编写Dockerfile
FROM rhel7
EXPOSE 80 22
COPY yum.repo /etc/yum.repos.d/yum.repo
RUN rpmdb --rebuilddb && yum install -y httpd openssh-server openssh-clients supervisor && yum clean all && ssh-keygen -q -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" && ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" && ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" && echo root:westos | chpasswd
CMD ["/usr/bin/supervisord"]

这里写图片描述

[root@foundation38 docker]# docker build -t rhel7:v3 .    运行Dockerfile
Sending build context to Docker daemon 7.168 kB
Step 1/5 : FROM rhel7
 ---> 0a3eb3fde7fd
Step 2/5 : EXPOSE 80 22
 ---> Running in 193de6cc330a
 ---> cb8b2a69c24b
Removing intermediate container 193de6cc330a
Step 3/5 : COPY yum.repo /etc/yum.repos.d/yum.repo
 ---> 75638e9efebf
Removing intermediate container 613dffa35c7b
Step 4/5 : RUN rpmdb --rebuilddb && yum install -y httpd openssh-server openssh-clients supervisor && yum clean all && ssh-keygen -q -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" && ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" && ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" && echo root:westos | chpasswd
 ---> Running in 6e7ae1a06089
Skipping unreadable repository '///etc/yum.repos.d/rhel7.repo'
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-45.el7 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-45.el7 for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libsystemd-daemon.so.0(LIBSYSTEMD_DAEMON_31)(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libsystemd-daemon.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
---> Package openssh-clients.x86_64 0:6.6.1p1-31.el7 will be installed
--> Processing Dependency: openssh = 6.6.1p1-31.el7 for package: openssh-clients-6.6.1p1-31.el7.x86_64
--> Processing Dependency: fipscheck-lib(x86-64) >= 1.3.0 for package: openssh-clients-6.6.1p1-31.el7.x86_64
--> Processing Dependency: libedit.so.0()(64bit) for package: openssh-clients-6.6.1p1-31.el7.x86_64
--> Processing Dependency: libfipscheck.so.1()(64bit) for package: openssh-clients-6.6.1p1-31.el7.x86_64
---> Package openssh-server.x86_64 0:6.6.1p1-31.el7 will be installed
--> Processing Dependency: libwrap.so.0()(64bit) for package: openssh-server-6.6.1p1-31.el7.x86_64
---> Package supervisor.noarch 0:3.1.3-3.el7 will be installed
--> Processing Dependency: python-meld3 >= 0.6.5 for package: supervisor-3.1.3-3.el7.noarch
--> Processing Dependency: python-setuptools for package: supervisor-3.1.3-3.el7.noarch
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package fipscheck-lib.x86_64 0:1.4.1-5.el7 will be installed
--> Processing Dependency: /usr/bin/fipscheck for package: fipscheck-lib-1.4.1-5.el7.x86_64
---> Package httpd-tools.x86_64 0:2.4.6-45.el7 will be installed
---> Package libedit.x86_64 0:3.0-12.20121213cvs.el7 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
---> Package openssh.x86_64 0:6.6.1p1-31.el7 will be installed
---> Package python-meld3.x86_64 0:0.6.10-1.el7 will be installed
---> Package python-setuptools.noarch 0:0.9.8-4.el7 will be installed
--> Processing Dependency: python-backports-ssl_match_hostname for package: python-setuptools-0.9.8-4.el7.noarch
---> Package redhat-logos.noarch 0:70.0.3-6.el7 will be installed
---> Package systemd-libs.x86_64 0:219-30.el7 will be installed
--> Processing Dependency: libdw.so.1()(64bit) for package: systemd-libs-219-30.el7.x86_64
---> Package tcp_wrappers-libs.x86_64 0:7.6-77.el7 will be installed
--> Running transaction check
---> Package elfutils-libs.x86_64 0:0.166-2.el7 will be installed
--> Processing Dependency: elfutils-libelf(x86-64) = 0.166-2.el7 for package: elfutils-libs-0.166-2.el7.x86_64
--> Processing Dependency: libelf.so.1(ELFUTILS_1.7)(64bit) for package: elfutils-libs-0.166-2.el7.x86_64
---> Package fipscheck.x86_64 0:1.4.1-5.el7 will be installed
---> Package python-backports-ssl_match_hostname.noarch 0:3.4.0.2-4.el7 will be installed
--> Processing Dependency: python-backports for package: python-backports-ssl_match_hostname-3.4.0.2-4.el7.noarch
--> Running transaction check
---> Package elfutils-libelf.x86_64 0:0.158-3.el7 will be updated
---> Package elfutils-libelf.x86_64 0:0.166-2.el7 will be an update
---> Package python-backports.x86_64 0:1.0-8.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                          Arch   Version                Repository
                                                                           Size
================================================================================
Installing:
 httpd                            x86_64 2.4.6-45.el7           source7.3 1.2 M
 openssh-clients                  x86_64 6.6.1p1-31.el7         source7.3 642 k
 openssh-server                   x86_64 6.6.1p1-31.el7         source7.3 440 k
 supervisor                       noarch 3.1.3-3.el7            docker    445 k
Installing for dependencies:
 apr                              x86_64 1.4.8-3.el7            source7.3 103 k
 apr-util                         x86_64 1.5.2-6.el7            source7.3  92 k
 elfutils-libs                    x86_64 0.166-2.el7            source7.3 262 k
 fipscheck                        x86_64 1.4.1-5.el7            source7.3  21 k
 fipscheck-lib                    x86_64 1.4.1-5.el7            source7.3  11 k
 httpd-tools                      x86_64 2.4.6-45.el7           source7.3  84 k
 libedit                          x86_64 3.0-12.20121213cvs.el7 source7.3  92 k
 mailcap                          noarch 2.1.41-2.el7           source7.3  31 k
 openssh                          x86_64 6.6.1p1-31.el7         source7.3 437 k
 python-backports                 x86_64 1.0-8.el7              source7.3 5.8 k
 python-backports-ssl_match_hostname
                                  noarch 3.4.0.2-4.el7          source7.3  12 k
 python-meld3                     x86_64 0.6.10-1.el7           docker     73 k
 python-setuptools                noarch 0.9.8-4.el7            source7.3 397 k
 redhat-logos                     noarch 70.0.3-6.el7           source7.3  13 M
 systemd-libs                     x86_64 219-30.el7             source7.3 367 k
 tcp_wrappers-libs                x86_64 7.6-77.el7             source7.3  66 k
Updating for dependencies:
 elfutils-libelf                  x86_64 0.166-2.el7            source7.3 208 k

Transaction Summary
================================================================================
Install  4 Packages (+16 Dependent packages)
Upgrade             (  1 Dependent package)

Total download size: 18 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
--------------------------------------------------------------------------------
Total                                               56 MB/s |  18 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : fipscheck-1.4.1-5.el7.x86_64                                1/22 
  Installing : fipscheck-lib-1.4.1-5.el7.x86_64                            2/22 
  Installing : apr-1.4.8-3.el7.x86_64                                      3/22 
  Installing : apr-util-1.5.2-6.el7.x86_64                                 4/22 
  Installing : openssh-6.6.1p1-31.el7.x86_64                               5/22 
  Installing : httpd-tools-2.4.6-45.el7.x86_64                             6/22 
  Updating   : elfutils-libelf-0.166-2.el7.x86_64                          7/22 
  Installing : elfutils-libs-0.166-2.el7.x86_64                            8/22 
  Installing : systemd-libs-219-30.el7.x86_64                              9/22 
  Installing : libedit-3.0-12.20121213cvs.el7.x86_64                      10/22 
  Installing : python-backports-1.0-8.el7.x86_64                          11/22 
  Installing : python-backports-ssl_match_hostname-3.4.0.2-4.el7.noarch   12/22 
  Installing : python-setuptools-0.9.8-4.el7.noarch                       13/22 
  Installing : python-meld3-0.6.10-1.el7.x86_64                           14/22 
  Installing : redhat-logos-70.0.3-6.el7.noarch                           15/22 
  Installing : tcp_wrappers-libs-7.6-77.el7.x86_64                        16/22 
  Installing : mailcap-2.1.41-2.el7.noarch                                17/22 
  Installing : httpd-2.4.6-45.el7.x86_64                                  18/22 
  Installing : openssh-server-6.6.1p1-31.el7.x86_64                       19/22 
  Installing : supervisor-3.1.3-3.el7.noarch                              20/22 
  Installing : openssh-clients-6.6.1p1-31.el7.x86_64                      21/22 
  Cleanup    : elfutils-libelf-0.158-3.el7.x86_64                         22/22 
  Verifying  : python-backports-ssl_match_hostname-3.4.0.2-4.el7.noarch    1/22 
  Verifying  : mailcap-2.1.41-2.el7.noarch                                 2/22 
  Verifying  : tcp_wrappers-libs-7.6-77.el7.x86_64                         3/22 
  Verifying  : httpd-2.4.6-45.el7.x86_64                                   4/22 
  Verifying  : python-setuptools-0.9.8-4.el7.noarch                        5/22 
  Verifying  : redhat-logos-70.0.3-6.el7.noarch                            6/22 
  Verifying  : python-meld3-0.6.10-1.el7.x86_64                            7/22 
  Verifying  : openssh-server-6.6.1p1-31.el7.x86_64                        8/22 
  Verifying  : openssh-clients-6.6.1p1-31.el7.x86_64                       9/22 
  Verifying  : elfutils-libs-0.166-2.el7.x86_64                           10/22 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                11/22 
  Verifying  : python-backports-1.0-8.el7.x86_64                          12/22 
  Verifying  : supervisor-3.1.3-3.el7.noarch                              13/22 
  Verifying  : fipscheck-lib-1.4.1-5.el7.x86_64                           14/22 
  Verifying  : systemd-libs-219-30.el7.x86_64                             15/22 
  Verifying  : apr-1.4.8-3.el7.x86_64                                     16/22 
  Verifying  : libedit-3.0-12.20121213cvs.el7.x86_64                      17/22 
  Verifying  : openssh-6.6.1p1-31.el7.x86_64                              18/22 
  Verifying  : httpd-tools-2.4.6-45.el7.x86_64                            19/22 
  Verifying  : elfutils-libelf-0.166-2.el7.x86_64                         20/22 
  Verifying  : fipscheck-1.4.1-5.el7.x86_64                               21/22 
  Verifying  : elfutils-libelf-0.158-3.el7.x86_64                         22/22 

Installed:
  httpd.x86_64 0:2.4.6-45.el7                                                   
  openssh-clients.x86_64 0:6.6.1p1-31.el7                                       
  openssh-server.x86_64 0:6.6.1p1-31.el7                                        
  supervisor.noarch 0:3.1.3-3.el7                                               

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7                                                      
  apr-util.x86_64 0:1.5.2-6.el7                                                 
  elfutils-libs.x86_64 0:0.166-2.el7                                            
  fipscheck.x86_64 0:1.4.1-5.el7                                                
  fipscheck-lib.x86_64 0:1.4.1-5.el7                                            
  httpd-tools.x86_64 0:2.4.6-45.el7                                             
  libedit.x86_64 0:3.0-12.20121213cvs.el7                                       
  mailcap.noarch 0:2.1.41-2.el7                                                 
  openssh.x86_64 0:6.6.1p1-31.el7                                               
  python-backports.x86_64 0:1.0-8.el7                                           
  python-backports-ssl_match_hostname.noarch 0:3.4.0.2-4.el7                    
  python-meld3.x86_64 0:0.6.10-1.el7                                            
  python-setuptools.noarch 0:0.9.8-4.el7                                        
  redhat-logos.noarch 0:70.0.3-6.el7                                            
  systemd-libs.x86_64 0:219-30.el7                                              
  tcp_wrappers-libs.x86_64 0:7.6-77.el7                                         

Dependency Updated:
  elfutils-libelf.x86_64 0:0.166-2.el7                                          

Complete!
Skipping unreadable repository '///etc/yum.repos.d/rhel7.repo'
Cleaning repos: docker source7.3
Cleaning up everything
 ---> f2dd63436a3a
Removing intermediate container 6e7ae1a06089
Step 5/5 : CMD /usr/bin/supervisord
 ---> Running in b0a673fb23e9
 ---> 64d04b9af726
Removing intermediate container b0a673fb23e9
Successfully built 64d04b9af726

这里写图片描述

[root@foundation38 docker]# ls
Dockerfile  ssh  supervisord.conf  web  yum.repo
[root@foundation38 docker]# pwd
/tmp/docker
[root@foundation38 docker]# cat supervisord.conf  配置进行多个服务的配置安装
[supervisord]
nodaemon=true

[program:sshd]
command=/usr/sbin/sshd -D

[program:httpd]
command=/usr/sbin/httpd
[root@foundation38 docker]# vim Dockerfile   编写Dockfile写入文件
[root@foundation38 docker]# cat Dockerfile 
FROM rhel7
EXPOSE 80 22
COPY yum.repo /etc/yum.repos.d/yum.repo
RUN rpmdb --rebuilddb && yum install -y httpd openssh-server openssh-clients supervisor && yum clean all && ssh-keygen -q -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" && ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" && ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" && echo root:westos | chpasswd
COPY supervisord.conf /etc/supervisord.conf
CMD ["/usr/bin/supervisord"]

这里写图片描述

[root@foundation38 docker]# docker build -t rhel7:v3 .  运行Dockerfile
Sending build context to Docker daemon 8.192 kB
Step 1/6 : FROM rhel7
 ---> 0a3eb3fde7fd
Step 2/6 : EXPOSE 80 22
 ---> Using cache
 ---> cb8b2a69c24b
Step 3/6 : COPY yum.repo /etc/yum.repos.d/yum.repo
 ---> Using cache
 ---> 75638e9efebf
Step 4/6 : RUN rpmdb --rebuilddb && yum install -y httpd openssh-server openssh-clients supervisor && yum clean all && ssh-keygen -q -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" && ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" && ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" && echo root:westos | chpasswd
 ---> Using cache
 ---> f2dd63436a3a
Step 5/6 : COPY supervisord.conf /etc/supervisord.conf
 ---> 01d6fe9d5140
Removing intermediate container 8f8b5a547e21
Step 6/6 : CMD /usr/bin/supervisord
 ---> Running in 2fa0ec4a9dff
 ---> 7065648c9208
Removing intermediate container 2fa0ec4a9dff
Successfully built 7065648c9208

这里写图片描述
测试httpd和ssh服务:

[root@foundation38 docker]# docker run -d --name v4 -v /tmp/docker/web:/var/www/html rhel7:v3  使用挂载的方式创建容器来测试httpd服务
5b480c7a9135817cf112021762b3377fde2999bca2d2bc7473dad2ac8859dc6a
[root@foundation38 docker]# docker ps 查看进程
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
5b480c7a9135        rhel7:v3            "/usr/bin/supervisord"   7 seconds ago       Up 6 seconds        22/tcp, 80/tcp      v4

这里写图片描述

[root@foundation38 docker]# ssh -l root 172.17.0.2  测试ssh的连接
The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established.
ECDSA key fingerprint is 30:0e:bd:7e:f3:bb:0c:32:ec:bc:60:c7:64:71:a2:ed.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.2' (ECDSA) to the list of known hosts.
root@172.17.0.2's password: 
-bash-4.2# logout
Connection to 172.17.0.2 closed.
[root@foundation38 docker]# docker ps  查看docker进程
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
5b480c7a9135        rhel7:v3            "/usr/bin/supervisord"   14 minutes ago      Up 14 minutes       22/tcp, 80/tcp      v4

这里写图片描述
在网页测试可以看到阿帕其默认访问目录:
这里写图片描述
CMD方式执行Dockerfile:

[root@foundation38 docker]# pwd
/tmp/docker
[root@foundation38 docker]# ls
Dockerfile  ssh  supervisord.conf  web  yum.repo
[root@foundation38 docker]# mkdir test
[root@foundation38 docker]# cd test/
[root@foundation38 test]# ls
[root@foundation38 test]# vim Dockerfile
[root@foundation38 test]# cat Dockerfile 
FROM rhel7
CMD echo "hello world!"
[root@foundation38 test]# docker build -t rhel7:v4 .   运行rhel7:v4镜像
Sending build context to Docker daemon 2.048 kB
Step 1/2 : FROM rhel7
 ---> 0a3eb3fde7fd
Step 2/2 : CMD echo "hello world!"
 ---> Running in a0917e260524
 ---> a87e3a0a73f3
Removing intermediate container a0917e260524
Successfully built a87e3a0a73f3
[root@foundation38 test]# docker run --rm rhel7:v4   运行
hello world!
[root@foundation38 test]# docker run --rm rhel7:v4 echo westos  可以直接加参数改变输出结果
westos

这里写图片描述
ENTRYPOINT方式执行Dockerfile:

[root@foundation38 test]# docker rmi rhel7:v4
Untagged: rhel7:v4
Deleted: sha256:a87e3a0a73f33f7fc7dc4c6b66e7425ee9ba6f6d79830a7017a564fda4d18993
[root@foundation38 test]# vim Dockerfile 
[root@foundation38 test]# cat Dockerfile   不可以改变输出
FROM rhel7
ENTRYPOINT echo "hello world!"
[root@foundation38 test]# docker build -t rhel7:v4 .
Sending build context to Docker daemon 2.048 kB
Step 1/2 : FROM rhel7
 ---> 0a3eb3fde7fd
Step 2/2 : ENTRYPOINT echo "hello world!"
 ---> Running in 449065a6b8c9
 ---> 008e969ef028
Removing intermediate container 449065a6b8c9
Successfully built 008e969ef028
[root@foundation38 test]# docker run --rm rhel7:v4
hello world!
[root@foundation38 test]# docker run --rm rhel7:v4 westos  运行的时候无法改变输出内容
hello world!
[root@foundation38 test]# docker run --rm rhel7:v4 echo westos
hello world!

这里写图片描述
CMD+ENTRYPOINT方式执行Dockerfile:

[root@foundation38 test]# vim Dockerfile 
[root@foundation38 test]# cat Dockerfile 
FROM rhel7
ENTRYPOINT ["/bin/echo","hello"]
CMD ["world!"]
[root@foundation38 test]# docker build -t rhel7:v4 .
Sending build context to Docker daemon 3.584 kB
Step 1/3 : FROM rhel7
 ---> 0a3eb3fde7fd
Step 2/3 : ENTRYPOINT /bin/echo hello
 ---> Running in 2060a8acf448
 ---> 0824a1c19f07
Removing intermediate container 2060a8acf448
Step 3/3 : CMD world!
 ---> Running in fd34017606e0
 ---> 1f39bb2fac7c
Removing intermediate container fd34017606e0
Successfully built 1f39bb2fac7c
[root@foundation38 test]# docker run --rm rhel7:v4
hello world!
[root@foundation38 test]# docker run --rm rhel7:v4 westos  只可以改变CMD后面所接的参数
hello westos

这里写图片描述
ENV+ENTRYPOINT方式执行Dockerfile:

[root@foundation84 docker]# cd test/
[root@foundation84 test]# vim Dockerfile
[root@foundation84 test]# cat Dockerfile 
FROM rhel7
ENV name westos
ENTRYPOINT echo "hello $name"
[root@foundation84 test]# docker build -t rhel7:v4 .
Sending build context to Docker daemon 2.048 kB
Step 1/3 : FROM rhel7
 ---> 0a3eb3fde7fd
Step 2/3 : ENV name westos
 ---> Running in 24d40e637bc8
 ---> 3cdbe2c044d3
Removing intermediate container 24d40e637bc8
Step 3/3 : ENTRYPOINT echo "hello $name"
 ---> Running in 6c98600583b2
 ---> 61cd65eefd7a
Removing intermediate container 6c98600583b2
Successfully built 61cd65eefd7a
[root@foundation84 test]# docker run --rm rhel7:v4
hello westos
[root@foundation84 test]# docker run --rm rhel7:v4 test  依旧是不可改变是输出结果
hello westos

这里写图片描述
调用变量的方式执行Dockerfile:

[root@foundation38 test]# docker rmi rhel7:v4  删除镜像
Untagged: rhel7:v4
Deleted: sha256:053738a7cab652f8ea16db9f6cf62489eed2a429aabfbe2ff4e64f971e67dbe9
Deleted: sha256:69e6915b05a4e8b6075da815411e80a465a028c6ce14311fdffab7cabf0000b9
[root@foundation38 test]# vim Dockerfile 
[root@foundation38 test]# cat Dockerfile 
FROM rhel7
ENV name westos
ENTRYPOINT ["/bin/sh","-c","echo $name"]
[root@foundation38 test]# docker build -t rhel7:v4 .
Sending build context to Docker daemon 3.584 kB
Step 1/3 : FROM rhel7
 ---> 0a3eb3fde7fd
Step 2/3 : ENV name westos
 ---> Running in 224d6dc22103
 ---> b34c2134005d
Removing intermediate container 224d6dc22103
Step 3/3 : ENTRYPOINT /bin/sh -c echo $name
 ---> Running in 1670d554df25
 ---> fa8d510a0508
Removing intermediate container 1670d554df25
Successfully built fa8d510a0508
[root@foundation38 test]# docker run --rm rhel7:v4
westos
[root@foundation38 test]# docker run --rm rhel7:v4 test  调用变量文件中已经指定,另外赋值不会改变输出
westos

这里写图片描述
docker的部分命令演示:

[root@foundation38 test]# docker ps  查看docker进程
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
5b480c7a9135        rhel7:v3            "/usr/bin/supervisord"   About an hour ago   Up About an hour    22/tcp, 80/tcp      v4
[root@foundation38 test]# docker ps -f status=exited    查看为退出状态的docker
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
fb61b91db3e0        e8c13b6b6ffc        "/bin/sh -c 'yum i..."   About an hour ago   Exited (1) About an hour ago                       distracted_easley
[root@foundation38 test]# docker ps -aqf status=exited
fb61b91db3e0
[root@foundation38 test]# docker rm `docker ps -aqf status=exited`  将已经是退出状态的容器删除
fb61b91db3e0
[root@foundation38 test]# docker run --help | grep privileged 
      --privileged                            Give extended privileges to this container

这里写图片描述
–privileged=true命令赋予容器权限:

[root@foundation38 test]# docker run -it --name vm1 ubuntu  创建并运行容器
root@c40afb506da9:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
root@c40afb506da9:/# ip addr  可以查看到IP
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
62: eth0@if63: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:3/64 scope link 
       valid_lft forever preferred_lft forever
root@c40afb506da9:/# ip link set down eth0  没有权限将ip关闭
RTNETLINK answers: Operation not permitted
root@c40afb506da9:/# exit

这里写图片描述

[root@foundation38 test]# docker run -it --name vm2 --privileged=true ubuntu  赋予权限创建并运行docker
root@f60613681bcc:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
64: eth0@if65: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:3/64 scope link 
       valid_lft forever preferred_lft forever
root@f60613681bcc:/# ip link set down eth0  可以down掉eth0
root@f60613681bcc:/# ip addr  查看ip已经被down掉
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
64: eth0@if65: <BROADCAST,MULTICAST> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 scope global eth0
       valid_lft forever preferred_lft forever

这里写图片描述

root@f60613681bcc:/# ip link set up eth0  重新将eth0启动
root@f60613681bcc:/# ip addr  查看IP已经启动了eth0
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
64: eth0@if65: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.3/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:3/64 scope link tentative 
       valid_lft forever preferred_lft forever
root@f60613681bcc:/# id  容器就有了超级用户的权限
uid=0(root) gid=0(root) groups=0(root)
root@f60613681bcc:/# exit
exit
[root@foundation38 test]# docker rm -f `docker ps -aq`  删除所有容器
f60613681bcc
c40afb506da9
5b480c7a9135

这里写图片描述
加载stress镜像进行压力测试:

[root@foundation38 docker]# docker run --rm -it --name vm1 -m 100M --memory-swap 100M stress --vm 1 --vm-bytes 90M  当内存小于所指定物理内存和swap分区和时可以进行压测
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [6] forked
stress: dbug: [6] allocating 94371840 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: dbug: [6] freed 94371840 bytes
stress: dbug: [6] allocating 94371840 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: dbug: [6] freed 94371840 bytes
stress: dbug: [6] allocating 94371840 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: dbug: [6] freed 94371840 bytes
stress: dbug: [6] allocating 94371840 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: dbug: [6] freed 94371840 bytes
stress: dbug: [6] allocating 94371840 bytes ...
stress: dbug: [6] touching bytes in strides of 4096 bytes ...
stress: dbug: [6] freed 94371840 bytes
stress: dbug: [6] allocating 94371840 bytes ...

这里写图片描述
再打开一个shell输入top命令来实时动态地查看系统的整体运行情况:
这里写图片描述
当内存超过所给的100M时无法进行压力测试:

[root@foundation38 docker]# docker run --rm -it --name vm1 -m 100M --memory-swap 100M stress --vm 1 --vm-bytes 110M
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [7] forked
stress: dbug: [7] allocating 115343360 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: FAIL: [1] (416) <-- worker 7 got signal 9
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (422) kill error: No such process
stress: FAIL: [1] (452) failed run completed in 0s

这里写图片描述
交换分区的内存是物理内存的1.5到2倍,当物理内存不够的时候,会调用交换分区,磁盘是慢设备,当swap被占用系统会慢下来。
不设置交换分区时候可以超过所给内存值,会自动调用交换分区:

[root@foundation38 docker]# docker run --rm -it --name vm1 -m 100M stress --vm 1 --vm-bytes 110M
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [7] forked
stress: dbug: [7] allocating 115343360 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: dbug: [7] freed 115343360 bytes
stress: dbug: [7] allocating 115343360 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: dbug: [7] freed 115343360 bytes
stress: dbug: [7] allocating 115343360 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: dbug: [7] freed 115343360 bytes
stress: dbug: [7] allocating 115343360 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...

这里写图片描述
在top端查看可以查看到stress运行情况:
这里写图片描述
当测试内存为所指定内存的两倍时无法执行,默认swap分区大小等于内存大小:

[root@foundation38 docker]# docker run --rm -it --name vm1 -m 100M stress --vm 1 --vm-bytes 200M
stress: info: [1] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: dbug: [1] using backoff sleep of 3000us
stress: dbug: [1] --> hogvm worker 1 [7] forked
stress: dbug: [7] allocating 209715200 bytes ...
stress: dbug: [7] touching bytes in strides of 4096 bytes ...
stress: FAIL: [1] (416) <-- worker 7 got signal 9
stress: WARN: [1] (418) now reaping child worker processes
stress: FAIL: [1] (422) kill error: No such process
stress: FAIL: [1] (452) failed run completed in 2s

这里写图片描述
进行优先级测试,调用两个终端进行压测:
这里写图片描述
这里写图片描述
查看本机cpu个数:

[root@foundation38 docker]# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 42
Model name:            Intel(R) Core(TM) i5-2520M CPU @ 2.50GHz
Stepping:              7
CPU MHz:               3012.792
BogoMIPS:              4984.04
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              3072K
NUMA node0 CPU(s):     0-3

这里写图片描述
在top端并没有体现出优先级,因为-c 1指定一个cpu,本机总共由四个cpu,意味着cpu会全速给两个进程进行测试,所以无法突显优先级
这里写图片描述
指定4个cpu进行优先级测试,相当于两个人抢唯一的一个东西:
这里写图片描述
这里写图片描述
在top端可以明显看到1024的优先级在512的两倍:
这里写图片描述
测试容器写的速度:

[root@foundation38 docker]# docker run --rm -it --device-write-bps /dev/sda:10M ubuntu  指定每秒10M运行容器
root@e809badd2b7e:/# time dd if=/dev/zero of=testfile bs=1M count=100 oflag=direct  测试截取100M所需时间
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 9.95491 s, 10.5 MB/s  速度与设定值接近

real    0m9.957s
user    0m0.000s
sys 0m0.031s
root@e809badd2b7e:/# time dd if=/dev/zero of=testfile bs=1M count=200 oflag=direct  测试截取200M所需时间
200+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 19.9544 s, 10.5 MB/s   速度与设定值接近

real    0m19.955s
user    0m0.000s
sys 0m0.061s
root@e809badd2b7e:/# exit
exit

这里写图片描述

[root@foundation38 docker]# docker run --rm -it --device-write-bps /dev/sda:20M ubuntu  指定每秒20M运行docker
root@6ee57f13f9ea:/# time dd if=/dev/zero of=testfile bs=1M count=200 oflag=direct  测试截取200M所需时间
200+0 records in
200+0 records out
209715200 bytes (210 MB) copied, 9.93999 s, 21.1 MB/s  速度与所给值接近

real    0m9.941s
user    0m0.003s
sys 0m0.062s
root@6ee57f13f9ea:/# exit
exit
[root@foundation38 docker]# docker run --rm -it --device-write-bps /dev/sda:5M ubuntu  指定每秒5M运行docker
root@3a1af75ea8dd:/# time dd if=/dev/zero of=testfile bs=1M count=20 oflag=direct  测试截取20M所需时间
20+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 4.00237 s, 5.2 MB/s  速度与所给值接近

real    0m4.003s
user    0m0.000s
sys 0m0.007s

这里写图片描述

猜你喜欢

转载自blog.csdn.net/aaaaaab_/article/details/81867595