Thuta Learning
BasicDevOps & Toolsintermediate

docker run

Relax. We'll talk through this in plain words — no textbook voice.

docker run is the command that creates and starts a new container based on an image. It's also the command you'll run into most often in everyday Docker use.

Commonly used options

-d — run in the background

-p — map a host port to a container port

--name — give the container a name

-e — set an environment variable

--rm — auto-remove the container once it's done

dockerfile
# Run Alpine Linux and print a message
# --rm removes the container after the command finishes
docker run --rm alpine echo "hello from docker"

# Run Nginx in the background with a name
docker run --name web-demo -d -p 8080:80 nginx

The first command uses the Alpine Linux image to print a message. Because of --rm, the container gets deleted as soon as the task finishes. The second command runs the Nginx web server in the background and names it web-demo.

You should see
The terminal will show "hello from docker". For the Nginx command, you'll be able to open localhost:8080 in your browser.

Info

Once you've given the container a name, you won't have to type out the long container ID for commands like docker stop web-demo later on.

Easy traps

  • If a container with the same name already exists, you'll get a name conflict error. Either remove the existing container with docker rm, or give the new one a different name.
docker run | Thuta Learning