【centos容器配置记录】基于centos7和Python3.6的docker容器配置记录

最近需要在一个centos服务器下部署模型,我的模型之前是在windows系统上基于Python3.6开发的。我需要将该项目迁移到centos(Linux),所以我打算先在自己的window系统上配置好,代码也调试好,再远程部署。遇到了不少坑,所以记录一下。

步骤一:在window下载docker desktop。

涉及到一些wsl服务的问题,我遇到的坑和解决方案可以看我之前的记录

步骤二:拉取centos镜像。

直接拉是不行的,科学上网也没用。需要加镜像

在docker desktop找到setting--》Docker Engine--》添加

"registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com",
    "https://registry.docker-cn.com",
    "https://dockerhub.azk8s.cn"
  ]

如果还不行,再添加Google的dns服务(这个后面也有用,maybe)

"dns": [
    "8.8.8.8",
    "8.8.4.4"
  ]

设置完后,点击右下角的Apply

设置好后,在powershell或是terminal输入:

docker pull centos:7

步骤三:基于centos镜像创建容器

# 创建并进入CentOS容器,指定DNS服务器
docker run -it --name my_centos_container --dns 8.8.8.8 --dns 8.8.4.4 centos:7 /bin/bash

如果前面设置了dns,这里可以不用指定。

步骤四:使用yum安装Python3.6

方法一:用yum:

原来的正常步骤:

# 更新系统包
yum update -y

# 安装依赖项
yum install -y epel-release
yum install -y https://repo.ius.io/ius-release-el7.rpm
yum update -y

# 安装Python 3.6
yum install -y python36u python36u-devel

但会遇到报错:

yum用不了!!!!

解决方法,完全参照CentOS 7 yum无法使用解决方法Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch= - 愚生浅末 - 博客园

cd /etc/yum.repos.d

# 依次执行
cp  CentOS-Base.repo   CentOS-Base.repo.backup
vi CentOS-Base.repo

 在command模式下,dG全部删除。然后输入i进入insert模式,把下面的复制进去。按esc回到command模式。最后输入“:wq”即可报错退出,别忘了“:”。

# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#
 
[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
#baseurl=http://vault.centos.org/7.9.2009/x86_64/os/
baseurl=http://vault.centos.org/7.9.2009/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
 
#released updates 
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
#baseurl=http://vault.centos.org/7.9.2009/x86_64/os/
baseurl=http://vault.centos.org/7.9.2009/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#$baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
#baseurl=http://vault.centos.org/7.9.2009/x86_64/os/
baseurl=http://vault.centos.org/7.9.2009/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
#baseurl=http://vault.centos.org/7.9.2009/x86_64/os/
baseurl=http://vault.centos.org/7.9.2009/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

之后依次执行以下命令

sudo yum clean all
sudo yum makecache

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

cat CentOS-Base.repo

看下输出结果中是不是aliyun的镜像,是的话应该没啥问题了。

如果yum还是用不了,可以考虑源码方式安装Python。

方法二:源码安装 

这里主要参考了这篇文章:docker centos 镜像中安装python36详解!生成centos+python36的基础镜像_centos镜像加载python环境-优快云博客

依次执行以下的命令即可

mkdir -p /usr/local/python/python3
cd /usr/local
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
tar -zxvf Python-3.6.8.tgz
cd /usr/local/Python-3.6.8/
./configure --prefix=/usr/local/python/python3
make && make install

步骤五:安装pip

# 下载适用于 Python 3.6 的 get-pip.py
curl https://bootstrap.pypa.io/pip/3.6/get-pip.py -o get-pip.py

# 安装 pip
python3.6 get-pip.py

# 验证 pip 安装
pip3.6 --version

步骤六:装包

在宿主机将装包所需要的文件传入容器里。

docker cp 主机文件路径 my_container:/root/

和在window系统一样,利用requirements.txt文件,装包即可。涉及一些老版本的torch,可以在torch下载whl文件并传入容器。

pip3.6 install requirements.txt
pip3.6 install xxxxxx.whl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值