LinuxCommandLibrary

docker-run

create and start containers from images

TLDR

Run a container interactively

$ docker run -it [image] [bash]
copy
Run container in background
$ docker run -d [image]
copy
Run with port mapping
$ docker run -p [8080:80] [image]
copy
Run with volume mount
$ docker run -v [/host/path:/container/path] [image]
copy
Run with environment variables
$ docker run -e [VAR=value] [image]
copy
Run with automatic removal on exit
$ docker run --rm [image]
copy
Run with custom name
$ docker run --name [container_name] [image]
copy
Run with memory limit
$ docker run -m [512m] [image]
copy

SYNOPSIS

docker run [options] image [command] [args]

DESCRIPTION

docker run creates and starts a new container from an image. It combines docker create and docker start into a single command, providing the primary way to launch containers.
The command pulls the image if not present locally, creates a container from it, and starts execution. Options control resource allocation, networking, storage, and runtime behavior.
Containers are isolated but can connect to host resources through port mapping, volume mounts, and network configuration. The -it flags enable interactive terminal access for debugging and exploration.

PARAMETERS

-d, --detach

Run container in background.
-it
Interactive with pseudo-TTY.
-p, --publish host:container
Publish container port to host.
-v, --volume host:container
Bind mount a volume.
-e, --env VAR=value
Set environment variable.
--name name
Assign container name.
--rm
Remove container on exit.
-m, --memory limit
Memory limit (e.g., 512m, 1g).
--cpus n
Number of CPUs.
--network network
Connect to network.
--restart policy
Restart policy: no, always, unless-stopped, on-failure.
-w, --workdir dir
Working directory inside container.
-u, --user user
Username or UID.
--entrypoint cmd
Override default entrypoint.

CAVEATS

Containers run as root by default; use -u for non-root execution. Ports below 1024 require root on the host. Volumes with relative paths are created as Docker volumes, not bind mounts. Container filesystem changes are lost unless volumes are used.

HISTORY

docker run has been the fundamental Docker command since the initial release in 2013 by Solomon Hykes and dotCloud (later Docker, Inc.). The command's design draws from Unix philosophy and LXC container concepts. Options have expanded significantly to support enterprise features, orchestration, and security requirements.

SEE ALSO

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community