These commands are strongly associated with the Docker Container Lifecycle.

docker run

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 -t flags 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 port
docker run -p 8080:8080 windsuzu/simpleweb
 
# redirect local 1234 port to container 8080 port
docker 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.

docker create busybox echo hello
# 610ea68044541a3b4e2bac5ca889b6978e1fa529dd5c96199aeeb11b9b9b5765

docker start

The docker start simply starts the container we created with docker create, or restarts the container that was already finished and stopped.

docker start [options] container_id

Example

The following example starts the busybox container with the command echo hello. We also append an -a flag to display the output of the container.

docker start -a 610ea68044541a3b4e2bac5ca889b6978e1fa529dd5c96199aeeb11b9b9b5765
# hello

References