📜  docker run (1)

📅  最后修改于: 2023-12-03 15:30:31.925000             🧑  作者: Mango

Docker Run

"Docker run" is a command used in Docker to start an instance of a Docker container. The command is used to create and start a container from a Docker image.

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

The basic syntax for the "docker run" command consists of the Docker command, followed by a set of options, the Docker image to be used, and any additional parameters or arguments that may be required by the container or application being run.

Options
  • -d: This option runs the container in the background.

  • -p: This option maps the container port to the host port.

  • -v: This option creates a volume for the container.

  • --name: This option assigns a name to the container.

  • --env: This option sets environment variables for the container.

Image

The Docker image is the core component of the "docker run" command. It is the blueprint for the container and determines the container's behavior, configuration, and environment. Images can be pulled from Docker registries or built locally using a Dockerfile.

Command

The command is an optional parameter that specifies the command to be executed when the container is started. If no command is specified, the container will use the default command in the Docker image.

Arguments

Arguments are also optional and are used to provide additional parameters or configuration information for the container or application being run.

Examples

The following examples illustrate how to use the "docker run" command.

Example 1
docker run -it ubuntu

This command runs an Ubuntu container in interactive mode, which allows the user to interact with the container's shell.

Example 2
docker run -d -p 8080:80 nginx

This command runs an Nginx container in the background and maps the container port 80 to the host port 8080. This allows the user to access the Nginx web server running inside the container on port 8080 from the host machine.

Example 3
docker run -v /data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql

This command runs a MySQL container with a mounted volume and sets an environment variable for the MySQL root password. The mounted volume allows the user to store MySQL data outside the container, which makes it persistent even if the container is removed.

Conclusion

The "docker run" command is a fundamental command in Docker and is essential for running containers and applications. By understanding the syntax and options available, Docker users can create and configure containers that meet their specific needs.