The docker run command asks the Docker server to “create” and “start” a container based on the given image. If the image is not found on the local machine, server will retrieve it from the Docker Hub first.
docker run [options] image_name [command]
Example
The following example pulls the busybox image from the Docker Hub, then creates and starts a container with it. It also specifies a shell to be started inside the container and the -i and -tflags to interact directly with the shell in the container.
docker run -it busybox sh
PS. busybox is a tiny (<5Mb) image that combines many common UNIX utilities into a single executable for crafting space-efficient distributions.
Port Mapping
Sometimes the application in our container runs or listens on a specific port. If we want to access the app from our local network and browser, we need to explicitly redirect the incoming request from the local network to the port inside the container’s network.
To achieve such port mapping, we simply add a -p flag to the docker run command, specifying local_port : container_port.
# redirect local 8080 port to container 8080 portdocker run -p 8080:8080 windsuzu/simpleweb# redirect local 1234 port to container 8080 portdocker run -p 1234:8080 windsuzu/simpleweb
docker create
The docker create command takes an image and creates a new container without running it. It then prints the container_id for further operations. You can specify a startup command for this container at the time of creation, which will be executed each time the container is started.
docker create [options] image_name [command]
Example
The following example creates a new container from the busybox image and assigns a startup command of echo hello.