How to Start a Docker Cntainer and Run Java Web App in It

This is the first tutorial after I have successfully installed Docker Desktop. The plan was to create a docker container with Java 17 installed on it, copy a Spring Boot application on it, start it up, and access it from the host box. And it took me a long time to learn all the steps. I am sharing these steps with the readers. Hopefully, this will be a help to the new comers on Docker containers.

The way Docker container works is that one can create a "Dockerfile". This file specifies how the container can be created, with specific user groups and user added, and copied files and directories from host to the container, and start up a specific application when the container starts.

This is my Dockerfile:

FROM openjdk:17-oracle
RUN groupadd appusers
RUN useradd -s /bin/bash -m -c "App User" -Gappusers appuser
RUN mkdir -p /app/test/
COPY ./hanbo-angular-movable-popup-1.0.1.jar /app/test/
RUN chown -R appuser:appusers /app
USER appuser
ENTRYPOINT ["java", "-jar", "/app/test/hanbo-angular-movable-popup-1.0.1.jar"]

The FROM statement specifies a Docker container template. What I needed is a container that has Oracle Java 17 installed. The RUN statements will have the active container run Linux commands. The COPY statement will copy files or directories from host into the container. This only works when the files and directories are in the same directory of the Dockerfile. If they are not in the same directory, COPY statement will fail. The USER will set the current user of the container to a specific user instead of ROOT user. Finally the ENTRYPOINT will start up the application after the container becomes active.

Once I have the Dockerfile, I can build a container based on this file. The command to do this is:

docker build -t 'hanjava17' .

This command must run in the same directory where the Dockerfile is. Also the jar file I have specified must also be in the same folder.

The output from the build command looks like this:

[+] Building 2.4s (11/11) FINISHED                                                       docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                     0.2s
 => => transferring dockerfile: 352B                                                                     0.0s
 => [internal] load metadata for docker.io/library/openjdk:17-oracle                                     0.3s
 => [internal] load .dockerignore                                                                        0.2s
 => => transferring context: 2B                                                                          0.0s
 => [1/6] FROM docker.io/library/openjdk:17-oracle@sha256:83ffa182a7cfc8313583fe1cc42172a48a021f368a1ff  0.0s
 => [internal] load build context                                                                        0.2s
 => => transferring context: 61B                                                                         0.0s
 => CACHED [2/6] RUN groupadd appusers                                                                   0.0s
 => CACHED [3/6] RUN useradd -s /bin/bash -m -c "App User" -Gappusers appuser                            0.0s
 => CACHED [5/6] COPY ./hanbo-angular-movable-popup-1.0.1.jar /app/test/                                 0.0s
 => CACHED [6/6] RUN chown -R appuser:appusers /app                                                      0.0s
 => exporting to image                                                                                   0.3s
 => => exporting layers                                                                                  0.0s
 => => writing image sha256:b6a4bff90c1b9bfa67f947c68fe380b0da8a0dc41d5c73b268892ed0f0a24c41             0.1s
 => => naming to docker.io/library/hanjava17                                                             0.1s

Next, I can run the new container with the following command:

docker run -dit  -p 8080:8080 'hanjava17'

The outut of this command is:

docker run -dit  -p 8080:8080 'hanjava17'
42801df24f078c3aafa7c038b8b115672ccfc996280730f61e1ca1020e6dbfe9

Once the container is running, I was able to see it in the Docker Desktop. I have to click "Containers" on the left side. It would show me the containers that are available. The options I used -dit will run the container in detached mode with a TTY connection to the container. And from Docker Desktop, I can use the EXEC tab to access the containers console and run command. The -p option allows the host machine's port to be connected to the port of the container. This allows the TCP communication from host port 8080 to the container's port 8080. This will allow outside access to the container through the host box. The last command line input is the Docker image name.

Once the container is running, and I can see the log in Docker Desktop that the Spring Boot application is started up successfully, now I can access the Spring Boot application with the following URL:

http://localhost:8080/

If all above steps are working as expected, I was able to see my web application in the browser. The HTTP request goes through the host box, and passed to the container. The Spring Boot application in the container would respond, and passed back through the host box to the browser.

The hard part of all these are:

  • Learn how to create the Docker image. Including how run the Linux commands, and how to copy file into the container.
  • Learn how to run the Docker image.
  • Learn how to expose the container port and connect to the host port for access.

Your Comment


Required
Required
Required

All Related Comments

Loading, please wait...
{{cmntForm.errorMsg}}
{{cmnt.guestName}} commented on {{cmnt.createDate}}.

There is no comments to this post/article.