Docker run Command
Docker run Command
The docker run
command is used to create and start a new container from a specified Docker image. It is one of the most frequently used Docker commands when working with containers.
Basic Syntax
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Options
Option | Description |
---|---|
-d |
Run container in detached mode (background). |
-it |
Run container in interactive mode with a terminal. |
--name <name> |
Assign a custom name to the container. |
-p host:container |
Map a port from the host machine to the container. |
-v host:container |
Mount a volume for data persistence. |
--rm |
Automatically remove the container when it exits. |
-e KEY=VALUE |
Set environment variables inside the container. |
Examples
Run an Ubuntu container interactively
docker run -it ubuntu bash
Starts a container using the Ubuntu image and gives you a shell inside it.
Run Nginx in the background and expose port 8080
docker run -d -p 8080:80 --name webserver nginx
Runs Nginx, maps container port 80 to host port 8080, and names the container webserver
.
Run MySQL with environment variables
docker run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=root mysql
Starts a MySQL container with root password set to root
.