jenkins学习4-进docker容器安装python3环境

2020-01-15 16:48:01 浏览数 (1)

前言

使用docker安装jenkins环境,jenkins构建的workspace目录默认是在容器里面构建的,如果我们想执行python3的代码,需进容器内部安装python3的环境。

遇到问题

构建的时候输入pip3,提示:pip3: not found

构建job后控制台输出内容

代码语言:javascript复制
Started by user admin
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/yoyoketang
[yoyoketang] $ /bin/sh -xe /tmp/jenkins2225854290036644814.sh
  pwd
/var/jenkins_home/workspace/yoyoketang
  pip3
/tmp/jenkins2225854290036644814.sh: 3: /tmp/jenkins2225854290036644814.sh: pip3: not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE

pip3: not found,说明jenkins容器内部并没有python3的环境

进jenkins容器

进docker容器, 加上-u root 以root用户权限进入容器

docker exec -it -u root 容器id /bin/bash

查看当前操作系统cat /etc/issue

代码语言:javascript复制
root@cb8e397d5308:/# uname -a
Linux cb8e397d5308 3.10.0-957.21.3.el7.x86_64 #1 SMP Tue Jun 18 16:35:19 UTC 2019 x86_64 GNU/Linux
root@cb8e397d5308:/# cat /etc/issue
Debian GNU/Linux 9 n l

这里查到的是Debian系统(不是ubuntu, 也不是centos),这里yum是没有的,所以不能用yum安装了,这里用apt-get代替yum 先执行更新apt-get

apt-get update

容器内部安装python3环境

下载python3安装包

进入容器内部, linux安装python3环境,参考这篇https://www.cnblogs.com/yoyoketang/p/10195102.html

代码语言:javascript复制
root@cb8e397d5308:/var/jenkins_home# cd /var/jenkins_home/
root@cb8e397d5308:/var/jenkins_home# mkdir python3
root@cb8e397d5308:/var/jenkins_home# cd python3/
root@cb8e397d5308:/var/jenkins_home/python3# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
root@cb8e397d5308:/var/jenkins_home/python3# tar -xvf Python-3.6.8.tgz
root@cb8e397d5308:/var/jenkins_home/python3# ls
Python-3.6.8  Python-3.6.8.tgz
root@cb8e397d5308:/var/jenkins_home/python3# cd Python-3.6.8
root@cb8e397d5308:/var/jenkins_home/python3/Python-3.6.8# ./configure --prefix=/var/jenkins_home/python3
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.6... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... linux
checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/var/jenkins_home/python3/Python-3.6.8':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

执行 ./configure --prefix=/var/jenkins_home/python3时报上面错:configure: error: no acceptable C compiler found in $PATH

apt-get 安装依赖包

这个是因为缺少gcc相关依赖包,使用apt-get代替yum安装相关依赖包

代码语言:javascript复制
apt-get -y install gcc automake autoconf libtool make
apt-get -y install make*
apt-get -y install zlib*
apt-get -y install openssl libssl-dev
apt-get install sudo

make编译安装

在/var/jenkins_home/python3/Python-3.6.8目录执行make和make install 安装

代码语言:javascript复制
./configure --prefix=/var/jenkins_home/python3 --with-ssl
make
make install

添加软链接

添加python3软链接

ln -s /var/jenkins_home/python3/bin/python3.6 /usr/bin/python3

添加pip3软链接

ln -s /var/jenkins_home/python3/bin/pip3 /usr/bin/pip3

检查环境

输入pip3 和python3检查环境

代码语言:javascript复制
root@cb8e397d5308:/var/jenkins_home/python3/Python-3.6.8# pip3

root@cb8e397d5308:/var/jenkins_home/python3/Python-3.6.8# python3
Python 3.6.8 (default, Jan  1 2020, 10:15:14)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

使用pip3安装一个requests包

pip3 install requests

ssl问题

如果pip3安装的时候遇到报ssl相关问题:pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

这个是因为缺少ssl依赖包,网上的解决方案是yum install openssl-devel ,由于Debian系统没有yum,用apt-get安装

apt-get -y install openssl libssl-dev 安装完成之后只能解决系统自带的python2对应的pip安装问题,无法解决python3的pip3安装问题。

解决办法:上面编译的时候需加上参数 —with-ssl

./configure —prefix=/var/jenkins_home/python3 —with-ssl

重新执行make和make install 就可以了

也可以在python环境检查是否能导入ssl

代码语言:javascript复制
root@cb8e397d5308:/var/jenkins_home/python3/Python-3.6.8# python3
Python 3.6.8 (default, Jan  1 2020, 10:15:14)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>

0 人点赞