Docker is good solution when you want to fast deploying your applications and run in different operating systems. You don't need to install your application's runtime anywhere. Just install Docker and run anything you want. Docker build image for you application with all dependencies inside.
In this tutorial I show you how to create new Java application and run it in Docker.
Start with creating directory for project:
mkdir myproject && cd myproject
Create Main.java
file with this code:
class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Create file with Dockerfile
name without any extension at the end and put this code:
FROM openjdk
ARG APP=/app
COPY . .
WORKDIR ${APP}
EXPOSE 8001
# This command compile your code into binary application
RUN javac Main.java
# Run it
CMD ["java", "Main"]
Start building image:
docker build --tag my-project-name .
Wait until image building complete. Progress must be like this:
user@localhost:~/myproject$ docker build --tag myproject .
[+] Building 45.7s (9/9) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 130B 0.0s
=> [internal] load metadata for docker.io/library/openjdk:latest 6.5s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 125B 0.0s
=> [1/4] FROM docker.io/library/openjdk:latest@sha256:9b448de897d211c9e0ec635a485650 36.4s
=> => resolve docker.io/library/openjdk:latest@sha256:9b448de897d211c9e0ec635a485650a 0.0s
=> => sha256:fe05457a5e9b9403f8e72eeba507ae80a4237d2d2d3f219fa62ceb128482 954B / 954B 0.0s
=> => sha256:71260f256d19f4ae5c762601e5301418d2516ca591103b1376f063be 4.46kB / 4.46kB 0.0s
=> => sha256:197c1adcd755131915cd019bdd58658d44445b3638f65449932c1 44.56MB / 44.56MB 14.3s
=> => sha256:57b698b7af4b18900b53c768746b1dfb603dfb9aec1eea328fdac8 12.26MB / 12.26MB 3.2s
=> => sha256:95a27dbe0150755fca4304b4afd0d7d6dd6a40ede6fdb30da85 188.74MB / 188.74MB 34.2s
=> => sha256:9b448de897d211c9e0ec635a485650aed6e28d4eca1efbc34940560a 1.04kB / 1.04kB 0.0s
=> => extracting sha256:197c1adcd755131915cd019bdd58658d44445b3638f65449932c18ee39b60 1.3s
=> => extracting sha256:57b698b7af4b18900b53c768746b1dfb603dfb9aec1eea328fdac86d37001 0.4s
=> => extracting sha256:95a27dbe0150755fca4304b4afd0d7d6dd6a40ede6fdb30da8568e9e8cdf2 1.9s
=> [2/4] COPY . /java 1.2s
=> [3/4] WORKDIR /java 0.1s
=> [4/4] RUN javac Main.java 1.0s
=> exporting to image 0.2s
=> => exporting layers 0.1s
=> => writing image sha256:add3eb13efdafbb7beb9a68d96ca53d870b7c6e7e302c037a35b965d70 0.0s
=> => naming to docker.io/library/myproject 0.0s
Run builded image:
docker run -p 8080:8001 -d my-project-name
Here 8080
- port of host machine, 8001 - port of container, which specified in Dockerfile at EXPOSE
instruction.
Below example of output. Program returned Hello World!
:
user@localhost:~/myproject$ docker run -p 8080:8001 -d my-project-name
Hello World!
Top comments (0)