Running your first container
35 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)The lab computer has no JDK and you may not install software, but Docker is there. Can you still run Java 25? Yes: one docker run command is enough. Start with the smallest container.
docker run hello-world, line by line
docker run pulls an image if needed, creates a container from it, starts the container's main program and shows its output in your terminal.
hello-world is a tiny official image whose only program prints a message and ends. In the output, the first lines come from the pull and the rest from the program.
Example 1 · docker run hello-world
Runs in: a terminal with Docker installed
docker run hello-worldUnable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
e6590344b1a5: Pull complete
Digest: sha256:ec153840d1e635ac434fab5e377081f17e0e15afab27beb3f726c3265039cfff
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/What this code does
- Lines 1 to 5 are the pull: not found locally, download the layer, check the digest (a fingerprint of the image), done. Your digest and layer id will differ.
- Steps 1 to 4 in the message are the real flow: client asks the daemon, the daemon pulls, creates a container, starts it and streams its output back.
- Then the program ends, so the container exits. It still exists as a stopped container until you remove it.
Run the same command a second time and the pull lines are gone, because the image is now stored on your computer; only a new container is created. When the program ends, the container stops.
Order of execution
What docker run does
Put the steps in the order Docker takes them.
docker commands are the same in PowerShell.A JDK without installing one: jshell in a container
Interactive mode, written -it, connects your keyboard to the program (-i) and gives it a terminal (-t), so programs such as JShell can show a prompt.
docker run -it --rm eclipse-temurin:25-jdk jshell starts JShell, Java's interactive shell, from the official JDK 25 image. Your computer needs only Docker, not a JDK.
Example 2 · JShell from the JDK 25 image
Runs in: a terminal with Docker installed
docker run -it --rm eclipse-temurin:25-jdk jshellUnable to find image 'eclipse-temurin:25-jdk' locally
25-jdk: Pulling from library/eclipse-temurin
...
Status: Downloaded newer image for eclipse-temurin:25-jdk
| Welcome to JShell -- Version 25.0.1
| For an introduction type: /help intro
jshell> int fee = 45000;
fee ==> 45000
jshell> fee * 4
$2 ==> 180000
jshell> System.getProperty("os.name")
$3 ==> "Linux"
jshell> /exit
| GoodbyeWhat this code does
- The first run downloads the JDK image (a few hundred MB); later runs start in a second or two. Your version number may differ.
os.nameisLinuxeven on Windows or macOS: the container runs Linux, inside Docker Desktop's small Linux VM.- After
/exitthe main process (jshell) ends, the container exits and--rmdeletes it.
The last word, jshell, is the command to run inside the container; anything after the image name replaces the image's default command. The three flags in front make it usable:
-i(interactive) keeps the keyboard input connected to the program.-tgives the program a terminal, so you get thejshell>prompt.--rmdeletes the container as soon as it exits, so no stopped containers pile up.
Fix the error
Fix the command
Pick the line that fixes the problem.
JShell ends at once and no prompt appears. Which line fixes it?
Error
No jshell> prompt: the program gets no keyboard input and the container exits at once.
-it, JShell sees no input, ends at once, and the container exits. Interactive programs always need -it.Running your own Java file with a bind mount
A bind mount shares a folder of your computer with the container, written -v HOST_FOLDER:CONTAINER_FOLDER. The container sees your files, and they stay on your computer.
Hello.java is an ordinary Java file. The commands below share your folder with the container and run the file from there.
Example 3 · Hello.java: a normal single-file program
public class Hello { public static void main(String[] args) { int tuition = 45000; int hostel = 30000; int library = 2500; System.out.println("Hello from ShopLite"); System.out.println("Semester fees = Rs. " + (tuition + hostel + library)); }}Hello from ShopLite Semester fees = Rs. 77500
What this code does
- A plain Java program with
main, saved asHello.javain an empty folder of your own. - Nothing in it knows about Docker. That is the point: the same file runs on your JDK and in the container.
Example 4 · Run Hello.java in the container
Runs in: a terminal with Docker installed, in the folder that holds Hello.java
lsdocker run --rm -v "$PWD":/app -w /app eclipse-temurin:25-jdk java Hello.javadocker run --rm eclipse-temurin:25-jdk java -versiondocker run --rm eclipse-temurin:25-jdk uname -sHello.java Hello from ShopLite Semester fees = Rs. 77500 openjdk version "25.0.1" 2025-10-21 LTS OpenJDK Runtime Environment Temurin-25.0.1+8 (build 25.0.1+8-LTS) OpenJDK 64-Bit Server VM Temurin-25.0.1+8 (build 25.0.1+8-LTS, mixed mode, sharing) Linux
What this code does
-v "$PWD":/appshares the current folder as/app;-w /appstarts the command there, sojava Hello.javafinds the file.- The second and third commands prove where it ran: Temurin JDK 25 on Linux, whatever is (or is not) installed on your computer. Your version line may differ.
- No
-ithere: the program reads no keyboard input, prints, and ends.
-w /app sets the working directory, the folder where the command starts, so java Hello.java runs in /app and finds the file. java Hello.java is the JDK's single-file launcher (Java 11 and newer): it compiles the file in memory and runs it.
Run the program on your computer first if you have a JDK, then in the container: the output is the same, because the code does not depend on the machine.
-v ${PWD}:/app. In Git Bash, if Docker complains about a path such as C:/Program Files/Git/app, put MSYS_NO_PATHCONV=1 before the command; Git Bash rewrote the /app path.Container lifecycle: created, running, exited
The main process is the first program a container starts. The container lives exactly as long as it: when the main process ends, the container exits.
docker create makes a container, docker start runs it, and it becomes exited when its main process ends. Watch the states with docker ps -a, then see the same idea as a Java model.
Example 5 · Watch the states with docker create, start, ps and rm
Runs in: a terminal with Docker installed, in the folder that holds Hello.java
docker create --name fee-job -v "$PWD":/app -w /app eclipse-temurin:25-jdk java Hello.javadocker ps -a --filter name=fee-job --format "table {{.Names}}\t{{.Status}}"docker start -a fee-jobdocker ps -a --filter name=fee-job --format "table {{.Names}}\t{{.Status}}"docker rm fee-job9b2e5c7f0a4d81e3c6f2a9b0d7e1c4f8a3b6d9e2f5c8a1b4d7e0f3a6c9b2e5d8 NAMES STATUS fee-job Created Hello from ShopLite Semester fees = Rs. 77500 NAMES STATUS fee-job Exited (0) 3 seconds ago fee-job
What this code does
docker createprints the new container's id and stops there: the state is Created.docker start -aruns it and attaches its output to your terminal.- After the program ends,
docker ps -ashowsExited (0). Plaindocker pslists only running containers, so it would not showfee-jobat all. --filterand--formatonly shorten the listing.docker rmdeletes the container and prints its name; the image stays.
Example 6 · A model of the lifecycle in Java
enum State { CREATED, RUNNING, EXITED }
// A simplified model: a container lives exactly as long as its main processclass ModelContainer { private final String name; private final Runnable mainProcess; private State state; private int exitCode = -1;
ModelContainer(String name, Runnable mainProcess) { this.name = name; this.mainProcess = mainProcess; this.state = State.CREATED; System.out.println(name + ": " + state); }
void start() { state = State.RUNNING; System.out.println(name + ": " + state); try { mainProcess.run(); exitCode = 0; } catch (RuntimeException e) { System.out.println(" error: " + e.getMessage()); exitCode = 1; } state = State.EXITED; System.out.println(name + ": " + state + " (" + exitCode + ")"); }}
public class LifecycleDemo { public static void main(String[] args) { ModelContainer feeJob = new ModelContainer("fee-job", () -> System.out.println(" Semester fees = Rs. " + (45000 + 30000 + 2500))); feeJob.start();
ModelContainer badJob = new ModelContainer("bad-job", () -> { throw new IllegalStateException("DB_URL is required when APP_ENV=prod"); }); badJob.start(); }}fee-job: CREATED fee-job: RUNNING Semester fees = Rs. 77500 fee-job: EXITED (0) bad-job: CREATED bad-job: RUNNING error: DB_URL is required when APP_ENV=prod bad-job: EXITED (1)
What this code does
- A simplified model, not Docker's code:
start()runs the main process and, as soon as it returns, the state becomes EXITED. Nothing keeps the container alive after that. - A main process that throws ends with exit code 1, like a real JVM after an uncaught exception, so
docker ps -awould showExited (1).
The exit code is the program's result: 0 means success, any other number means failure. hello-world and java Hello.java finish in a second, so their containers exit at once; a web server never finishes on its own, so its container keeps running until you stop it.
An exited container is not deleted. docker ps lists only running containers; docker ps -a lists all of them. docker start runs an exited container again, and docker rm deletes it.
Predict the output
Predict docker ps
Choose what Docker shows.
Round 1 of 2
The container has finished. Is demo listed by plain docker ps?
Unable to find image and manifest unknown
manifest unknown is a Docker error that means the repository exists but the tag you asked for does not, usually because of a typo.
Unable to find image NAME locally is not an error: the image is not on your computer yet, so Docker pulls it next. The real errors come after it.
$ docker run eclipse-temurin:25-jdkk java -versionUnable to find image 'eclipse-temurin:25-jdkk' locallydocker: Error response from daemon: manifest for eclipse-temurin:25-jdkk not found: manifest unknown: manifest unknown$ docker run eclipse-temurine:25-jdk java -versionUnable to find image 'eclipse-temurine:25-jdk' locallydocker: Error response from daemon: pull access denied for eclipse-temurine, repository does not exist or may require 'docker login': denied: requested access to the resource is deniedThe tag typo 25-jdkk gives manifest unknown. A wrong repository name such as eclipse-temurine gives pull access denied ... repository does not exist, which also appears when the image is private.
- Wrong tag (manifest unknown): open the image page on Docker Hub, copy a tag from its Tags list, and run again.
- Wrong name (pull access denied): check the spelling; official images have no namespace, so
eclipse-temurin, nottemurin/eclipse-temurin. - Docker prints one more line after each error that points to
docker run --help; its wording differs between Docker versions.
Next step
Check your understanding
12 quick questions