change
This commit is contained in:
5
Container/Docker/Dockerfile/Centos/Dockerfile
Normal file
5
Container/Docker/Dockerfile/Centos/Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
||||
FROM scratch
|
||||
|
||||
ADD ./centos/rootfs /
|
||||
|
||||
CMD /bin/bash
|
||||
34
Container/Docker/Dockerfile/Centos/Dockerfile-ssh
Normal file
34
Container/Docker/Dockerfile/Centos/Dockerfile-ssh
Normal file
@@ -0,0 +1,34 @@
|
||||
FROM centos:7
|
||||
|
||||
# 设置环境变量(登录密码)
|
||||
ARG ROOT_PASSWORD
|
||||
|
||||
# 切换镜像源
|
||||
RUN mv /etc/yum.repos.d/* /tmp \
|
||||
&& curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
|
||||
|
||||
# 安装SSH服务及相关工具
|
||||
RUN yum install -y \
|
||||
openssh-server \
|
||||
openssh-clients \
|
||||
passwd \
|
||||
sudo \
|
||||
vim \
|
||||
net-tools \
|
||||
iproute \
|
||||
which \
|
||||
&& yum clean all
|
||||
|
||||
# 配置SSH服务
|
||||
RUN ssh-keygen -A \
|
||||
&& mkdir -p /var/run/sshd \
|
||||
&& echo "root:${ROOT_PASSWORD}" | chpasswd \
|
||||
&& sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/' /etc/ssh/sshd_config \
|
||||
&& sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config \
|
||||
&& sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config
|
||||
|
||||
# 开放SSH端口
|
||||
EXPOSE 22
|
||||
|
||||
# 启动SSH服务
|
||||
CMD ["/usr/sbin/sshd", "-D"]
|
||||
38
Container/Docker/Dockerfile/Centos/README.md
Normal file
38
Container/Docker/Dockerfile/Centos/README.md
Normal file
@@ -0,0 +1,38 @@
|
||||
*
|
||||
|
||||
> 本文作者:丁辉
|
||||
|
||||
# Centos
|
||||
|
||||
- Centos 基础镜像构建
|
||||
|
||||
```bash
|
||||
./build.sh
|
||||
```
|
||||
|
||||
- Centos 配置策略允许外部通过 SSH 登录
|
||||
|
||||
1. 构建镜像
|
||||
|
||||
```bash
|
||||
docker build --build-arg ROOT_PASSWORD=password -t centos-ssh:7 -f Dockerfile-ssh .
|
||||
```
|
||||
|
||||
**参数解释**
|
||||
|
||||
`ROOT_PASSWORD`:配置登录 Centos 默认 ROOT 密码
|
||||
|
||||
2. 启动镜像并登录
|
||||
|
||||
```bash
|
||||
docker run --name centos -p 2222:22 -d centos-ssh:7
|
||||
```
|
||||
|
||||
3. 登录
|
||||
|
||||
```bash
|
||||
ssh -p 2222 root@IP
|
||||
```
|
||||
|
||||
|
||||
|
||||
72
Container/Docker/Dockerfile/Centos/build.sh
Normal file
72
Container/Docker/Dockerfile/Centos/build.sh
Normal file
@@ -0,0 +1,72 @@
|
||||
#!/bin/bash
|
||||
|
||||
#############################################################################################
|
||||
# 用途: 构建 Centos 系统 Docker 镜像的脚本
|
||||
# 作者: 丁辉
|
||||
# 编写时间: 2023-11-27
|
||||
#############################################################################################
|
||||
|
||||
# 镜像地址
|
||||
# 阿里云: https://mirrors.aliyun.com/centos/
|
||||
# 官方: https://www.centos.org/
|
||||
# 其他: https://vault.centos.org/
|
||||
|
||||
VERSION="7.9.2009"
|
||||
|
||||
CENTOS_VERSION="7"
|
||||
URL="https://mirrors.aliyun.com/centos/$VERSION/os/x86_64/Packages"
|
||||
RPM_VERSION="centos-release-7-9.2009.0.el7.centos.x86_64.rpm"
|
||||
|
||||
CENTOS_URL="$URL/$RPM_VERSION"
|
||||
|
||||
# 加载检测脚本
|
||||
source <(curl -sS https://gitee.com/offends/Linux/raw/main/File/Shell/Check_command.sh)
|
||||
|
||||
function INSTALL_WGET(){
|
||||
CHECK_INSTALL wget
|
||||
}
|
||||
|
||||
# 初始化目录和文件
|
||||
function INIT_DIR(){
|
||||
CHECK_DIR ./centos/rootfs && cd ./centos
|
||||
CHECK_COMMAND_NULL rpm --root $PWD/rootfs --initdb
|
||||
SEND_INFO "初始化目录和文件完成"
|
||||
SEND_INFO "正在获取RPM文件"
|
||||
CHECK_COMMAND_NULL wget $CENTOS_URL
|
||||
|
||||
CHECK_FILE "centos-release-7-9.2009.0.el7.centos.x86_64.rpm"
|
||||
NULL_TRUE rpm -ivh --nodeps --root $PWD/rootfs --package ./$RPM_VERSION
|
||||
|
||||
# #在无法获取到软件包源的情况下使用
|
||||
# SEND_INFO "正在备份 YUM 源文件"
|
||||
# CHECK_DIR /etc/yum.repos.d/Offends
|
||||
# CHECK_COMMAND_NULL \cp -r /etc/yum.repos.d/epel.repo /etc/yum.repos.d/Offends && \cp -r /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Offends
|
||||
# # 获取需要的软件包源
|
||||
# SEND_INFO "正在获取软件包源"
|
||||
# CHECK_COMMAND_NULL wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-$CENTOS_VERSION.repo
|
||||
# CHECK_COMMAND_NULL wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-$CENTOS_VERSION.repo
|
||||
# # 清除缓存
|
||||
# SEND_INFO "正在清除缓存"
|
||||
# CHECK_COMMAND_NULL yum makecache
|
||||
# # 根据自己需求修改
|
||||
# CHECK_COMMAND_NULL sed -i 's@baseurl=.*@baseurl=https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/@' /etc/yum.repos.d/*.repo
|
||||
|
||||
SEND_INFO "正在安装基础软件包,拉取过程较慢请稍后"
|
||||
CHECK_COMMAND_NULL yum --installroot=$PWD/rootfs install yum --nogpgcheck -y
|
||||
SEND_INFO "开始构建镜像"
|
||||
CHECK_COMMAND_NULL cd .. && docker build -t centos:$VERSION .
|
||||
SEND_INFO "构建完成,镜像名称: centos:$VERSION"
|
||||
|
||||
# # 恢复 YUM 源文件
|
||||
# SEND_INFO "正在恢复 YUM 源文件"
|
||||
# CHECK_COMMAND_NULL rm -rf /etc/yum.repos.d/CentOS-Base.repo && rm -rf /etc/yum.repos.d/epel.repo
|
||||
# CHECK_COMMAND_NULL cp -r /etc/yum.repos.d/Offends/* /etc/yum.repos.d/
|
||||
}
|
||||
|
||||
|
||||
function ALL(){
|
||||
INSTALL_WGET
|
||||
INIT_DIR
|
||||
}
|
||||
|
||||
ALL
|
||||
Reference in New Issue
Block a user