有盐轻小说
24.94MB · 2025-10-13
备份:
cd /etc/yum.repos.d/
mkdir repo_bak
mv *.repo repo_bak/ # 备份所有 .repo 文件
切换yum源:
wget -O /etc/yum.repos.d/CentOS-Base.repo <https://mirrors.aliyun.com/repo/Centos-7.repo>
wget -O /etc/yum.repos.d/epel.repo <https://mirrors.aliyun.com/repo/epel-7.repo>
yum clean all && yum makecache
sudo systemctl stop firewalld
yum update -y
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo <http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo>
yum makecache fast
yum install -y docker-ce docker-ce-cli [containerd.io](http://containerd.io/)
systemctl start docker
systemctl enable docker
docker --version
yum update -y
编辑daemon.json文件
vi /etc/docker/daemon.json
编辑文件内容
sudo tee /etc/docker/daemon.json << 'EOF'
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"] // 这边修改成自己的docker的代理
}
EOF
重启docker
sudo systemctl restart docker
如果代理有权限认证需要docker login 代理地址登陆一下
docker pull ubuntu:20.04
ssh-keygen
生成对应秘钥位置
将上面的id_rsa.pub复制到linux服务器上/root,将id_rsa.pub文件内容复制到authorized_keys文件
cat id_rsa.pub >> authorized_keys
创建dockerfile文件
cat > /root/Dockerfile << 'EOF'
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y
openssh-server
&& rm -rf /var/lib/apt/lists/*
RUN mkdir -p /var/run/sshd /root/.ssh
# 复制 authorized_keys 文件
COPY authorized_keys /root/.ssh/authorized_keys
RUN chmod 700 /root/.ssh && chmod 600 /root/.ssh/authorized_keys
# 配置 SSH
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config &&
sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config &&
echo "AuthorizedKeysFile .ssh/authorized_keys" >> /etc/ssh/sshd_config
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
EOF
构建镜像
cd /root/
docker build -t my-ssh-app -f Dockerfile .
创建容器
docker run -d --name my-app
-p 2222:22
-v /root/go:/root/go
my-ssh-app
注意端口号需要和创建容器的端口号一致