文章目录
官网
首先第一步是设置环境,参考 链接,重要的是把所需的镜像和软件下载下来。看docker-compose-pull-images.yml
version: '3'
services:
ubuntu:
image: ubuntu:latest
busybox:
image: busybox:latest
openjdk:
image: openjdk:latest
wildfly:
image: jboss/wildfly:latest
javaee7-hol:
image: arungupta/javaee7-hol:latest
docker-javaee:
image: arungupta/docker-javaee:dockerconeu17
mysql:
image: mysql:8
hadoop:
image: tailtarget/hadoop:2.7.2
jenkins:
image: jenkins/jenkins:lts
prometheus:
image: prom/prometheus:latest
node-exporter:
image: prom/node-exporter
cadvisor:
image: google/cadvisor:latest
grafana:
image: grafana/grafana
debian:
image: debian:stable-slim
alpine:
image: alpine:3.6
openjdk-slim:
image: openjdk:9-jdk-slim
二、构建docker 镜像
docker是通过读取Dockerfile
里面的说明来构建 镜像 的。对于Dockerfile
中的完整命令列表参考 官网链接
三、创建第一个java应用的镜像
注意!不要用windows,不要用windows,不要用windows,有毒!我用win10运行下面这个命令不得行,回寝室用ubuntu就行了。
3.1 创建一个java应用
- 首先创建一个空文件夹,然后执行下面命令用于创建一个maven项目。
mvn archetype:generate -DgroupId=org.examples.java -DartifactId=helloworld -DinteractiveMode=false
- 然后构建项目
cd helloworld
mvn package
- 现在就可以验证我们的项目是否构建成功,是否可以运行
java -cp target/helloworld-1.0-SNAPSHOT.jar org.examples.java.App
运行结果:
- 运行我们的openJDK容器
docker container run -it openjdk
3.2 打包Java应用为一个镜像,并运行他
- 在helloworld目录下面新建Dockerfile文件,内容:
FROM openjdk:latest
COPY target/helloworld-1.0-SNAPSHOT.jar /usr/src/helloworld-1.0-SNAPSHOT.jar
CMD java -cp /usr/src/helloworld-1.0-SNAPSHOT.jar org.examples.java.App
- 构建我们的镜像(不要忘了 “.”)
docker image build -t hello-java:latest .
- 运行镜像,获取容器
docker container run hello-java:latest
结果:
3.3 使用Docker Maven Plugin打包和运行Java应用
完整的目标集合 链接。
新建一个空的目录,然后:
- 克隆项目
git clone https://github.com/arun-gupta/docker-java-sample/
- 创建镜像
mvn -f docker-java-sample/pom.xml package -Pdocker
- 运行容器
mvn -f docker-java-sample/pom.xml install -Pdocker
结果:
使用Docker Maven Plugin打包和运行Java应用是怎样实现的呢??就只用在pom.xml
中添加下面内容:
<profiles>
<profile>
<id>docker</id>
<build>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.22.1</version>
<configuration>
<images>
<image>
<name>hello-java</name>
<build>
<from>openjdk:latest</from>
<assembly>
<descriptorRef>artifact</descriptorRef>
</assembly>
<cmd>java -cp maven/${project.name}-${project.version}.jar org.examples.java.App</cmd>
</build>
<run>
<wait>
<log>Hello World!</log>
</wait>
</run>
</image>
</images>
</configuration>
<executions>
<execution>
<id>docker:build</id>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>docker:start</id>
<phase>install</phase>
<goals>
<goal>start</goal>
<goal>logs</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
四、Dockerfile命令设计模式
4.1 命令行 和 入口点 的区别
命令行的默认入口点是/bin/sh
,docker container run -it ubuntu
就是使用这个默认shell。入口点 允许覆盖其他命令的入口点,甚至可以自定义它,如:docker container run -it --entrypoint=/bin/cat ubuntu /etc/passwd
4.2 ADD和COPY的区别
一般情况下,我们使用COPY。然而ADD具有COPY的所有能力。并且还具有下面以下功能:
- 运行在 镜像 中的tar文件自动提取。如
ADD app.tar.gz /opt/var/myapp
- 运行从远程RUL地址下载文件。 然而,不推荐这样使用。因为,下载下来的文件会作为 镜像 的一部分,从而导致 镜像 臃肿。建议使用curl或wget明确下载存档,提取和删除存档
4.3 导入到导出镜像
导入,使用image save命令保存到.tar文件中。
docker image save helloworld > helloworld.tar
导出,使用以下load命令导入这些tar文件
docker image load -i helloworld.tar