Docker Foundations·Ship the environment with the app

Installing and your first container

Install Docker, understand images versus containers, and run your first container in both foreground and background.

Beginner 10 min read Lesson 1 of 300 Challenge included
What you will be able to do
  • 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

  1. 1Install Docker Desktop or Docker Engine and confirm it works with docker --version.
  2. 2Run a first container with docker run hello-world and watch Docker pull the image.
  3. 3Start a detached web server with docker run -d -p 8080:80 nginx:alpine.
  4. 4Confirm it is up with docker ps and open localhost:8080 in a browser.
  5. 5Enter a container interactively with docker run -it --rm node:22-alpine sh.
terminalTerminal
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 ps

Common 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.
Your turn

Run a web server in the background

Easy

Start an nginx:alpine container named web, map host port 8080 to container port 80, and run it detached. Then list the running containers.

BASH · starter
docker run nginx:alpine
Check your understanding before moving on.
Knowledge checkQuestion 1

What is a container?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Images and registries
Lesson 1 of 300 · 0% of course