
# 制作Docker镜像并启动容器
本节以HCE 2.0为例介绍在HCE上制作HCE的Docker镜像并启动容器。
#### 约束限制
- 运行容器镜像的HCE系统版本和制作的HCE容器镜像版本须保持一致。
 
#### 制作镜像归档文件
1. 确认repo源配置正常。 请检查默认的/etc/yum.repos.d/hce.repo配置文件中参数是否正确，正确的配置如下。
   ```
   [base]
   name=HCE $releasever base
   baseurl=https://repo.huaweicloud.com/hce/$releasever/os/$basearch/
   enabled=1
   gpgcheck=1
   gpgkey=https://repo.huaweicloud.com/hce/$releasever/os/RPM-GPG-KEY-HCE-2
   [updates]
   name=HCE $releasever updates
   baseurl=https://repo.huaweicloud.com/hce/$releasever/updates/$basearch/
   enabled=1
   gpgcheck=1
   gpgkey=https://repo.huaweicloud.com/hce/$releasever/updates/RPM-GPG-KEY-HCE-2
    
   [debuginfo]
   name=HCE $releasever debuginfo
   baseurl=https://repo.huaweicloud.com/hce/$releasever/debuginfo/$basearch/
   enabled=0
   gpgcheck=1
   gpgkey=https://repo.huaweicloud.com/hce/$releasever/debuginfo/RPM-GPG-KEY-HCE-2
   ```
   ![](https://support.huaweicloud.com/usermanual-hce/public_sys-resources/note_3.0-zh-cn.png)
   HCEOS 3.0的配置中，RPM-GPG-KEY-HCE-2换成RPM-GPG-KEY-HCE-3。
   
2. 新建一个全新的目录作为Docker镜像的根系统文件目录，以/tmp/docker_rootfs目录为例，并将软件包安装到该目录。
   ```
   mkdir -p /tmp/docker_rootfs 
   yum --setopt=install_weak_deps=False --installroot /tmp/docker_rootfs --releasever 2.0 install bash yum coreutils security-tool procps-ng vim-minimal tar findutils filesystem hce-repos hce-rootfiles cronie -y
   ```
   ![](https://support.huaweicloud.com/usermanual-hce/public_sys-resources/caution_3.0-zh-cn.png)
   上述yum命令中，默认会安装当前 HCE 系统版本所需的软件包，如需安装指定HCE版本的软件包，可通过--releasever参数显示指定目标版本。例如，上述命令用于安装HCE 2.0所需的软件包。
   
3. chroot进入临时目录。
   ```
   chroot /tmp/docker_rootfs
   ```
   
4. 配置临时目录。
   1. 使用security-tool.sh关闭不必要服务。
      ```
      export EULEROS_SECURITY=0
      echo "export TMOUT=300" >> /etc/bashrc
      /usr/sbin/security-tool.sh -d / -c /etc/hce_security/hwsecurity/hce_security_install.conf -u /etc/hce_security/usr-security.conf -l /var/log/hce-security.log -s
      ```
      执行过程中，有如[图1]错误打印均为正常现象，报错的原因为：
      - 缺少服务文件。在chroot文件系统下没有启动服务导致。
      
      - 缺少引导系统的文件/etc/sysconfig/init。工具在系统启动阶段关闭服务，镜像rootfs不涉及系统启动。
      
      - 缺少/proc/sys/kernel/sysrq，这是系统启动后生成的调用节点，在chroot文件系统下不存在。
      
      
      图1报错图示   
      ![](https://support.huaweicloud.com/usermanual-hce/zh-cn_image_0000002512088945.png "点击放大") 
   
   2. 卸载security-tool、cronie、systemd软件包及其依赖软件包。
      ```
      cp -af /etc/pam.d /etc/pam.d.bak
      rm -f /etc/yum/protected.d/sudo.conf /etc/yum/protected.d/systemd.conf
      yum remove -y security-tool cronie systemd
      rpm -e --nodeps logrotate crontabs
      rm -rf /etc/pam.d
      mv /etc/pam.d.bak /etc/pam.d
      sh -c 'shopt -s globstar; for f in $(ls /**/*.rpmsave); do rm -f $f; done' &> /dev/null 
      [ -d /var/lib/dnf ] && rm -rf /var/lib/dnf/*
      [ -d /var/lib/rpm ] && rm -rf /var/lib/rpm/__db.*
      ```
      
   
   3. 移除/boot目录。
      ```
      rm -rf /boot
      ```
      
   
   4. 设置容器镜像语言为en_US。
      ```
      cd /usr/lib/locale;rm -rf $(ls | grep -v en_US | grep -vw C.utf8 )
      rm -rf /usr/share/locale/*
      ```
      
   
   5. 移除共享文件 man、doc、info和mime。
      ```
      rm -rf /usr/share/{man,doc,info,mime}
      ```
      
   
   6. 移除缓存日志文件。
      ```
      rm -rf /etc/ld.so.cache
      [ -d /var/cache/ldconfig ] && rm -rf /var/cache/ldconfig/*
      [ -d /var/cache/dnf ] && rm -rf /var/cache/dnf/*
      [ -d /var/log ] && rm -rf /var/log/*.log
      ```
      
   
   7. 移除java安全证书。
      ```
      rm -rf /etc/pki/ca-trust/extracted/java/cacerts /etc/pki/java/cacerts
      ```
      
   
   8. 移除/etc/machine-id。
      ```
      rm -rf /etc/machine-id
      ```
      
   
   9. 移除/etc/mtab。
      ```
      rm -rf /etc/mtab
      ```
      
    
5. 退出chroot。
   ```
   exit
   ```
   
6. 打包压缩临时目录，生成Docker镜像归档文件hce-docker.x86_64.tar.xz。
   归档路径为/tmp/docker_rootfs/hce-docker.x86_64.tar.xz
   ```
   pushd /tmp/docker_rootfs/
   tar cvf hce-docker.x86_64.tar .
   xz hce-docker.x86_64.tar
   popd
   ```
   
 
#### 使用镜像归档文件启动容器
1. 确认repo源配置正常。 请检查默认的/etc/yum.repos.d/hce.repo配置文件中参数是否正确，正确的配置如下。
   ```
   [base]
   name=HCE $releasever base
   baseurl=https://repo.huaweicloud.com/hce/$releasever/os/$basearch/
   enabled=1
   gpgcheck=1
   gpgkey=https://repo.huaweicloud.com/hce/$releasever/os/RPM-GPG-KEY-HCE-2
   [updates]
   name=HCE $releasever updates
   baseurl=https://repo.huaweicloud.com/hce/$releasever/updates/$basearch/
   enabled=1
   gpgcheck=1
   gpgkey=https://repo.huaweicloud.com/hce/$releasever/updates/RPM-GPG-KEY-HCE-2
    
   [debuginfo]
   name=HCE $releasever debuginfo
   baseurl=https://repo.huaweicloud.com/hce/$releasever/debuginfo/$basearch/
   enabled=0
   gpgcheck=1
   gpgkey=https://repo.huaweicloud.com/hce/$releasever/debuginfo/RPM-GPG-KEY-HCE-2
   ```
   ![](https://support.huaweicloud.com/usermanual-hce/public_sys-resources/note_3.0-zh-cn.png)
   HCEOS 3.0的配置中，RPM-GPG-KEY-HCE-2换成RPM-GPG-KEY-HCE-3。
   
2. 安装docker软件包。
   ```
   yum install docker -y
   ```
   
3. 使用镜像归档文件创建容器镜像。
   ```
   mv /tmp/docker_rootfs/hce-docker.x86_64.tar.xz .
   docker import hce-docker.x86_64.tar.xz
   ```
   执行**docker images**命令可查看到容器镜像ID，例如6cfefae3a541。
   图2查看容器镜像ID   
   ![](https://support.huaweicloud.com/usermanual-hce/zh-cn_image_0000002480089052.png "点击放大")
   ![](https://support.huaweicloud.com/usermanual-hce/public_sys-resources/note_3.0-zh-cn.png)
   创建镜像可使用如下命令指定镜像的REPOSITORY和TAG参数。
   **docker import \[OPTIONS\] file\|URL\|- \[REPOSITORY\[:TAG\]\]**
   
4. 使用指定的镜像运行容器并进入其bash环境。 运行如下命令后，如果shell视图改变，表示成功进入容器的bash环境。6cfefae3a541为上述镜像ID。
   ```
   docker run -it 6cfefae3a541 bash
   ```
   
 
