Skip to content

CS160-04-Spring2024/DockerLab-HelloWorld

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Docker Lab - HelloWorld

A sample Java program compiled and executed in a Docker container.

Running on the host machine

Prerequisites

  • OpenJDK 11 or above

Build and run

  1. Compile the Java source.
    javac HelloWorld.java
  2. Run the Java program.
    java HelloWorld
    You will see the following printed out:
    Hello World!
    

Running in a Docker container

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.

Prerequisites

  • Docker Desktop or Docker Engine

Build and run

  1. Build the Docker image from the Dockerfile.
    docker build -t helloworld .
  2. Run a Docker container from Docker image helloworld.
    docker run helloworld
    You will see the following printed out:
    Hello World!
    
    Then the Docker container will stop.

Docker CLI commands

For more commands, see Docker CLI reference.

Image

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

Container

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)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published