1.首先确保docker中是否有需要的node镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
node v5 19324e52baaa 19 minutes ago 1.51GB
node v4 3822b318543b 3 hours ago 1.51GB
openjdk v3 3bda6baa6ad3 4 hours ago 488MB
node v3 6dcc49df843e 26 hours ago 958MB
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 3/19 : RUN mkdir /var/tmp/jdk/
---> Using cache
---> eb8e3a1de0e8
Step 4/19 : COPY jdk-8u191-linux-x64.tar.gz /var/tmp/jdk/
---> Using cache
---> 1dc01b850891
Step 5/19 : RUN tar xzf /var/tmp/jdk/jdk-8u191-linux-x64.tar.gz -C /var/tmp/jdk
---> Running in 4717d0c40e5c
Removing intermediate container 4717d0c40e5c
---> 3e6b020edb22
Step 6/19 : RUN mkdir /var/tmp/maven
---> Running in 95f9bba903a9
Removing intermediate container 95f9bba903a9
---> c13c405247cb
Step 7/19 : COPY apache-maven-3.6.3-bin.tar.gz /var/tmp/maven/
---> d78abb3d4476
Step 8/19 : RUN tar xzf /var/tmp/maven/apache-maven-3.6.3-bin.tar.gz -C /var/tmp/maven
---> Running in 5f18b572e61a
Removing intermediate container 5f18b572e61a
---> 8acc19c2b928
Step 9/19 : ENV JAVA_HOME /var/tmp/jdk/jdk1.8.0_191
---> Running in a58594d86f9b
Removing intermediate container a58594d86f9b
---> 191c4a506514
Step 10/19 : ENV M2_HOME /var/tmp/maven/apache-maven-3.6.3
---> Running in 7ec931c7c0bc
Removing intermediate container 7ec931c7c0bc
---> ca44e210bffb
Step 11/19 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin:$M2_HOME/bin
---> Running in ad814776f208
Removing intermediate container ad814776f208
---> 4254da22ed79
Step 12/19 : RUN echo 'export JAVA_HOME=/var/tmp/jdk1.8.0_191' >> /etc/profile
---> Running in a666351d08e7
Removing intermediate container a666351d08e7
---> 1260e35852df
Step 13/19 : RUN echo 'export JAVA_BIN=/var/tmp/jdk1.8.0_191/bin' >> /etc/profile
---> Running in 5ccd9fe03edb
Removing intermediate container 5ccd9fe03edb
---> 41eeaa763e91
Step 14/19 : RUN echo 'export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' >> /etc/profile
---> Running in 2ff900bd0e0e
Removing intermediate container 2ff900bd0e0e
---> 5092b8916d61
Step 15/19 : RUN echo 'export MVN_HOME=/var/tmp/apache-maven-3.6.3' >> /etc/profile
---> Running in cdb149501028
Removing intermediate container cdb149501028
---> 10abcbe9327c
Step 16/19 : RUN echo 'export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL' >> /etc/profile
---> Running in d9ef883d417b
Removing intermediate container d9ef883d417b
---> 872e166a7668
Step 17/19 : RUN echo 'export PATH=$PATH:$NGINX_HOME/sbin:$JAVA_HOME/bin:$MVN_HOME/bin' >> /etc/profile
---> Running in 73529414e90c
Removing intermediate container 73529414e90c
---> c518c746cfd6
Step 18/19 : CMD mvn -version
---> Running in bca5f53521cc
Removing intermediate container bca5f53521cc
---> bd3daccd93f4
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
node v5 aee8be290eae 2 hours ago 1.51GB
node v4 3822b318543b 3 hours ago 1.51GB
openjdk v3 3bda6baa6ad3 4 hours ago 488MB
node v3 6dcc49df843e 27 hours ago 958MB
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
- 进入到容器中查看jdk和maven环境是否配置成功
[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快捷键
易报错点:
- COPY指令格式
COPY 本机目录 容器目录
本机目录不能使用绝对路径,因为它本身就是一个相对路径,本机路径直接从当前目录开始,所以需要复制的文件放到当前目录
Exp:COPY test/1 /home/1
只会复制本机的1目录下所有文件,而不会创建1目录,所以后面需要指定
- 配置文件的路径要与Dockerfile中解压的指定路径相同,否则source重新加载配置文件后还是查不到版本信息