Quick revision
The short answer
docker build -t orders:1.2 . builds an image from the Dockerfile in the current directory and tags it orders:1.2. docker run -d -p 8080:8080 --name orders -e SPRING_PROFILES_ACTIVE=dev orders:1.2 starts a container from it in the background, maps host port 8080 to container port 8080, names it orders and sets an environment variable. docker logs -f follows its output, docker exec -it opens a shell inside it, docker ps -a and docker image ls list containers and images, and docker rm -f stops and deletes the container while the image stays.
What to remember
In this answer, we separated images, the read-only packages built by docker build, from containers, the running instances started by docker run. We went through each run flag, used docker logs and docker exec to look inside, listed containers and images with docker ps -a and docker image ls, and saw that docker rm -f removes the container while the image stays until we remove or prune it. We also saw why docker stop is gentler than docker kill.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
docker build -t orders:1.2 . builds an image from the Dockerfile in the current directory and tags it orders:1.2. docker run -d -p 8080:8080 --name orders -e SPRING_PROFILES_ACTIVE=dev orders:1.2 starts a container from it in the background, maps host port 8080 to container port 8080, names it orders and sets an environment variable. docker logs -f follows its output, docker exec -it opens a shell inside it, docker ps -a and docker image ls list containers and images, and docker rm -f stops and deletes the container while the image stays.
Why this question is asked
Even in teams that deploy to Kubernetes, the Docker CLI is how most of us build images and check that a container works before it ever reaches a cluster. Interviewers ask about these commands to check that we understand the two core ideas behind them, images and containers, and that we can use the CLI to build, run, look inside and clean up without guessing. In this answer, we will recap images and containers, then go through the commands in the order we would use them during a normal day, explain each flag, and finish with how containers are stopped.
1A Quick Recap of Images and Containers
An image is a read-only package: a file system with our application, the Java runtime and everything else needed, plus metadata such as the command to run. It is built in layers, one per instruction in the Dockerfile, and is identified by a name and a tag, such as orders:1.2.
A container is a running (or stopped) instance of an image. Docker adds a thin writable layer on top of the image, starts the image's command as a process in an isolated environment, and gives it its own network interface. One image can start many containers. Most commands below act on either images or containers, and knowing which is half of using the CLI well.
2Building the Image
docker build reads a Dockerfile and executes its instructions to produce an image. The final dot is the build context: the directory whose files are sent to the builder and can be copied into the image with COPY. The -t flag tags the result with a name and version, so we can refer to it later.
Notice that docker image ls lists the images stored locally, with their tags and sizes. A .dockerignore file keeps large folders such as .git out of the build context, which makes builds faster.
3Running the Container and Each Flag
docker run creates a container from an image and starts it. Each flag in our command has one job. -d runs it detached, in the background, and prints the container ID instead of attaching our terminal to its output. -p 8080:8080 publishes a port in the form host:container, so a request to port 8080 on our machine reaches port 8080 inside the container; without it the application is only reachable from inside Docker's network. --name orders gives the container a fixed name, so later commands can say orders instead of a random ID, and a second container with the same name will be refused. -e SPRING_PROFILES_ACTIVE=dev sets an environment variable inside the container, which Spring Boot reads to activate the dev profile.
| Part | Meaning | Without it |
|---|---|---|
| -d | run in the background | terminal attached to output |
| -p 8080:8080 | host port -> container port | not reachable from the host |
| --name orders | fixed name for later commands | random name such as eager_curie |
| -e KEY=value | environment variable inside | default profile used |
| orders:1.2 | image to start | required |
4Looking Inside a Running Container
docker logs shows what the container's main process wrote to standard output and standard error, which is where Spring Boot logs by default. With -f it keeps following new lines, like tail -f, until we press Ctrl+C. It also works on stopped containers, which makes it the first tool when something crashed.
docker exec runs an extra command inside a running container. -i keeps standard input open and -t allocates a terminal, so docker exec -it orders sh gives us an interactive shell inside, where we can look at files, environment variables and network settings. It only works if the image contains a shell; minimal and distroless images often do not, in which case we use other tools such as a debug container.
5Listing and Cleaning Up
docker ps lists running containers. With -a it also lists stopped ones, with their exit status, which is how we find a container that crashed. docker image ls lists local images.
docker rm removes a container. A running container cannot be removed without -f, which forces it: Docker kills the process and then removes the container and its writable layer. The image is not affected, so we can start a new container from orders:1.2 immediately. Images are removed separately with docker image rm orders:1.2, or in bulk with docker image prune, which removes dangling images that have no tag.
6Follow-up questions
What is the difference between docker stop and docker kill?
docker stop sends SIGTERM to the container's main process and waits, 10 seconds by default, for it to exit; if it is still running, Docker sends SIGKILL. That gives Spring Boot time for graceful shutdown: finishing in-flight requests and closing connections. docker kill sends SIGKILL immediately by default, so the process gets no chance to clean up. Exit code 143 (128 plus 15) means it ended on SIGTERM, 137 (128 plus 9) on SIGKILL.
Why does docker stop sometimes take the full 10 seconds?
Often because the Java process is not PID 1. With a shell-form ENTRYPOINT, a shell is PID 1 and may not pass SIGTERM to Java, so nothing happens until SIGKILL. Using the exec form, ENTRYPOINT ["java", "-jar", ...], makes Java PID 1 and it receives the signal directly.
What is the difference between -p and EXPOSE?
EXPOSE in a Dockerfile only documents which port the application listens on. It does not make the port reachable from the host. -p at run time actually publishes the port, mapping a host port to a container port.
7Conclusion
In this answer, we separated images, the read-only packages built by docker build, from containers, the running instances started by docker run. We went through each run flag, used docker logs and docker exec to look inside, listed containers and images with docker ps -a and docker image ls, and saw that docker rm -f removes the container while the image stays until we remove or prune it. We also saw why docker stop is gentler than docker kill.
Open with
These commands follow the normal life of a container. docker build -t orders:1.2 . builds an image from the Dockerfile in the current directory, which is also the build context, and tags it as orders version 1.2. docker run then creates a container from that image: -d runs it in the background, -p 8080:8080 maps port 8080 on my machine to port 8080 inside the container, --name orders gives it a fixed name so I can refer to it, and -e SPRING_PROFILES_ACTIVE=dev sets an environment variable, which Spring Boot uses to activate the dev profile.
Then, if they want more
To see what it is doing I use docker logs -f orders, which follows stdout and stderr and also works after the container has stopped. docker exec -it orders sh opens an interactive shell inside the running container, assuming the image has a shell. docker ps -a lists all containers, including stopped ones with their exit codes, and docker image ls lists the images I have locally.
Finally docker rm -f orders kills and removes the container, but the image stays until I run docker image rm or docker image prune. As for stop versus kill: stop sends SIGTERM and waits ten seconds before SIGKILL, so Spring can shut down gracefully, while kill sends SIGKILL straight away.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Kubernetes documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain what these Docker commands do and when you would use each: docker build -t...": https://scrivio-interview-library.netlify.app/spring-boot/containers-and-kubernetes/8817f8d5d6/