使用node镜像基于dockerfile自动打包成可供jdk及maven运行环境

1.首先确保docker中是否有需要的node镜像

[root@docker ~]# docker images

REPOSITORY        TAG       IMAGE ID       CREATED             SIZE

openjdk           latest    db72010b969f   2 weeks ago         467MB

jenkins/jenkins   latest    5ef12c53cfdc   2 weeks ago         571MB

nginx             latest    35c43ace9216   5 weeks ago         133MB

node              11.15.0   5b97b72da029   20 months ago       904MB

这里我用的是node-11.15.0版本。

2.编写Dockerfile执行文件(Dockerfile是用来构建镜像的文本文件,包含了构建镜像所需的指令和说明,网上有详解)

[root@docker ~]# vim Dockerfile

FROM node:11.15.0

USER root

#安装JDK

RUN mkdir /var/tmp/jdk/

COPY jdk-8u191-linux-x64.tar.gz /var/tmp/jdk/

RUN tar xzf /var/tmp/jdk/jdk-8u191-linux-x64.tar.gz -C /var/tmp/jdk

#安装maven

RUN mkdir /var/tmp/maven

COPY apache-maven-3.6.3-bin.tar.gz /var/tmp/maven/

RUN tar xzf /var/tmp/maven/apache-maven-3.6.3-bin.tar.gz -C /var/tmp/maven

#设置环境变量

ENV JAVA_HOME /var/tmp/jdk/jdk1.8.0_191

ENV M2_HOME /var/tmp/maven/apache-maven-3.6.3

ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin:$M2_HOME/bin

RUN echo 'export JAVA_HOME=/var/tmp/jdk1.8.0_191' >> /etc/profile

RUN echo 'export JAVA_BIN=/var/tmp/jdk1.8.0_191/bin' >> /etc/profile

RUN echo 'export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' >> /etc/profile

RUN echo 'export MVN_HOME=/var/tmp/apache-maven-3.6.3' >> /etc/profile

RUN echo 'export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL' >> /etc/profile

RUN echo 'export PATH=$PATH:$NGINX_HOME/sbin:$JAVA_HOME/bin:$MVN_HOME/bin' >> /etc/profile

Dockerfile

3.执行Dockerfile生成镜像

[root@docker ~]# docker build -t node:v9 .

Sending build context to Docker daemon  878.7MB

Step 1/19 : FROM node:11.15.0

 ---> 5b97b72da029

Step 2/19 : USER root

 ---> Using cache

 ---> 92bf34a03b9a

......

Step 19/19 : CMD java -version

 ---> Running in 990e39ccc8be

Removing intermediate container 990e39ccc8be

 ---> 19324e52baaa

Successfully built 19324e52baaa

Successfully tagged node:v9

执行完显示成功。

4.查看生成的镜像

[root@docker ~]# docker images

REPOSITORY        TAG       IMAGE ID       CREATED             SIZE

node              v9        19324e52baaa   48 minutes ago      1.51GB

openjdk           latest    db72010b969f   2 weeks ago         467MB

jenkins/jenkins   latest    5ef12c53cfdc   2 weeks ago         571MB

nginx             latest    35c43ace9216   5 weeks ago         133MB

node              11.15.0   5b97b72da029   20 months ago       904MB

5.进入到容器中查看jdkmaven环境是否配置成功

[root@docker ~]# docker run -it node:v9 /bin/bash

查看Java版本

root@a28cd2f5819c:/# java -version

java version "1.8.0_191"

Java(TM) SE Runtime Environment (build 1.8.0_191-b12)

Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

查看maven版本

root@a28cd2f5819c:/# mvn -v

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)

Maven home: /var/tmp/maven/apache-maven-3.6.3

Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /var/tmp/jdk/jdk1.8.0_191/jre

Default locale: en_US, platform encoding: ANSI_X3.4-1968

OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"

6.查看容器中的profile配置文件

root@0c7a6e7bbb04:/# cat /etc/profile

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))

# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

 if [ "`id -u`" -eq 0 ]; then

  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

else

  PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"

fi

export PATH

 if [ "${PS1-}" ]; then

  if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then

    # The file bash.bashrc already sets the default PS1.

    # PS1='\h:\w\$ '

    if [ -f /etc/bash.bashrc ]; then

      . /etc/bash.bashrc

    fi

  else

    if [ "`id -u`" -eq 0 ]; then

      PS1='# '

    else

      PS1='$ '

    fi

  fi

fi

 if [ -d /etc/profile.d ]; then

  for i in /etc/profile.d/*.sh; do

    if [ -r $i ]; then

      . $i

    fi

  done

  unset i

fi

export JAVA_HOME=/var/tmp/jdk1.8.0_191

export JAVA_BIN=/var/tmp/jdk1.8.0_191/bin

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

export MVN_HOME=/var/tmp/apache-maven-3.6.3

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

export PATH=$PATH:$NGINX_HOME/sbin:$JAVA_HOME/bin:$MVN_HOME/bin

Dockerfile中的内容已追加到配置文件

 7.环境配置成功退出容器

root@a28cd2f5819c:/# exit

或使用Ctrl+D快捷键

易报错点:

1. COPY指令格式

COPY 本机目录 容器目录

本机目录不能使用绝对路径,因为它本身就是一个相对路径,本机路径直接从当前目录开始,所以需要复制的文件放到当前目录

ExpCOPY test/1 /home/1

只会复制本机的1目录下所有文件,而不会创建1目录,所以后面需要指定

2.配置文件的路径要与Dockerfile中解压的指定路径相同,否则source重新加载配置文件后还是查不到版本信息

 

posted @ 2021-04-19 09:41  Joshua¥A¥  阅读(615)  评论(0编辑  收藏  举报