欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

07-docker系列-使用dockerfile构建python、jenkins镜像

时间:2023-05-24

声明:本文乃“运维家”原创,转载请注明出处,更多内容请关注公众号“运维家”。

主旨

本文续接上一篇继续使用dockerfile方式,分别构建python、jenkins镜像。

环境

linux环境docker环境python3.7.1 安装包一个,其他版本均可

下载软件

python安装包可以从官网下载,但是比较麻烦,且需要登录,版本也比较凌乱,在这里小编提供一个3.7.1的python安装包。关注公众号“运维家”,后台回复“python安装包”即可获取下载链接。

python镜像构建

创建目录,并切换至对应目录:

[yunweijia@localhost ~]$ mkdir -pv docker/pythonmkdir: 已创建目录 "docker/python"[yunweijia@localhost ~]$ cd docker/python/[yunweijia@localhost python]$

上传python3.7.1的软件包:

[yunweijia@localhost python]$ pwd/home/yunweijia/docker/python[yunweijia@localhost python]$ lsinstall.sh Python-3.7.1.tgz[yunweijia@localhost python]$

python安装脚本:

[yunweijia@localhost python]$ pwd/home/yunweijia/docker/python[yunweijia@localhost python]$ vim install.sh yum -y install -y tar libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make initscriptscd /tmptar xf Python-3.7.1.tgzcd Python-3.7.1/./configure --prefix=/usr/local/python3 --enable-shared --with-sslmake && make install && make cleancd ~rm -rf /tmp/Python-3.7.1*mv /usr/bin/python /usr/bin/python27ln -s /usr/local/python3/bin/python3.7 /usr/bin/pythonln -s /usr/local/python3/bin/pip3.7 /usr/bin/pipecho "/usr/local/python3/lib" > /etc/ld.so.conf.d/python3.confldconfig[yunweijia@localhost python]$

dockerfile文件:

[yunweijia@localhost python]$ vim Dockerfile FROM centos:7COPY Python-3.7.1.tgz /tmp/Python-3.7.1.tgzCOPY install.sh /tmp/install.shRUN sh /tmp/install.sh[yunweijia@localhost python]$

构建python镜像:

[yunweijia@localhost python]$ sudo docker build -t yunweijia:python3 /home/yunweijia/docker/python/# 直至出现如下信息Successfully built 31255eafafc3Successfully tagged yunweijia:python3[yunweijia@localhost python]$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEyunweijia python3 31255eafafc3 6 minutes ago 662MBcentos 7 eeb6ee3f44bd 5 months ago 204MB[yunweijia@localhost python]$

验证python镜像:

[yunweijia@localhost python]$ sudo docker run -it yunweijia:python3 /bin/bash[root@57df1e69888d /]# python -VPython 3.7.1[root@57df1e69888d /]# pip -Vpip 10.0.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7)[root@57df1e69888d /]# exitexit[yunweijia@localhost python]$

jenkins镜像构建

创建目录,并切换至对应目录:

[yunweijia@localhost ~]$ mkdir -pv docker/jenkinsmkdir: 已创建目录 "docker/jenkins"[yunweijia@localhost ~]$ cd docker/jenkins/[yunweijia@localhost jenkins]$

jenkins安装脚本:

[yunweijia@localhost jenkins]$ vim jenkins_install.sh yum -y install wget gccyum -y install initscriptstouch /etc/yum.repos.d/jenkins.repocat >> /etc/yum.repos.d/jenkins.repo << EOF[jenkins]name=Jenkins-stablebaseurl=http://pkg.jenkins.io/redhat-stablegpgcheck=1EOFrpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.keyyum -y upgradeyum -y install epel-release java-11-openjdk-develyum -y install jenkins[yunweijia@localhost jenkins]$

dockerfile文件:

[yunweijia@localhost jenkins]$ vim Dockerfile FROM centos:7COPY jenkins_install.sh /tmp/jenkins_install.shRUN sh /tmp/jenkins_install.sh[yunweijia@localhost jenkins]$

构建jenkins镜像:

[yunweijia@localhost jenkins]$ sudo docker build -t yunweijia:jenkins /home/yunweijia/docker/jenkins/

验证jenkins镜像:

[yunweijia@localhost jenkins]$ sudo docker run -d yunweijia:jenkins /bin/bash -c "/etc/rc.d/init.d/jenkins start; while true;do echo yunweijia; sleep 5; done"c093cea8b3a43475428f022d3f4528c3ed0ef1bb4010dadb4025f3e4536a6465[yunweijia@localhost jenkins]$ [yunweijia@localhost jenkins]$ sudo docker exec -it c093cea8b3a4 /bin/bash[root@c093cea8b3a4 /]# [root@c093cea8b3a4 /]# ps -ef | grep jenkinsroot 1 0 0 10:46 ? 00:00:00 /bin/bash -c /etc/rc.d/init.d/jenkins start; while true;do echo yunweijia; sleep 5; donejenkins 11 1 69 10:46 ? 00:00:14 /etc/alternatives/java -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20root 83 64 0 10:46 pts/0 00:00:00 grep --color=auto jenkins[root@c093cea8b3a4 /]# exitexit[yunweijia@localhost jenkins]$

从上文和本文来看,使用dockerfile构建镜像是较为简单的一件事儿,但是需要我们不断的练习,至于如何在docker容器中安装服务,和直接在宿主机安装服务是一样的操作。

至此,本文结束。下一篇我们介绍下docker容器的网络模式。

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。