A sample Java program compiled and executed in a Docker container.
- OpenJDK 11 or above
- Compile the Java source.
javac HelloWorld.java
- Run the Java program.
You will see the following printed out:
java HelloWorld
Hello World!
We will build a Docker image from the Dockerfile, then create a Docker container to run the Java program.
For more Dockerfile instructions, see Dockerfile reference.
- Docker Desktop or Docker Engine
- Build the Docker image from the
Dockerfile.docker build -t helloworld . - Run a Docker container from Docker image
helloworld.You will see the following printed out:docker run helloworld
Then the Docker container will stop.Hello World!
For more commands, see Docker CLI reference.
List images.
docker imagesRemove one or more images.
docker rmi <IMAGE>
# Examples
docker rmi c53678d24ebc
docker rmi helloworld:latestRemove dangling images.
docker image pruneRun a container and run the command in the container.
docker run -it IMAGE COMMAND
# Examples
docker run helloworld lsRun a container and start an interactive terminal session for the container. To exit out of the container's terminal session, type and enter exit.
docker run -it IMAGE
# Examples
docker run -it helloworldList running containers.
docker psList all containers (running and stopped).
docker ps -aRemove one or more containers.
docker rm CONTAINER
# Examples
docker rm a64c0c4ca0e9
docker rmi helloworld_containerRemove all containers (running and stopped).
docker rm $(docker ps -aq)