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

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

主旨

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

环境

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

下载软件

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

python镜像构建

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

[yunweijia@localhost ~]$ mkdir -pv docker/python
mkdir: 已创建目录 "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/
# 直至出现如下信息
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镜像构建

创建目系统运维工作内容录,并切换至对应目录:

[yunweijia@localhost ~]$ mkdir -pv docker/jenkins
mkdir: 已创建目录 "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容器中安装服务,和直接在宿主机安装服务是一样的操作。

    至此,本文结束。下一篇我们介绍下dock3c是什么意思er容器的网络模式。