Create a docker image that runs a simple Java app

things to do

To understand Docker, let's create and run a docker image of a very simple application. Specifically, run an application that outputs "Hello world!" Once a second. Aim to do everything from build to execution on the container.

The Java source code is as follows. https://github.com/nannany/very-simple-application

environment

I ran it on Windows10 HOME. I used Docker Toolbox to create the operating environment for Docker on Windows. See the following article for details. https://qiita.com/idani/items/fb7681d79eeb48c05144

Flow of creating docker image

Write Dockerfile → Execute docker build command The docker image is created with.

The rough flow of docker image creation looks like the figure below. As a character to be aware of,

Decide which path to add files to the build context when executing the docker build command. At this time, describe the files that you do not want to bring to the build context in the .dockerignore file.

Also, the COPY command in the Dockerfile determines what to bring to the image in the build context.

ビルドコンテキスト.jpg

Dockerfile to use

As a whole, it is as follows.

FROM ubuntu:disco

COPY . .
RUN apt-get update && apt-get install -y \
    maven \
    openjdk-8-jre \
 && cd simple \
 && mvn package
CMD ["java","-jar","simple/target/simple-1.0-SNAPSHOT.jar"]

First, write FROM to select the base image. Here, select ʻubuntu: disco` as appropriate.

Then write COPY .. to copy the file from the build context to the image.

Next, install the packages (maven and openjdk) required to build the source and execute Java, and execute the maven jar creation command.

RUN apt-get update && apt-get install -y \
    maven \
    openjdk-8-jre \
 && cd simple \
 && mvn package

The way of writing is to imitate the following, and I was conscious of minimizing the number of layers and doing apt-get update and install at the same time. http://docs.docker.jp/engine/articles/dockerfile_best-practice.html

Finally, I wrote the following so that java -jar simple / target / simple-1.0-SNAPSHOT.jar is executed after the container is started.

CMD ["java","-jar","simple/target/simple-1.0-SNAPSHOT.jar"]

Docker command to run at build time

The docker command to be executed when creating an image is

docker build -t simple-application -f Dockerfile.cmd .

In -t simple-application, the name of the image is simple-application. In -f Dockerfile.cmd, the Dockerfile used when creating the image is Dockerfile.cmd in the path where the above command is executed. (By default, the Dockerfile in the path where you are executing the command is selected) The final . Means that the path under which the command is being executed will be added to the build context.

move

Let's operate the image created above with the following command.

docker run simple-application-cmd

It was displayed as below and it worked.

hello.gif

Image with maven

Above, I specified ʻubuntu: disco` as the base image and installed maven and Java on the image with RUN. However, since there is a base image that originally contains maven and Java, the Dockerfile that uses it is as follows. (I skipped it because I got an error in the test for some reason)

FROM maven:3-jdk-8

COPY . .
RUN cd simple && mvn package -Dmaven.test.skip=true
CMD ["java","-jar","simple/target/simple-1.0-SNAPSHOT.jar"]

Recommended Posts

Create a docker image that runs a simple Java app
Creating a lightweight Java environment that runs on Docker
Create a lightweight STNS Docker image
Docker × Java Building a development environment that is too simple
Create a TODO app in Java 7 Create Header
Docker Compact Manual (4: Create a custom image)
Consolidate your JavaFX app into a jar that runs on both Java 8/11
Create a simple search app with Spring Boot
Create a simple bulletin board with Java + MySQL
Create a Lambda Container Image based on Java 15
CICS-Run Java application-(1) Run a simple sample app
[Java] Create a filter
Create a portfolio app using Java and Spring Boot
A simple CRUD app made with Nuxt / Laravel (Docker)
Practice making a simple chat app with Docker + Sinatra
[Note] Create a java environment from scratch with docker
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
Create a java method [Memo] [java11]
[Java] Create a temporary file
[Java] Draw a simple pattern
How to create a lightweight container image for Java apps
Create a Docker image with the Oracle JDK installed (yum
How to deploy a simple Java Servlet app on Heroku
[Beginner] android app that rolls a ball using a sensor [Java]
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Create a simple web server with the Java standard library com.sun.net.httpserver
Create a Vue3 environment with Docker!
I tried to create a simple map app in Android Studio
3 Implement a simple interpreter in Java
I made a shopify app @java
Create a new app in Rails
Create a Java project using Eclipse
[Java] How to create a folder
I created a Selenium sample app that supports multiple browsers (Chrome, IE, Firefox) that runs in Java.
A simple sample callback in Java
2. Create Docker image and register Registry
Try to create a server-client app
Use Jenkins to build inside Docker and then create a Docker image.
Create a Docker Image for redoc-cli and register it on Docker Hub
A standalone Java app that sends logs to CloudWatch Logs with slf4j / logback
How to quickly create a reverse proxy that supports HTTPS with Docker
Clone your own web app on GitLab when building a Docker image
A shell script that builds a Docker image and pushes it to ECR
Steps to create a simple camel app using Apache Camel Spring Boot starters
Create a java web application development environment with docker for mac part2
[Java] Create and apply a slide master
Create a web environment quickly using Docker
[Rails6] Create a new app with Rails [Beginner]
Create a simple web application with Dropwizard
Create a simple on-demand batch with Spring Batch
[Rails withdrawal] Create a simple withdrawal function with rails
Create a MySQL environment with Docker from 0-> 1
Let's create a Java development environment (updating)
Create a simple bar chart with MPAndroidChart
[docker] [nginx] Make a simple ALB with nginx
Try making a calculator app in Java
[Rails] Let's create a super simple Rails API
Deploy a Java web app on Heroku
[Rails 5] Create a new app with Rails [Beginner]
Create a List that holds multiple classes
Install Docker and create Java runtime environment