Docker Fundamentals
Understand what containers really are, how images differ from containers, and run your first one.
A container is a lightweight, isolated process that bundles an application with everything it needs to run — libraries, dependencies, config — so it behaves the same on your laptop, a teammate’s machine, and production.
The crucial distinction:
- An image is a read-only template (a blueprint). It’s built once and never changes.
- A container is a running instance of an image. You can start many containers from the same image, like objects from a class.
Containers differ from virtual machines because they share the host’s kernel instead of each booting a full operating system. That makes them start in milliseconds and use a fraction of the resources.
graph TD D["Dockerfile"] -->|docker build| I["Image
(read-only template)"] I -->|docker run| C1["Container 1"] I -->|docker run| C2["Container 2"] I -->|docker run| C3["Container 3"] C1 --- K["Host OS Kernel"] C2 --- K C3 --- K
- Check Docker is working
Confirm the Docker engine is installed and running:
docker --version docker run hello-worldThe
hello-worldimage prints a confirmation message and exits. If you see it, your installation works. - Run an interactive container
Start an Ubuntu container and drop into its shell:
docker run -it ubuntu bashYou’re now inside a fresh Ubuntu environment. Try
cat /etc/os-release, then typeexitto leave. The container stops when its main process ends. - Run a web server in the background
Start nginx, detached (
-d), mapping container port 80 to your port 8080:docker run -d -p 8080:80 --name web nginxOpen
http://localhost:8080— you’ll see the nginx welcome page served from the container. - Inspect and clean up
List, inspect logs, then stop and remove:
docker ps # running containers docker logs web # see its output docker stop web # stop it docker rm web # remove it
A minimal Dockerfile that packages a Node.js app into an image:
FROM node:20-alpine # start from a small base image
WORKDIR /app # set the working directory
COPY package*.json ./ # copy dependency manifests first (layer caching)
RUN npm ci # install dependencies
COPY . . # copy the rest of the source
EXPOSE 3000 # document the port
CMD ["node", "server.js"] # what runs when the container startsBuild and run it:
docker build -t myapp:1.0 .
docker run -p 3000:3000 myapp:1.0 - Run
docker run -d -p 8080:80 nginx, confirm it in a browser, then stop it. - Use
docker exec -it <name> shto get a shell inside a running container. - Pull the
alpineimage and check its tiny size withdocker images. - Write a one-line
Dockerfilebased onalpinethat runsecho "hello"as its command, build it, and run it.
Cheat Sheet▾
| Task | Command |
|---|---|
| Run a container | docker run <image> |
| Run interactively | docker run -it <image> bash |
| Run detached + port map | docker run -d -p 8080:80 <image> |
| List running containers | docker ps (add -a for all) |
| Shell into a container | docker exec -it <name> sh |
| View logs | docker logs <name> |
| Stop / remove container | docker stop <name> / docker rm <name> |
| List / remove images | docker images / docker rmi <image> |
| Build an image | docker build -t name:tag . |
| Clean up everything unused | docker system prune |
Common Interview Questions▾
What's the difference between an image and a container?
An image is an immutable template built from a Dockerfile. A container is a running (or stopped) instance of an image. Many containers can run from one image.
How do containers differ from virtual machines?
Containers share the host OS kernel and isolate at the process level, so they’re lightweight and start almost instantly. VMs run a full guest OS on a hypervisor, using far more resources.
What does `-p 8080:80` mean?
It publishes a port: traffic to port 8080 on the host is forwarded to port 80
inside the container. The format is host:container.
Troubleshooting Scenarios▾
“port is already allocated”.
Another process is using that host port. Pick a different one (-p 8081:80) or
stop whatever is using it.
Container exits immediately.
Its main process finished. Containers live only as long as their primary command
runs — a web server stays up, but echo hello exits at once. Check docker logs.
“Cannot connect to the Docker daemon”.
The Docker engine isn’t running. Start Docker Desktop (or sudo systemctl start docker on Linux), and ensure your user can access the daemon.