如安在docker容器中运行java程序

跟着Java版别的更新,现在还有许多人在运用不同版别,7,8,9,11,13乃至14。新的语法的添加也导致了一些改变。咱们的开发环境也变得更加的复杂,导致了许多集成装置软件的呈现。如:sdkman。又类似python的virtualenv,运用对应的开发环境以支撑不断更新的版别改变。docker的呈现让咱们的开发环境的结构上呈现了改变,整个将开发的方式转向了轻量级模块化,高度的灵活性。


装置Docker

  • Mac和Windows请下载Docker Desktop并装置 请参阅

创立Java代码

    public class Main {
        public static final void main(String[] args) {
            System.out.println("Hello World! It's in Docker Container");
        }
    }

编写Dockerfile 1

这儿运用openjdk,历史的原因现在openjdk和oraclejdk根本共同了。

FROM openjdk:7
COPY ./src /user/src
WORKDIR /user/src
RUN cd /user/src
RUN ls
RUN javac Main.java
CMD ["java", "Main"]

构建docker image

  • docker build -t my-java-app .

    创立一个名为custom-java-app的docker镜像。

    zzw:run_java_in_docker zzw$ tree .
    .
    ├── Dockerfile
    └── src
        └── Main.java
    1 directory, 2 files
    zzw:run_java_in_docker zzw$ docker build -t my-java-app .
    Sending build context to Docker daemon  3.584kB
    Step 1/7 : FROM openjdk:7
     ---> d735a2057e60
    Step 2/7 : COPY ./src /user/src
     ---> 2f73db0048b2
    Step 3/7 : WORKDIR /user/src
     ---> Running in 253f6ab329bc
    Removing intermediate container 253f6ab329bc
     ---> 4556a7649c61
    Step 4/7 : RUN cd /user/src
     ---> Running in 2ef0d5fc33af
    Removing intermediate container 2ef0d5fc33af
     ---> 2c3bc60d26ef
    Step 5/7 : RUN ls
     ---> Running in 35b1d1d275b7
    Main.java
    Removing intermediate container 35b1d1d275b7
     ---> b33876781109
    Step 6/7 : RUN javac Main.java
     ---> Running in 82a1391878d1
    Removing intermediate container 82a1391878d1
     ---> fba21944e1af
    Step 7/7 : CMD ["java", "Main"]
     ---> Running in 5a6d886b0b01
    Removing intermediate container 5a6d886b0b01
     ---> 48170726b7d4
    Successfully built 48170726b7d4
    Successfully tagged my-java-app:latest
    zzw:run_java_in_docker zzw$ di
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    my-java-app         latest              48170726b7d4        24 seconds ago      475MB
    

在容器中履行程序

  • docker run -it --rm --name my-running-app my-java-app

    zzw:run_java_in_docker zzw$ docker run -it --rm --name my-running-app my-java-app
    Hello World! It's in Docker Container
    

Footnotes

  1. 相关代码 ↩