Your First Container

Use docker run to download an image and start a container in a single command.

Syntaxdocker run [options] IMAGE [command]

The docker run command is your entry point to Docker. It creates and starts a container from an image.

Running a web server

Let's run the popular Nginx web server. The command below downloads the image if needed, then starts a container.

  • -d runs the container in the background (detached).
  • -p 8080:80 maps port 8080 on your machine to port 80 in the container.
  • --name web gives the container a friendly name.

Open http://localhost:8080 in your browser and you will see the Nginx welcome page — served from inside a container.

Stopping and removing it

Use docker stop web to stop the container, and docker rm web to delete it.

Example

Example · bash
# Start Nginx in the background on port 8080
docker run -d -p 8080:80 --name web nginx

# Confirm it is running
docker ps

# Stop and remove it when done
docker stop web
docker rm web

When to use it

  • A new team member runs their first container to confirm Docker is working correctly on their machine.
  • A developer runs an Nginx container locally to prototype a web server configuration before writing a Dockerfile.
  • A trainer uses docker run with an interactive Alpine container to teach students basic Linux commands in a safe sandbox.

More examples

Run hello-world container

Pulls the hello-world image if needed and starts a container that prints a success message, then exits.

Example · bash
docker run hello-world

Run interactive Alpine shell

Starts an interactive Alpine Linux container; --rm removes it automatically when you exit the shell.

Example · bash
docker run -it --rm alpine sh
# Inside the container:
cat /etc/os-release
ls /

Run detached Nginx web server

Starts Nginx in the background and maps port 8080 on the host to port 80 in the container.

Example · bash
docker run -d --name web -p 8080:80 nginx
# Verify it is running
curl http://localhost:8080

Discussion

  • Be the first to comment on this lesson.