简介:Docker run command is the most essential command in Docker, which is used to create and run a container. This article will introduce you to the basic syntax and usage of the Docker run command.
The Docker run command is used to create and run a container. It is the most essential command in Docker. Here is the basic syntax of the Docker run command:
docker run [options] image-name [command] [arg...]
In this command, the image-name is the name of the Docker image you want to use to create the container. You can use your local machine’s Docker image or pull an image from online repositories such as Docker Hub or Quay.io.
Here are some common options you can use with the Docker run command:
-d
: Run the container in the background.-it
: Provide an interactive terminal.-p
: Publish a container’s port to the host.-v
: Share data between the container and host.This command will create and run a container in the background using the specified image-name.
docker run -d image-name
This command will create and run a container interactively, providing an interactive terminal. After the container starts, it will execute
docker run -it image-name /bin/bash
/bin/bash
to enter the container’s command prompt.This command will publish the container’s port 80 to host port 8080. You can access the service running inside the container through the host’s port 8080.
docker run -p 8080:80 image-name
This command will share
docker run -v /host/directory:/container/directory image-name
/host/directory
on the host with /container/directory
in the container. This allows you to persist data between container runs or share data between the container and host.image-name
in the Docker run command, and you can also provide additional arguments or options according to your needs. The Docker run command provides a flexible way to create and run containers, allowing you to customize your container’s behavior and environment.