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
# 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 nginxThe 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.
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.