Docker interview questions for freshers test whether you understand containers and can package an app to run anywhere. Expect questions on images vs containers, Dockerfile instructions and the commands you use every day. This page has 25 questions with short answers, a sample Dockerfile and a Compose file.
Commands assume Docker is installed and you can run docker without errors. On Linux you may need sudo or to add your user to the docker group.
How to answer Docker interview questions
Answer with the idea, the command and why it helps. "An image is a read-only template; a container is a running copy of it. docker run nginx creates a container from the nginx image, so the same app runs the same way on every machine."
Pro tip: Most Docker questions end with "write the Dockerfile". Practise one for a small Node.js, Python or Java app until you can write it from memory.
Docker basics questions
Q1. What is Docker?
30-second answer: Docker is a platform for building, sharing and running applications in containers. A container bundles the app with its libraries and settings, so it runs the same on a laptop, a test server and production. It solves the "works on my machine" problem.
Q2. What is a container?
30-second answer: A container is an isolated process running on the host's operating system kernel. It has its own file system, network and process list, but it shares the host kernel. That makes containers small and quick to start, often in a second or two.
Q3. What is the difference between a container and a virtual machine?
| Point | Container | Virtual machine |
|---|---|---|
| Includes | App and its libraries | Full guest operating system |
| Kernel | Shares the host kernel | Has its own kernel |
| Size | Usually megabytes | Usually gigabytes |
| Start time | Seconds or less | Minutes |
| Isolation | Process-level | Stronger, hardware-level |
30-second answer: A VM virtualises hardware and runs a whole OS. A container virtualises at the OS level and shares the kernel, so it is lighter and faster. VMs give stronger isolation.
Q4. What is the difference between an image and a container?
| Point | Image | Container |
|---|---|---|
| What it is | Read-only template | Running (or stopped) instance of an image |
| Created by | docker build or docker pull |
docker run or docker create |
| Changes | Never changes once built | Has a thin writable layer |
| Many from one | One image | Many containers from the same image |
30-second answer: An image is like a class and a container is like an object. You build an image once and start as many containers from it as you need.
Q5. What are image layers?
30-second answer: Each instruction in a Dockerfile that changes files, such as RUN, COPY or ADD, creates a read-only layer. Layers are cached and shared between images. When you rebuild, Docker reuses unchanged layers, so builds are faster if the instructions that change least come first.
Q6. What is Docker Hub, and what is a registry?
30-second answer: A registry stores and shares images. Docker Hub is the public default registry, with official images such as nginx, python and postgres. Companies also use private registries, such as Amazon ECR or GitHub Container Registry. You use docker pull to download and docker push to upload.
Dockerfile questions
Q7. What is a Dockerfile? Write one for a Node.js app.
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
30-second answer: A Dockerfile is a text file with instructions to build an image. This one starts from a small Node.js image, installs dependencies, copies the code and sets the start command. Copying package*.json before the rest means the npm ci layer is reused when only the code changes.
Q8. What do the main Dockerfile instructions do?
| Instruction | Does |
|---|---|
FROM |
Sets the base image |
WORKDIR |
Sets the working folder for later instructions |
COPY |
Copies files from the build context into the image |
ADD |
Like COPY, and can also extract local tar files and fetch URLs |
RUN |
Runs a command while building, such as installing packages |
ENV |
Sets an environment variable |
ARG |
Sets a variable available only during the build |
EXPOSE |
Documents the port the app listens on |
CMD |
Default command when the container starts |
ENTRYPOINT |
Main executable of the container |
30-second answer: FROM first, then set up the folder, copy files, run build steps, and end with how to start the app.
Q9. What is the difference between RUN and CMD?
30-second answer: RUN runs while the image is being built, and its result is saved in a layer. CMD runs when a container starts. RUN npm install installs packages into the image; CMD ["node", "server.js"] starts the app each time a container runs.
Q10. What is the difference between CMD and ENTRYPOINT?
30-second answer: ENTRYPOINT sets the executable that always runs. CMD sets default arguments or a default command that is easy to override. Arguments after the image name in docker run replace CMD, but are added to ENTRYPOINT.
ENTRYPOINT ["ping"]
CMD ["localhost"]
docker run myping runs ping localhost. docker run myping google.com runs ping google.com.
Q11. What is the difference between COPY and ADD?
30-second answer: COPY only copies local files and folders. ADD can also extract a local tar archive and download from a URL. Use COPY unless you need those extras, because its behaviour is clearer.
Q12. What does EXPOSE do? Does it publish the port?
30-second answer: No. EXPOSE only documents which port the app listens on inside the container. To reach it from the host, publish the port with -p when running, such as docker run -p 8080:3000 myapp.
Q13. What is a .dockerignore file?
30-second answer: It lists files and folders to leave out of the build context, like .gitignore. Typical entries are node_modules, .git, log files and .env. It makes builds faster and stops secrets from ending up in the image.
Q14. What is a multi-stage build?
FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /src
COPY . .
RUN mvn -q package -DskipTests
FROM eclipse-temurin:21-jre
COPY --from=build /src/target/app.jar /app.jar
CMD ["java", "-jar", "/app.jar"]
30-second answer: A multi-stage build uses one stage with the full build tools and copies only the result into a small final image. The final image here has the JRE and the jar, but not Maven or the source code, so it is smaller and safer.
Docker command questions
Q15. Which Docker commands do you use every day?
| Command | Does |
|---|---|
docker build -t myapp:1.0 . |
Build an image from the Dockerfile in the current folder |
docker images |
List images |
docker run -d -p 8080:3000 --name web myapp:1.0 |
Start a container in the background |
docker ps |
List running containers |
docker ps -a |
List all containers, including stopped ones |
docker logs -f web |
Follow a container's logs |
docker exec -it web sh |
Open a shell inside a running container |
docker stop web |
Stop a container |
docker rm web |
Remove a stopped container |
docker rmi myapp:1.0 |
Remove an image |
docker pull nginx |
Download an image |
30-second answer: Build, run, check with ps and logs, get inside with exec, then stop and rm.
Q16. What do the options in docker run -d -p 8080:80 --name web nginx mean?
30-second answer: -d runs the container in the background. -p 8080:80 maps port 8080 on the host to port 80 in the container, so http://localhost:8080 reaches nginx. --name web gives the container a name to use in other commands. nginx is the image.
Other common options are -e KEY=value for environment variables, -v for volumes and --rm to delete the container when it stops.
Q17. What is the difference between docker stop and docker kill?
30-second answer: docker stop sends SIGTERM so the app can shut down cleanly, then SIGKILL after a timeout (10 seconds by default). docker kill sends SIGKILL at once. Use stop normally.
Q18. What is the difference between docker exec and docker attach?
30-second answer: docker exec starts a new process in a running container, usually a shell for debugging. docker attach connects your terminal to the container's main process. Exiting an attached session can stop the container, so exec is the safer choice.
Q19. How do you clean up unused Docker objects?
30-second answer: docker container prune removes stopped containers. docker image prune removes dangling images. docker system prune removes stopped containers, unused networks, dangling images and build cache. Add -a to also remove all unused images. Read the confirmation message before agreeing.
Data, network and Compose questions
Q20. What happens to data when a container is removed?
30-second answer: Data written inside the container's writable layer is lost when the container is removed. To keep data, such as a database's files, store it in a volume or a bind mount outside the container.
Q21. What is the difference between a volume and a bind mount?
| Point | Volume | Bind mount |
|---|---|---|
| Managed by | Docker | You, any host folder |
| Example | -v pgdata:/var/lib/postgresql/data |
-v $(pwd)/src:/app/src |
| Best for | Database and app data | Live code changes during development |
| Portable | Yes | Depends on host paths |
30-second answer: Use volumes for data that must survive, and bind mounts when you want the container to see files you are editing on your machine.
Q22. How do containers talk to each other?
30-second answer: Containers on the same user-defined network can reach each other by container name. Create a network with docker network create appnet and start containers with --network appnet. Then the app can connect to the database at host db. Docker Compose creates such a network for you.
Q23. What is Docker Compose?
services:
web:
build: .
ports:
- "8080:3000"
environment:
DB_HOST: db
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
30-second answer: Compose defines several containers in one YAML file and starts them together with docker compose up -d. docker compose down stops and removes them. Here the web app reaches the database at host db, and the database keeps its data in the pgdata volume. In real projects, keep passwords out of the file using an .env file or secrets.
Q24. How do you reduce the size of an image?
30-second answer: Use a small base image such as an alpine or slim tag, use multi-stage builds, add a .dockerignore, and combine related RUN commands while cleaning package caches in the same step. Smaller images download faster and have fewer security issues.
Q25. How is Docker different from Kubernetes?
30-second answer: Docker builds images and runs containers on one machine. Kubernetes runs and manages many containers across many machines: it restarts failed ones, scales them and spreads traffic. They work together. Read Docker vs Kubernetes for a full comparison.
Quick revision table
| Topic | One line to remember |
|---|---|
| Image | Read-only template |
| Container | Running instance of an image |
| Dockerfile | Instructions to build an image |
| RUN vs CMD | Build time vs start time |
| CMD vs ENTRYPOINT | Easy-to-override default vs fixed executable |
EXPOSE vs -p |
Documents vs publishes a port |
| Volume | Data that survives the container |
| Compose | Many containers from one YAML file |
How to prepare in one week
| Day | Task |
|---|---|
| 1 | Install Docker, run nginx, learn ps, logs, stop, rm |
| 2 | Write a Dockerfile for a small app |
| 3 | Layers, caching and .dockerignore |
| 4 | Volumes and bind mounts with a database |
| 5 | Networks and Docker Compose |
| 6 | Multi-stage build and image size |
| 7 | Revise and do a mock interview |
Docker runs on Linux under the hood, so revise the Linux interview questions for freshers too. For Docker in a microservices setup, read Docker basics for microservices.

