當前位置:首頁 > IT技術 > 編程語言 > 正文

07-docker系列-使用dockerfile構建python、jenkins鏡像
2022-02-14 10:43:58

聲明:本文乃“運維家”原創(chuàng),轉載請注明出處,更多內(nèi)容請關注公眾號“運維家”。

07-docker系列-使用dockerfile構建python、jenkins鏡像_docker


主旨

本文續(xù)接上一篇繼續(xù)使用dockerfile方式,分別構建python、jenkins鏡像。


環(huán)境

linux環(huán)境
docker環(huán)境
python3.7.1 安裝包一個,其他版本均可


下載軟件

python安裝包可以從官網(wǎng)下載,但是比較麻煩,且需要登錄,版本也比較凌亂,在這里小編提供一個3.7.1的python安裝包。關注公眾號“運維家”,后臺回復“python安裝包”即可獲取下載鏈接。


python鏡像構建

創(chuàng)建目錄,并切換至對應目錄:

[yunweijia@localhost ~]$ mkdir -pv docker/python
mkdir: 已創(chuàng)建目錄 "docker/python"
[yunweijia@localhost ~]$ cd docker/python/
[yunweijia@localhost python]$


上傳python3.7.1的軟件包:

[yunweijia@localhost python]$ pwd
/home/yunweijia/docker/python
[yunweijia@localhost python]$ ls
install.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 initscripts
cd /tmp
tar xf Python-3.7.1.tgz
cd Python-3.7.1/
./configure --prefix=/usr/local/python3 --enable-shared --with-ssl
make && make install && make clean
cd ~
rm -rf /tmp/Python-3.7.1*
mv /usr/bin/python /usr/bin/python27
ln -s /usr/local/python3/bin/python3.7 /usr/bin/python
ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip
echo "/usr/local/python3/lib" > /etc/ld.so.conf.d/python3.conf
ldconfig
[yunweijia@localhost python]$


dockerfile文件:

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


構建python鏡像:

[yunweijia@localhost python]$ sudo docker build -t yunweijia:python3 /home/yunweijia/docker/python/
# 直至出現(xiàn)如下信息
Successfully built 31255eafafc3
Successfully tagged yunweijia:python3
[yunweijia@localhost python]$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yunweijia python3 31255eafafc3 6 minutes ago 662MB
centos 7 eeb6ee3f44bd 5 months ago 204MB
[yunweijia@localhost python]$


驗證python鏡像:

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


jenkins鏡像構建

創(chuàng)建目錄,并切換至對應目錄:

[yunweijia@localhost ~]$ mkdir -pv docker/jenkins
mkdir: 已創(chuàng)建目錄 "docker/jenkins"
[yunweijia@localhost ~]$ cd docker/jenkins/
[yunweijia@localhost jenkins]$


?jenkins安裝腳本:

[yunweijia@localhost jenkins]$ vim jenkins_install.sh 
yum -y install wget gcc
yum -y install initscripts
touch /etc/yum.repos.d/jenkins.repo
cat >> /etc/yum.repos.d/jenkins.repo << EOF
[jenkins]
name=Jenkins-stable
baseurl=http://pkg.jenkins.io/redhat-stable
gpgcheck=1
EOF
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum -y upgrade
yum -y install epel-release java-11-openjdk-devel
yum -y install jenkins
[yunweijia@localhost jenkins]$


dockerfile文件:

[yunweijia@localhost jenkins]$ vim Dockerfile 
FROM centos:7
COPY jenkins_install.sh /tmp/jenkins_install.sh
RUN 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 jenkins
root 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; done
jenkins 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=20
root 83 64 0 10:46 pts/0 00:00:00 grep --color=auto jenkins
[root@c093cea8b3a4 /]# exit
exit
[yunweijia@localhost jenkins]$


從上文和本文來看,使用dockerfile構建鏡像是較為簡單的一件事兒,但是需要我們不斷的練習,至于如何在docker容器中安裝服務,和直接在宿主機安裝服務是一樣的操作。


至此,本文結束。下一篇我們介紹下docker容器的網(wǎng)絡模式。

本文摘自 :https://blog.51cto.com/u

開通會員,享受整站包年服務立即開通 >