Skip to main content
⚡ OpenDevDocs
Docker
devopsdockerReferenceBeginner

docker run

Create and start a new container from a Docker image with port mapping and environment variables.

Verified: 2026-09-30
Tested on:docker 27.x
Platforms:
linuxmacoswindows
#docker#containers#cli#run

Command

docker run -d --name web-app -p 3000:3000 -e NODE_ENV=production node:22-alpine


Short Description

docker run creates a writeable container layer over the specified image and executes it with provided environment variables, network port mappings, and volume mounts.


Syntax

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Examples

1. Run Background Web Server with Port Mapping

docker run -d \
  --name my-nginx \
  -p 8080:80 \
  -v $(pwd)/html:/usr/share/nginx/html:ro \
  --restart unless-stopped \
  nginx:alpine

2. Run Interactive Ephemeral Container

docker run --rm -it ubuntu:24.04 bash

Common Options

OptionShorthandDescription
--detach-dRun container in background and print container ID.
--nameAssign a custom name to the container.
--publish-p host:containerMap host port to container port (e.g. 8080:80).
--volume-v host_path:container_pathMount a host directory or named volume.
--env-e KEY=VALUEPass an environment variable.
--rmAutomatically remove container when it exits.
--restartRestart policy (no, always, unless-stopped, on-failure).

When to Use

  • Launching local services (Redis, PostgreSQL, Nginx) for development.
  • Running microservices in containerized production environments.
  • Executing one-off testing scripts inside clean isolated containers.

Warnings

On this page