📜  容器上的 docker run 命令 - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:53:37.335000             🧑  作者: Mango

Docker Run Command on Containers - Shell/Bash

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Docker containers are lightweight, portable, and self-sufficient. They can run on any machine that can run docker.

One of the most basic commands for working with Docker containers is the docker run command. This command is used to create and run a new container from a Docker image.

Basic Syntax

Here's the basic syntax for the docker run command:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • IMAGE: This is the Docker image that you want to run. It can be either the image name or the image ID.
  • COMMAND: This is the command that you want to run inside the container. If this is not specified, the default command defined in the Dockerfile will be used.
  • ARG...: These are the arguments that you want to pass to the command.
  • OPTIONS: These are various options that you can pass to the docker run command to customize its behavior.
Options

Let's take a look at some of the most commonly used options for the docker run command:

  • -d: This option is used to run the container in detached mode, which means that the container will run in the background.
  • -p: This option is used to map a container port to a host port. For example, to map port 80 of the container to port 8080 on the host, you can use the docker run -p 8080:80 command.
  • -e: This option is used to set environment variables inside the container.
  • --name: This option is used to give a name to the container.
  • --rm: This option is used to automatically remove the container when it stops running.
Examples

Here are some examples of the docker run command in action:

docker run ubuntu

This command will run a new container from the ubuntu image and start a new shell inside the container.

docker run -d -p 8080:80 nginx

This command will run a new container from the nginx image in detached mode, and map port 80 of the container to port 8080 on the host.

docker run -e MYSQL_ROOT_PASSWORD=password --name mysql mysql:latest

This command will run a new container from the mysql image, set the MYSQL_ROOT_PASSWORD environment variable to password, and give the container a name of mysql.

Conclusion

The docker run command is a very important tool for working with Docker containers. By understanding its syntax and options, you can easily create and run new containers from any Docker image.