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 images
Remove one or more images.
docker rmi <IMAGE>
# Examples
docker rmi c53678d24ebc
docker rmi helloworld:latest
Remove dangling images.
docker image prune
Run a container and run the command in the container.
docker run -it IMAGE COMMAND
# Examples
docker run helloworld ls
Run 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 helloworld
List running containers.
docker ps
List all containers (running and stopped).
docker ps -a
Remove one or more containers.
docker rm CONTAINER
# Examples
docker rm a64c0c4ca0e9
docker rmi helloworld_container
Remove all containers (running and stopped).
docker rm $(docker ps -aq)