Docker 容器挂载宿主机环境

  |  
阅读次数
  |  
字数 844
  |  
时长 ≈ 4 分钟

使用Docker在一台宿主机上面部署多个Java应用镜像的时候并不是将jdk软件包打入镜像,而是选择将jdk部署在每台宿主上,在运行镜像时,通过挂载目录的方式将宿主机上的java home目录挂载至容器指定目录下,从而减少镜像的臃肿与环境的重用。

此次我们使用jdk和maven来测试。

下载

我们通过官网选择相应版本包进行下载;

JDK:jdk-8u181-linux-x64.tar.gz

MAVEN:apache-maven-3.5.4-bin.tar.gz

解压

在软件包下载目录下执行解压命令:

1
2
3
tar zxvf jdk-8u181-linux-x64.tar.gz

tar zxvf apache-maven-3.5.4-bin.tar.gz

解压完成后看到文件路径如下:

1
2
3
4
5
6
7
# tidy @ TidydeMBP in ~/Documents/docker_env [15:48:44] C:130
$ ll
total 411864
drwxr-xr-x 9 tidy staff 288B 9 13 15:27 apache-maven-3.5.4
-rw-r--r--@ 1 tidy staff 8.4M 9 13 15:20 apache-maven-3.5.4-bin.tar.gz
-rw-r--r--@ 1 tidy staff 177M 9 13 15:26 jdk-8u181-linux-x64.tar.gz
drwxr-xr-x@ 15 tidy staff 480B 7 7 16:09 jdk1.8.0_181

Dockerfile

编写Dockerfile生成测试镜像:

1
2
3
4
5
6
7
8
9
10
11
12
#Dockerfile构建依赖于宿主机的jdk于maven环境
FROM centos

#设置镜像JDK、MAVEN环境变量
ENV JAVA_HOME=/usr/local/jdk
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV MAVEN_HOME /usr/local/maven
ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin

#输出JDK、MAVEN版本
CMD ["java", "-version"]
CMD ["mvn", "-v"]

构建镜像

进入Dockerfile所在目录,执行docker build命令构建镜像:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# tidy @ TidydeMBP in ~/work/vscode-workspace/docker-elk [15:26:40]
$ tree
.
├── dockerfile
│ └── java_maven
│ └── Dockerfile

# tidy @ TidydeMBP in ~/work/vscode-workspace/docker-elk [15:26:47]
$ cd dockerfile/java_maven

# tidy @ TidydeMBP in ~/work/vscode-workspace/docker-elk/dockerfile/java_maven [15:30:28]
$ docker build -t java_maven .
Sending build context to Docker daemon 2.56kB
Step 1/7 : FROM centos
---> 2d194b392dd1
Step 2/7 : ENV JAVA_HOME /usr/local/jdk
---> Running in 47137d66fd18
---> 60b1bd6626c7
Removing intermediate container 47137d66fd18
Step 3/7 : ENV CLASSPATH .:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
---> Running in a66031efc8bf
---> e0e26a2a5f14
Removing intermediate container a66031efc8bf
Step 4/7 : ENV MAVEN_HOME /usr/local/maven
---> Running in a55b5e2abf5b
---> 403c5a064733
Removing intermediate container a55b5e2abf5b
Step 5/7 : ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
---> Running in 8475db727f20
---> a5ab62e67862
Removing intermediate container 8475db727f20
Step 6/7 : CMD java -version
---> Running in 9e5f6902521f
---> 0c4c941de958
Removing intermediate container 9e5f6902521f
Step 7/7 : CMD mvn -v
---> Running in 0f5ccf06ca43
---> 294d9f33668b
Removing intermediate container 0f5ccf06ca43
Successfully built 294d9f33668b
Successfully tagged java_maven:latest

启动容器

获取到刚解压的JDK、MAVEN的HOME目录,在docker run的时候指定-v参数挂载映射目录

JDK -> ~/Documents/docker_env/jdk1.8.0_181

MAVEN -> ~/Documents/docker_env/apache-maven-3.5.4

1
2
3
4
5
6
7
# tidy @ TidydeMBP in ~/work/vscode-workspace/docker-elk/dockerfile/java_maven [15:30:49]
$ docker run -v ~/Documents/docker_env/jdk1.8.0_181:/usr/local/jdk -v ~/Documents/docker_env/apache-maven-3.5.4:/usr/local/maven --name java_maven java_maven
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T18:33:14Z)
Maven home: /usr/local/maven
Java version: 1.8.0_181, vendor: Oracle Corporation, runtime: /usr/local/jdk/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "4.9.49-moby", arch: "amd64", family: "unix"

可以看到我们创建运行容器之后,在容器里面使用命令,并成功打印出了JDK和MAVEN的版本信息。

踩坑

因为宿主机使用的是Mac,所以原本打算直接挂载Mac上面已经安装了的JDK到容器上面,后面发现docker run的时候报了cannot execute binary file错误,后知后觉,才发现是Mac上面的JDK不能在Linux上面执行。