How to install Docker on Windows and create a Docker Hello World with Java

We will see here how to install Docker on Windows and how to create a Java hello world in Docker .

Download and install Docker

To install Docker on Windows you must first download Docker from this URL https://docs.docker.com/compose/insta

Install Docker Windows

Choosing the option Get Docker Desktop for Windows true the option to download
https://docs.docker.com/docker-for-windows/install/

Install Docker Windows

It will ask you to register with your ID and password. If you don’t have one, you must create your account.

Install Docker Windows

Once you have login you will see the button for downloading the installer.

Install Docker Windows

After starting the downloaded file, the installer will ask you if you prefer to use Docker with a Windows or Linux software. The best option is Linux. This can be changed later. Chosen this will begin the unpacking of Docker

Install Docker en Windows

Once the installation is finished, open Docker Desktop to start docker and let it run.

Start Docker

You will see the docker icon in the notification area. There you can enter with your ID and password and set general docker settings. The default options are sufficient for this example.

Docker on Windows

Now you can check your Docker installation. Open a cmd terminal, PowerShell or CMD, and check typing:

1
docker version

Docker Version

How to create a hello world with Docker

From a Windows CMD terminal run this:

1
docker run hello-world

This will download a hello world sample container from the docker hub and execute it.

Docker Contenedor

You can execute this container later like this:

docker run hello-world

Execute Docker

How to create your first docker file

Let’s see now how to create your first dockerfile. A dockerfile is a file with instructions for Docker to create a container that we can execute with the configuration and software we need. We are going to create a docker with a Linux image and run a hello world in java.

First create your HelloWorld in Java, compile and run it.

1
2
3
4
5
6
7
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Running HelloWorld into docker");
        System.out.println("Hello World Docker!!!");
        System.out.println("Bye bye!");
    }
}
1
2
javac HelloWorld.java
java HelloWorld

Execute HelloWorld Java

Then create your dockerfile file with the following instructions

1
2
3
4
FROM alpine:latest
ADD HelloWorld.class HelloWorld.class
RUN apk --update add openjdk8-jre
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "HelloWorld"]

Let’s analyze this dockerfile file: - * FROM * alpine: latest - This instruction does add an Alpine linux to our docker. Alpine linux is a linux that weighs just about 5MB - * ADD * HelloWorld.class HelloWorld.class - We add the file HelloWorld.class to our image with the same original name. - * RUN * apk –update add openjdk8-jre - Install java *, specifically an openjdk to our docker image. - * ENTRYPOINT * [“java”, “-Djava.security.egd = file: / dev /./ urandom”, “HelloWorld”] - Will execute our HelloWorld code.

How to run your first dockerfile

Now we build a dockerfile to create our image. Here we say make a dockerfile build with the name docker-hello-world and the tag (tag) latest

1
docker build --tag "docker-hello-world:latest" .

Docker Build

Now execute:

1
docker images

We will see our image created and available to be used.

Docker images

Now we can run docker with this image and see the result.

1
docker run docker-hello-world:latest

Docker Run

Conclusion

You have seen here how to install Docker and verify that it is correctly installed. You have also created your first dockerfile to run a hello world made in Java inside the container.

We have seen some basic commands: - docker build command: to create a docker image. - docker run command: to execute a docker image. - docker images command: to see the created docker images.

You can see this code in: https://github.com/gustavopeiretti/docker-hello-world-java-example https://gitlab.com/gustavopeiretti/docker-hello-world-java-example

docker