實(shí)際開發(fā)環(huán)境
服務(wù)器系統(tǒng):ubuntu:14.04
web服務(wù)器:tomcat8
語言環(huán)境: jdk8
數(shù)據(jù)庫: mysql5.5
鏡像屬于 docker build的組件隅茎,主要使用Dockerfile來完成鏡像的上下文定義
創(chuàng)建OS系統(tǒng)鏡像
-
mkdir -p dockerR/baseos
創(chuàng)建文件夾 -
vim Dockerfile
創(chuàng)建打包文檔 - 編輯 Dockerfile
# Base os image
FROM ubuntu:14.04
MAINTAINER your_name <wangxunhg@gmail.com>
LABEL Description="This image is the base os images." Version="1.0"
# reconfig timezone
RUN echo "Asia/Shanghai" > /etc/timezone \
&& dpkg-reconfigure -f noninteractive tzdata
說明:
- 以#開頭的行為注釋肉微;
- 第二行FROM關(guān)鍵字告訴docker我們的新鏡像基于哪個基礎(chǔ)鏡像纹份;
- 第三行MAINTAINER關(guān)鍵字是維護(hù)者信息,可以修改為你的名字和郵箱地址;
- 第四行LABEL Description是對鏡像的一個簡單說明钱慢,Version指明該鏡像的版本號茄菊,方便維護(hù)舶胀;
- 第六行RUN關(guān)鍵字用于執(zhí)行一條命令。這里主要是修改系統(tǒng)的時區(qū)從標(biāo)準(zhǔn)UTC改為亞洲/上海网梢。
- 查看當(dāng)前鏡像
[root@bogon baseos]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
registry.cn-hangzhou.aliyuncs.com/ubuntu-14-04/ubuntu14.04 14.04 ba6d51835b6f 9 weeks ago 261.2 MB
centos 6.6 d0d663863c34 6 months ago 202.6 MB
registry.cn-hangzhou.aliyuncs.com/youdao/tomcat latest 688b918e72d1 11 months ago 538.2 MB
- 編譯打包生成鏡像
docker build -t example.com/baseos:1.0 .
[root@bogon baseos]# docker build -t example.com/baseos:1.0 .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM ba6d51835b6f
---> ba6d51835b6f
Step 1 : MAINTAINER your_name <wangxunhg@gmail.com>
---> Running in 4090fbf55855
---> d4498e75315c
Removing intermediate container 4090fbf55855
Step 2 : LABEL Description "This image is the base os images." Version "1.0"
---> Running in 9e4b30151ea3
---> 18292a807d21
Removing intermediate container 9e4b30151ea3
Step 3 : RUN echo "Asia/Shanghai" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata
---> Running in 7d0d0183e3d5
Current default time zone: 'Asia/Shanghai'
Local time is now: Fri Mar 17 15:53:00 CST 2017.
Universal Time is now: Fri Mar 17 07:53:00 UTC 2017.
---> 722a042f9a90
Removing intermediate container 7d0d0183e3d5
Successfully built 722a042f9a90
創(chuàng)建jre鏡像環(huán)境震缭,繼承OS鏡像
- 將jre打成tar包
[root@bogon baseos]# cd /root/Program/jdk1.8.0_121
[root@bogon jdk1.8.0_121]# cp -R jre/ jre_1.8.0_121/
[root@bogon jdk1.8.0_121]# tar -zcvf jre_1.8.0_121.tar.gz jre_1.8.0_121/
[root@bogon jdk1.8.0_121]# mv jre_1.8.0_121.tar.gz ~/dockerR/jre1.8/
- 生成編輯Dockerfile文件
# Base jre image
FROM example.com/baseos:1.0
MAINTAINER your_name <wangxunhg@gmail.com>
LABEL Description="This image is used to serve Oracle jre 1.8" Version="1.0"
# Install and config Oracle jre
ADD jre_1.8.0_121.tar.gz /usr/lib/jvm/
RUN update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jre_1.8.0_121/bin/java" 1 \
&& update-alternatives --set java /usr/lib/jvm/jre_1.8.0_121/bin/java
ENV JAVA_HOME /usr/lib/jvm/jre_1.8.0_121
說明:
- 繼承example.com/baseos:1.0鏡像
- ADD 相當(dāng)于COPY ,將jre_1.8.0_121.tar.gz復(fù)制到 鏡像/usr/lib/jvm/路徑下战虏。ADD與COPY不用點(diǎn):ADD會直接解壓文件拣宰。
- ENV 設(shè)置環(huán)境變量
- 編輯Dockerfile文件
[root@bogon jre1.8]# docker build -t example.com/jre:1.8 .
Sending build context to Docker daemon 73.58 MB
Sending build context to Docker daemon
Step 0 : FROM example.com/baseos:1.0
---> 722a042f9a90
Step 1 : MAINTAINER your_name <wangxunhg@gmail.com>
---> Running in 33573ed75b74
---> fbcf54e98056
Removing intermediate container 33573ed75b74
Step 2 : LABEL Description "This image is used to serve Oracle jre 1.8" Version "1.0"
---> Running in cd07f1143a40
---> 569abb36cfe0
Removing intermediate container cd07f1143a40
Step 3 : ADD jre_1.8.0_121.tar.gz /usr/lib/jvm/
---> 000c9eb92063
Removing intermediate container a1b6d96be327
Step 4 : RUN update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jre_1.8.0_121/bin/java" 1 && update-alternatives --set java /usr/lib/jvm/jre_1.8.0_121/bin/java
---> Running in ed3b862d6ee0
update-alternatives: using /usr/lib/jvm/jre_1.8.0_121/bin/java to provide /usr/bin/java (java) in auto mode
---> fd5352198db9
Removing intermediate container ed3b862d6ee0
Step 5 : ENV JAVA_HOME /usr/lib/jvm/jre_1.8.0_121
---> Running in 8b60a54c8bc2
---> 8db750272289
Removing intermediate container 8b60a54c8bc2
Successfully built 8db750272289
創(chuàng)建Tomcat鏡像,繼承jre鏡像
- 將tomcat.tar放到文件夾中
- 創(chuàng)建Dockerfile文件
# Base image
FROM example.com/jre:1.8
MAINTAINER your_name <wangxunhg@gmail.com>
LABEL Description="This image is used to run tomcat8 with jre8." Version="1.0"
ADD apache-tomcat-8.5.11.tar.gz /var/lib/
ENV CATALINA_HOME /var/lib/apache-tomcat-8.5.11
ENV PATH $CATALINA_HOME/bin:$PATH
WORKDIR $CATALINA_HOME
EXPOSE 8080
CMD ["catalina.sh", "run"]
說明:
- WORKDIR關(guān)鍵字定義了環(huán)境的工作目錄活烙,類似于Linux下的chroot徐裸。
- EXPOSE關(guān)鍵字用于暴露容器中可用的端口,注意它并不能直接將端口映射到主機(jī)啸盏,你需要使用-p選項(xiàng)來指定重贺。
- CMD關(guān)鍵字用來提供容器運(yùn)行時默認(rèn)執(zhí)行的命令。一個Dockerfile中只能有一個CMD,如果存在多個回懦,則只有最后一個會生效气笙。
- 鏡像工作空間:/var/lib/tomcat8
- 注意: 要讓一個容器保持運(yùn)行狀態(tài)而不退出,容器默認(rèn)執(zhí)行的命令需要在前臺運(yùn)行怯晕,而不是在后臺潜圃,這與我們通常在Linux環(huán)境下設(shè)置應(yīng)用在后臺運(yùn)行有所不同,如果你將容器中的應(yīng)用設(shè)置為后臺運(yùn)行的話舟茶,容器啟動完會立馬退出谭期。
- 編譯Dockerfile 文件
[root@bogon tomcat8]# docker build -t example.com/tomcat:8-jre8 .
Sending build context to Docker daemon 9.288 MB
Sending build context to Docker daemon
Step 0 : FROM example.com/jre:1.8
---> 8db750272289
Step 1 : MAINTAINER your_name <wangxunhg@gmail.com>
---> Running in a18253d2a9fe
---> 8b9c5cdf6873
Removing intermediate container a18253d2a9fe
Step 2 : LABEL Description "This image is used to run tomcat8 with jre8." Version "1.0"
---> Running in 83c9d84ba9ce
---> 0c400b40a0c3
Removing intermediate container 83c9d84ba9ce
Step 3 : ADD apache-tomcat-8.5.11.tar.gz /var/lib/
---> faae7356fb45
Removing intermediate container 6a518d1f3cd7
Step 4 : ENV CATALINA_HOME /var/lib/apache-tomcat-8.5.11
---> Running in 166c6d6fbd22
---> 19a746023a59
Removing intermediate container 166c6d6fbd22
Step 5 : ENV PATH $CATALINA_HOME/bin:$PATH
---> Running in 62fc0f4a416b
---> f88efffee71b
Removing intermediate container 62fc0f4a416b
Step 6 : WORKDIR $CATALINA_HOME
---> Running in 650459f28ad9
---> 4888813839d4
Removing intermediate container 650459f28ad9
Step 7 : EXPOSE 8080
---> Running in dca27c95faaf
---> 0bdbf5a24bab
Removing intermediate container dca27c95faaf
Step 8 : CMD catalina.sh run
---> Running in b3dd900e4e2c
---> 090a3128a888
Removing intermediate container b3dd900e4e2c
Successfully built 090a3128a888
- 啟動容器
docker run -d --name mytomcat1 -p 9988:8080 example.com/tomcat:8-jre8
![Upload Paste_Image.png failed. Please try again.]