利用Dockerfile創(chuàng)建一些基礎(chǔ)鏡像

在學(xué)習(xí) docker 的過程中,我們會(huì)經(jīng)常與鏡像打交道,同時(shí)也要經(jīng)常與網(wǎng)絡(luò)打交道,下載遠(yuǎn)程鏡像的快慢幾乎都是靠人品來支撐,這樣以來,學(xué)習(xí)的效率會(huì)大大降低,而且遠(yuǎn)程的一些鏡像,很多時(shí)候未必適合自己使用的需求,因此,制作一些基礎(chǔ)鏡像用于日常使用,就顯得非常必要了

Docker Github地址:https://github.com/eryajf/docker_learn

構(gòu)建 CentOS7.4

需要用到四個(gè)文件

[root@localhost centos7]$ls
aliyun-epel.repo  aliyun-mirror.repo  Dockerfile  supervisord.conf

1.Dockerfile

#
# Dockerizing CentOS7: Dockerfile for building CentOS images
#
#需要一個(gè)基礎(chǔ)鏡像,這里從國內(nèi)的daocloud下載,速度比較快。
FROM       daocloud.io/library/centos:centos7.4.1708
 
#維護(hù)者
MAINTAINER eryajf <Linuxlql@163.com>
 
#設(shè)置一個(gè)時(shí)區(qū)的環(huán)境變量
ENV TZ "Asia/Shanghai"
 
#虛擬終端
ENV TERM xterm
 
#dockerfile中有2條命令可以復(fù)制文件,1.COPY 2.ADD, ADD比COPY多2個(gè)功能,可以寫成連接 直接COPY到container,如果是壓縮文件,add能自動(dòng)解壓
ADD aliyun-mirror.repo /etc/yum.repos.d/CentOS-Base.repo
ADD aliyun-epel.repo /etc/yum.repos.d/epel.repo
 
RUN yum install -y curl wget tar bzip2 unzip vim-enhanced passwd sudo yum-utils hostname net-tools rsync man && \
    yum install -y gcc gcc-c++ git make automake cmake patch logrotate python-devel libpng-devel libjpeg-devel && \
    yum install -y --enablerepo=epel pwgen python-pip python-setuptools.noarch lrzsz ntp docker-client && \
    yum clean all
 
#配置supervisor 進(jìn)程管理工具,運(yùn)行單個(gè)進(jìn)程可以不使用
RUN easy_install supervisor && \
    mkdir -m 755 -p /etc/supervisor && \
    mkdir -m 755 /etc/supervisor/conf.d
ADD supervisord.conf /etc/supervisor/supervisord.conf
 
EXPOSE 22
 
ENTRYPOINT ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"]

2.兩個(gè) yum 源

列題一:

[root@localhost centos7]$cat aliyun-epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
 
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=http://mirrors.aliyun.com/epel/7/$basearch/debug
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
 
[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=http://mirrors.aliyun.com/epel/7/SRPMS
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0

列題二:

[root@localhost centos7]$cat aliyun-mirror.repo
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
 
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

3.supervisor 配置文件

[root@localhost centos7]$cat supervisord.conf
[unix_http_server]
file=/tmp/supervisor.sock   ; the path to the socket file
 
[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
loglevel=info                ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false               ; start in foreground if true; default false
minfds=1024                  ; min. avail startup file descriptors; default 1024
minprocs=200                 ; min. avail process descriptors;default 200
 
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
 
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
 
[include]
files = /etc/supervisor/conf.d/*.ini

開始構(gòu)建

docker build -t idocker.io/eryajf/centos:7.4 .

構(gòu)建完成查看一下:

[root@localhost ~]$docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
idocker.io/eryajf/centos   7.4                 13fb619afd8c        21 hours ago        519 MB

之后可以 push 到自己的私服當(dāng)中

docker push idocker.io/eryajf/centos:7.4

啟動(dòng)

docker run -d --name centos idocker.io/eryajf/centos:7.4

構(gòu)建 jdk 基礎(chǔ)鏡像

這個(gè)鏡像事實(shí)上在 dockerhub 上有官方發(fā)布的,只不過其底層鏡像都是基于 deebin 的,并不適合日常使用,因此這里就自己來制作一下。

需要兩個(gè)文件

[root@localhost jdk8]$ls
Dockerfile  jdk.tar.gz

1.Dockerfile

FROM       idocker.io/eryajf/centos:7.4
MAINTAINER eryajf <Linuxlql@163.com>
 
# Install jdk
ADD  jdk.tar.gz   /usr/local/
 
ENV JAVA_HOME /usr/local/jdk1.8.0_144
ENV PATH $PATH:$JAVA_HOME/bin

剩下那個(gè)是 jdk 的包,可以在官網(wǎng)進(jìn)行下載

構(gòu)建

docker build -t idocker.io/eryajf/jdk:1.8 .

啟動(dòng)驗(yàn)證

[root@localhost ~]$docker run -d --name jdk idocker.io/eryajf/jdk:1.8
17c9180d892f2406bb256113ec241843ac1e18f7e20aeb52de67ad8eaef2c724
[root@localhost ~]$docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
17c9180d892f        36a4fcd3d962        "/usr/bin/supervis..."   2 seconds ago       Up 1 second         22/tcp              jdk
[root@localhost ~]$docker exec -it jdk bash
[root@17c9180d892f /]# java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

構(gòu)建 tomcat 鏡像

需要用到如下三個(gè)文件

[root@localhost tomcat]$ls
Dockerfile  tomcat.ini  tomcat.tar.gz

1.Dockerfile

[root@localhost tomcat]$cat Dockerfile
FROM       idocker.io/eryajf/jdk:1.8
MAINTAINER eryajf <Linuxlql@163.com>
 
# Install jdk
ADD  tomcat.tar.gz   /usr/local/
ADD  tomcat.ini /etc/supervisor/conf.d

2.tomcat.ini

[root@localhost tomcat]$cat tomcat.ini
[program:tomcat]
environment=JAVA_HOME="/usr/local/jdk1.8.0_144",JAVA_BIN="/usr/local/jdk1.8.0_144/bin"
command=/usr/local/tomcat/bin/catalina.sh run
autostart=true
autorestart=true
startsecs=60
priority=1
stopasgroup=true
killasgroup=true
stderr_logfile=/usr/local/tomcat/logs/catalina.out

構(gòu)建鏡像

docker build -t idocker.io/eryajf/tomcat:8.5 .

啟動(dòng)

docker run -d -p 8080:8080 --name tomcat idocker.io/eryajf/tomcat:8.5

構(gòu)建 Jenkins 鏡像

之前使用過從 dockerhub 上下載的 Jenkins 官方發(fā)布的鏡像,然而那些鏡像的底層也都不是 centos 系統(tǒng)的,因此打算自己制作一個(gè) Jenkins 鏡像,以便于使用。

所需原料如下:

[root@localhost jenkins]$ls
Dockerfile  maven.tar.gz  ROOT.war

其中 maven 是配置好了的,ROOT.war 是 Jenkins 的包。

1.Dockerfile

[root@localhost jenkins]$cat Dockerfile
FROM       idocker.io/eryajf/tomcat:8.5
MAINTAINER eryajf <Linuxlql@163.com>
 
ADD  maven.tar.gz /usr/local/
COPY ROOT.war /usr/local/tomcat/webapps/
 
ENV JAVA_HOME /usr/local/jdk1.8.0_144
ENV MAVEN_HOME=/usr/local/maven
ENV JENKINS_HOME=/home/.jenkins_home
ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin

構(gòu)建

docker build -t idocker.io/eryajf/jenkins:2.138 .

啟動(dòng)驗(yàn)證

docker run -d -p 8080:8080 --name jenkins idocker.io/eryajf/jenkins:2.138

這種啟動(dòng)方式只能夠正常的將 Jenkins 啟動(dòng)起來,但是如果想要繼續(xù)利用 Jenkins 進(jìn)行持續(xù)集成,那么就需要將宿主機(jī)當(dāng)中的 docker命令掛載到容器當(dāng)中,這時(shí)可以使用如下命令:

docker run -d -p 8080:8080 --name jenkins -v /home/.jenkins_home:/home/.jenkins_home -v /usr/bin/docker:/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -v /etc/sysconfig/docker:/etc/sysconfig/docker  idocker.io/eryajf/jenkins:2.138

啟動(dòng)之后就可以在在這個(gè)基礎(chǔ)上使用 Jenkins 了,而且無論什么時(shí)候,就算這個(gè)容器掛掉了,只要掛載的數(shù)據(jù)目錄還在,那么隨時(shí)都可以通過如上這條指令將原有的 Jenkins 容器恢復(fù)出來。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容