Running Java Applications with Docker
Автор: SwitchIT
Загружено: 2024-03-06
Просмотров: 172
Описание:
This video explains steps to run a simple Java application that prints "Hello, World!" with docker or in other words steps to containerize your Java application.
1. Create a Java file named HelloWorld.java with the following content
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Compile the Java application (make sure Java is installed and available in PATH). Open terminal and run following command:
javac HelloWorld.java
3. Create a file named Dockerfile (without any file extension) in the same directory as HelloWorld.java. Add the following content to the Dockerfile:
FROM openjdk:latest
WORKDIR /app
COPY HelloWorld.class /app
CMD ["java", "HelloWorld"]
This Dockerfile instructs Docker to use the latest version of the OpenJDK image, set the working directory to /app, copy the compiled HelloWorld.class file into the /app directory inside the container, and run the java HelloWorld command when the container starts.
4. Build the Docker image:
Open your terminal or command prompt, navigate to the directory containing HelloWorld.java and Dockerfile, and run the following command to build the Docker image:
docker build -t hello-world-java .
This command builds a Docker image with the tag hello-world-java based on the Dockerfile in the current directory (.).
5. Run the Docker container:
Once the Docker image is built, you can run a container based on that image using the following command:
docker run hello-world-java
Повторяем попытку...

Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: