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:alpine2. Run Interactive Ephemeral Container
docker run --rm -it ubuntu:24.04 bashCommon Options
| Option | Shorthand | Description |
|---|---|---|
--detach | -d | Run container in background and print container ID. |
--name | Assign a custom name to the container. | |
--publish | -p host:container | Map host port to container port (e.g. 8080:80). |
--volume | -v host_path:container_path | Mount a host directory or named volume. |
--env | -e KEY=VALUE | Pass an environment variable. |
--rm | Automatically remove container when it exits. | |
--restart | Restart 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.