The docker run command is used to create and start a new container from a specified Docker image. Let’s break down each part of the docker run command and explain its purpose:
Syntax:
docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]
Example:
docker run -d --name laravel_container -p 8000:80 --network laravel_network -v laravel_storage:/var/www/html/storage laravel_app
- Options:
-d, --detach: Run the container in detached mode (background) and print the container ID.--name: Assign a name to the container.-e, --env: Set environment variables (e.g.,-e VAR=value).--network: Connect the container to a network.-p, --publish: Publish a container’s port(s) to the host (e.g.,-p host_port:container_port).-v, --volume: Bind mount a volume (e.g.,-v host_path:container_path).--restart: Restart policies for containers (e.g.,--restart=always).-it, --interactive: Keep STDIN open even if not attached, allocate a pseudo-TTY.--rm: Automatically remove the container when it exits.--privileged: Give extended privileges to the container.
- IMAGE[:TAG|@DIGEST]:
IMAGE: Specifies the Docker image to use for creating the container.TAG: (Optional) Specifies the tag of the image to use. If not provided, defaults tolatest.DIGEST: (Optional) Specifies the digest of the image to use. A digest uniquely identifies the image’s content.
- COMMAND [ARG…]:
- Specifies the command to run inside the container. If not provided, the default command specified in the Dockerfile is executed.
- Description:
- The
docker runcommand creates and starts a new container based on the specified Docker image. - If the image is not available locally, Docker automatically pulls it from the configured registry (e.g., Docker Hub) before creating the container.
- The
-dor--detachoption runs the container in detached mode, meaning it runs in the background and does not hold the terminal session. - The
--nameoption assigns a name to the container, making it easier to reference and manage. - The
-eor--envoption sets environment variables inside the container. - The
--networkoption connects the container to a specified network, enabling communication between containers. - The
-por--publishoption publishes container ports to the host, allowing external access to services running inside the container. - The
-vor--volumeoption creates a bind mount, mounting a directory or file from the host into the container. - The
--restartoption specifies restart policies for the container, defining behavior when the container exits. - The
-itor--interactiveoption allows interaction with the container, keeping STDIN open and allocating a pseudo-TTY. - The
--rmoption automatically removes the container when it exits, ensuring clean-up after execution. - The
--privilegedoption gives extended privileges to the container, allowing it to perform potentially unsafe operations.
- The
Overall, the docker run command is a versatile tool for creating and managing Docker containers, providing a wide range of options for customization and configuration. It enables developers to deploy applications in isolated and reproducible environments, making it a cornerstone of containerization technology.


