Installing and your first container
Install Docker, understand images versus containers, and run your first container in both foreground and background.
- Start a container from an image with docker run.
- Map container ports to host ports with -p.
- Run interactively with -it and detached with -d.
- List running containers with docker ps.
In plain English
An image is a read-only package that holds an application and everything it needs to run, such as a base operating system, libraries and your code. A container is a running copy of an image with its own writable layer added on top, so many containers can start from one image without changing it. docker run starts a container from an image, downloading the image automatically from a registry if it is not on your machine yet. The -p flag maps a port inside the container to a port on your host, so a web server inside the container is reachable from your browser. Interactive mode with -it attaches your terminal to the container, while detached mode with -d runs it in the background, and docker ps lists your running containers.
Step by step
- 1Install Docker Desktop or Docker Engine and confirm it works with docker --version.
- 2Run a first container with docker run hello-world and watch Docker pull the image.
- 3Start a detached web server with docker run -d -p 8080:80 nginx:alpine.
- 4Confirm it is up with docker ps and open localhost:8080 in a browser.
- 5Enter a container interactively with docker run -it --rm node:22-alpine sh.
docker run hello-world
docker run --rm -p 8080:80 nginx:alpine
docker run -it --rm node:22-alpine sh
docker run -d --name web -p 3000:3000 my-app:1.0
docker psCommon mistakes
- Leaving out -p means the service listens only inside the container and you cannot reach it from your host.
- Running an interactive container without -it leaves you with no terminal to type into.
- Forgetting -d keeps your terminal attached to the container instead of returning to your prompt.
- Assuming docker ps shows stopped containers is wrong; it lists running ones unless you add -a.
Run a web server in the background
Start an nginx:alpine container named web, map host port 8080 to container port 80, and run it detached. Then list the running containers.
docker run nginx:alpineWhat is a container?
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Lock in what you just learned.
Mark this lesson complete to save your place and move one step closer to the certificate.