使用Docker搭建自己的开发环境

实际开发环境

服务器系统:ubuntu:14.04
web服务器:tomcat8
语言环境: jdk8
数据库: mysql5.5

镜像属于 docker build的组件,主要使用Dockerfile来完成镜像的上下文定义

创建OS系统镜像

  1. mkdir -p dockerR/baseos 创建文件夹
  2. vim Dockerfile 创建打包文档
  3. 编辑 Dockerfile
# Base os image
FROM ubuntu:14.04
MAINTAINER your_name 
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关键字告诉docker我们的新镜像基于哪个基础镜像;
  • 第三行MAINTAINER关键字是维护者信息,可以修改为你的名字和邮箱地址;
  • 第四行LABEL Description是对镜像的一个简单说明,Version指明该镜像的版本号,方便维护;
  • 第六行RUN关键字用于执行一条命令。这里主要是修改系统的时区从标准UTC改为亚洲/上海。
  1. 查看当前镜像
[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
  1. 编译打包生成镜像
    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 
 ---> 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

创建jre镜像环境,继承OS镜像

  1. 将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/
  1. 生成编辑Dockerfile文件
# Base jre image
FROM example.com/baseos:1.0
MAINTAINER your_name 
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 相当于COPY ,将jre_1.8.0_121.tar.gz复制到 镜像/usr/lib/jvm/路径下。ADD与COPY不用点:ADD会直接解压文件。
  • ENV 设置环境变量
  1. 编辑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 
 ---> 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

创建Tomcat镜像,继承jre镜像

  1. 将tomcat.tar放到文件夹中
  2. 创建Dockerfile文件
# Base image
FROM example.com/jre:1.8
MAINTAINER your_name 
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关键字定义了环境的工作目录,类似于Linux下的chroot。
  • EXPOSE关键字用于暴露容器中可用的端口,注意它并不能直接将端口映射到主机,你需要使用-p选项来指定。
  • CMD关键字用来提供容器运行时默认执行的命令。一个Dockerfile中只能有一个CMD,如果存在多个,则只有最后一个会生效。
  • 镜像工作空间:/var/lib/tomcat8
  • 注意: 要让一个容器保持运行状态而不退出,容器默认执行的命令需要在前台运行,而不是在后台,这与我们通常在Linux环境下设置应用在后台运行有所不同,如果你将容器中的应用设置为后台运行的话,容器启动完会立马退出。
  1. 编译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 
 ---> 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
  1. 启动容器
docker run -d --name mytomcat1 -p 9988:8080 example.com/tomcat:8-jre8

![Upload Paste_Image.png failed. Please try again.]

你可能感兴趣的:(使用Docker搭建自己的开发环境)