点击上方「蓝字」关注testing
引言
流水账系列专辑创建啦,该专辑在计划中会记录工作或者学习中发生的问题及解决方法。 刚好前几天在学习中碰到一些问题,testing会以这些问题作为该专辑的开篇,后续会不定时的更新该系列! 这几天,testing都在自己部署自动化的环境,使用docker进行自动化持续集成。在自动化持续集成的过程中,使用Jenkins构建项目的时候发现容器和原生Linux一样都是自带python2.*.*的环境,而testing的脚本是需要python3环境的。特此记录如何在Docker容器中安装Python3环境。遇到问题
在Jenkins流水线构建时,testing中有一部分代码是这样的: 在构建到pip3的时候Blue Ocean日志却提示了:pip3: not found。这时候testing才想起来还没有安装python3的环境。 下面说下如何在Docker容器内安装python3。容器内部安装环境
# 打开xshell,连接服务器# 得到所有正在运行的容器信息[root@iZbp133r1a20f20uajfspoZ ~]# docker ps\CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9433531375d6 jenkins/jenkins "/sbin/tini -- /usr/…" 2 days ago Up 24 hours 50000/tcp, 0.0.0.0:8081->8080/tcp objective_euclid# 以管理员的方式进入Docker容器[root@iZbp133r1a20f20uajfspoZ ~]# docker exec -it -u root 9433531375d6 /bin/bash# 查看当前操作系统root@9433531375d6:/# cat /etc/issueDebian GNU/Linux 9 \n \l
这里查到的是Debian系统,即不是ubuntu, 也不是centos。这里yum是没有的,因此不能用yum安装。这里可以用apt-get代替yum。
先执行更新apt-get,因为testing安装过了,下面的就不贴结果了!
# 更新apt-getroot@9433531375d6:/# apt-get updateapt-get updateroot@9433531375d6:/# cd /var/jenkins_home/root@9433531375d6:/var/jenkins_home# mkdir python3root@9433531375d6:/var/jenkins_home# cd python3/# 下载python包root@9433531375d6:/var/jenkins_home/python3# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz# 解压root@9433531375d6:/var/jenkins_home/python3# tar -xvf Python-3.6.8.tgzroot@9433531375d6:/var/jenkins_home/python3# lsPython-3.6.8 Python-3.6.8.tgzroot@9433531375d6:/var/jenkins_home/python3# cd Python-3.6.8# 安装root@9433531375d6:/var/jenkins_home/python3/Python-3.6.8# ./configure --prefix=/var/jenkins_home/python3
注意:在安装的时候可能会报:configure: error: no acceptable C compiler found in $PATH。这是因为缺少gcc相关依赖包引起的,使用apt-get代替yum安装相关依赖包即可。
root@9433531375d6:/var/jenkins_home/python3/Python-3.6.8# apt-get -y install gcc automake autoconf libtool makeroot@9433531375d6:/var/jenkins_home/python3/Python-3.6.8# apt-get -y install make*root@9433531375d6:/var/jenkins_home/python3/Python-3.6.8# apt-get -y install zlib*root@9433531375d6:/var/jenkins_home/python3/Python-3.6.8# apt-get -y install openssl libssl-devroot@9433531375d6:/var/jenkins_home/python3/Python-3.6.8# apt-get install sudo
安装好后重新运行:
root@9433531375d6:/var/jenkins_home/python3/Python-3.6.8# ./configure --prefix=/var/jenkins_home/python3
安装成功后,需要在make、make install。
root@9433531375d6:/var/jenkins_home/python3/Python-3.6.8# makeroot@9433531375d6:/var/jenkins_home/python3/Python-3.6.8# make install
最后,添加python3及pip3的软连接即可。
root@9433531375d6:/var/jenkins_home/python3/Python-3.6.8# ln -s /var/jenkins_home/python3/bin/python3.6 /usr/bin/python3root@9433531375d6:/var/jenkins_home/python3/Python-3.6.8# ln -s /var/jenkins_home/python3/bin/pip3 /usr/bin/pip3
到这,在Docker容器内部安装Python3环境就完成了,我们来看看效果。
root@9433531375d6:/var/jenkins_home# python3Python 3.6.8 (default, Aug 31 2020, 10:47:39) [GCC 6.3.0 20170516] on linuxType "help", "copyright", "credits" or "license" for more information.>>> exit()root@9433531375d6:/var/jenkins_home# pip3Usage: pip3 [options]Commands: install Install packages. download Download packages. uninstall Uninstall packages. freeze Output installed packages in requirements format. list List installed packages. show Show information about installed packages. check Verify installed packages have compatible dependencies. config Manage local and global configuration. search Search PyPI for packages. wheel Build wheels from your requirements. hash Compute hashes of package archives. completion A helper command used for command completion. help Show help for commands.General Options: -h, --help Show help. --isolated Run pip in an isolated mode, ignoring environment variables and user configuration. -v, --verbose Give more output. Option is additive, and can be used up to 3 times. -V, --version Show version and exit. -q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels). --log Path to a verbose appending log. --proxy Specify a proxy in the form [user:passwd@]proxy.server:port. --retries Maximum number of retries each connection should attempt (default 5 times). --timeout Set the socket timeout (default 15 seconds). --exists-action Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort). --trusted-host Mark this host as trusted, even though it does not have valid or any HTTPS. --cert Path to alternate CA bundle. --client-cert Path to SSL client certificate, a single file containing the private key and the certificate in PEM format. --cache-dir
Store the cache data in
. --no-cache-dir Disable the cache. --disable-pip-version-check Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index. --no-color Suppress colored output