天天看点

docker 创建虚拟机并且远程链接 shell脚本

#!/bin/bash
updatesj(){
# 更新时间
rpmntpdate=`rpm -qa | grep ntpdate | wc -l`
if [ $rpmntpdate -gt 0 ]
                                                                                        # 相等 -eq    不相等 -ne     大于  -gt     大等于  -ge     小于  -lt       小等于  -le
   then
       echo "已经安装时间更新 ntpdate"
    else
           yum install -y ntpdate                                               # 安装工具
           ntpdate -u cn.pool.ntp.org                                   # 同步时间
           timedatectl set-timezone Asia/Shanghai               # 设置区域
           systemctl start ntpd                                         # 启动时间
           systemctl enable ntpd                                                # 开机启动:自动同步
fi
}
updatesj
installDocker(){
# 安装 docker
docker --version
if [ $? -eq 0 ]                                                                        # $? 判断上一条命令是否执行成功, -eq 等于  0
  then
       echo "已经安装docker"
       docker pull centos:7                                                  # 拉取镜像centos7
       tagc=$(docker images | grep centos | awk -F" " '{print $3}') ; echo $tagc
       tagd="127.0.0.1:5000/rancher/centos:7"
       docker tag $tagc $tagd                                             # 打标签
       docker rmi $tagd                                                       # 删除标签
  else
       echo "正在安装docker 中。。。"
       wget eisc.cn/file/shell/dockerinstall.sh ; sh dockerinstall.sh
fi
}
installDocker
systemctl restart docker                                                 # 重启docker
xnjdemo(){
       useradd demo                                                      # 创建用户  demo
       echo "eisc.cn" | passwd --stdin demo                  # 为用户 demo 设置密码为: eisc.cn
       chmod u+w /etc/sudoers                                     # 用户写 权限 这个文件
       cp /etc/sudoers /etc/sudoers.back
       sed -i "/^#/d" /etc/sudoers                                  # 删除开头#号的行
       sed -i "/^$/d" /etc/sudoers                                   # 删除空行
       echo "domo    ALL=(ALL)       ALL" >> /etc/sudoers
                                                                                     # 添加用户权限 所有权限
        sudo docker pull centos:7                                    # 拉群centos7 镜像,因为使用的 demo 用户,修改系统需要加上 sudo
        sudo docker images                                             # 列出镜像列表
        sudo systemctl start firewalld                               # 启动防火墙; stop 关闭
        sudo firewall-cmd --permanent --zone=public --add-port=10022/tcp
        sudo firewall-cmd --permanent --zone=public --add-port=10080/tcp
        firewall-cmd --reload                                            # 开放端口,和重载防火墙配置
        firewall-cmd --list-all                                             # 列出防火墙所有规则
        # sudo docker run -d --name c7-d1 --privileged=true -p 10022:22 -p 10080:80 \
        # -h c7-docker-1 -v /home/fd/container/c7-d-1:/home/c7-d1-ys centos:7 /usr/sbin/init
        docker run --name eisc  --privileged=true -dit -p 10022:22 -p 10080:80 centos:7 /usr/sbin/init
                                                                                       # 符号  \ 将长命令换行编写;   运行centos7 版本  名字为eisc
                                                                                       # 注意:一个单词不能换行后还有空格存在,例如: date  变成:dat  e
                                                                                       # -d 后台运行方式
                                                                                       # name 创建的容器名,方便启动、关闭、重启、删除容器等操作
                                                                                       # privileged=true        加上之后容器内部权限更多,不会出现权限问题
                                                                                       # -p 10022:22 -p 10080:80       指定端口映射,可同时放通多个端口
                                                                                       # -h c7-docker-1 指定容器主机名
                                                                                       # -v /home/fd/container/c7-d-1:/home/c7-d1-ys                                                         宿主机目录映射到容器内部目录
                                                                                       # centos:7       本地centos镜像版本
                                                                                       # /usr/sbin/init 启动方式
       docker exec -it eisc /bin/bash
                                                                                        # 进入启动的容器 eisc
                                                                                        # -d    后台运行方式
                                                                                        # name  创建的容器名,方便启动、关闭、重启、删除容器等操作
                                                                                        # privileged=true       加上之后容器内部权限更多,不会出现权限问题
                                                                                        # -p 10022:22 -p 10080:80      指定端口映射,可同时放通多个端口
                                                                                        # -h c7-docker-1        指定容器主机名
                                                                                        # -v /home/fd/container/c7-d-1:/home/c7-d1-ys                                                        宿主机目录映射到容器内部目录
                                                                                        # centos:7      本地centos镜像版本

                                                                                       # /usr/sbin/init        启动方式
}
xnjdemo
centosxuniji(){
# 启动的虚拟机需要执行下面命令 否则无法通过端口映射远程连接docker虚拟机

      yum install -y ntpdate                                              # 安装工具
      ntpdate -u cn.pool.ntp.org                                       # 同步时间
      timedatectl set-timezone Asia/Shanghai                  # 设置区域
      systemctl start ntpd                                                  # 启动时间
      systemctl enable ntpd                                              # 开机启动:自动同步
      yum install -y wget


repo="centos-yum.sh"
if [ ! -e $repo ]; then                 # -e 判断文件存在;参考:Shell if 条件判断文件或目录
yum install -y wget
wget www.eisc.cn/file/shell/centos-yum.sh ; sh centos-yum.sh
else
echo "yum已经切换"
fi



yum -y install systemd && \
yum -y install firewalld && \
yum -y install openssh openssh-server openssh-clients && \
systemctl start sshd
# ssh-keygen -t rsa                                # 配置免密登陆密钥
echo "eisc.cn" | passwd --stdin root
}

# docker commit c5d63d185542 centos7-csh    # 保存 docker ps -a 运行中的镜像,名字为:centos7-csh
# wget eisc.cn/file/shell/docker-xnj.sh ; sh docker-xnj.sh