Running Containers

docker run

docker run creates and starts a container from an image.

OptionMeaning
-ddetached: run in the background
-p 8080:80publish container port 80 on host port 8080
--name webgive the container a name
-e KEY=valueset an environment variable
--rmdelete the container when it exits
-itinteractive terminal (for shells)

Run a Web Server

Example

Start Nginx in the background and open http://localhost:8080 in the browser:

docker run -d --name web -p 8080:80 nginx:1.27

Example output

Unable to find image 'nginx:1.27' locally
1.27: Pulling from library/nginx
Status: Downloaded newer image for nginx:1.27
3f1c9a7e5b2d8c41a0f6e2b7d9c3a1e4f5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0

Example (interactive)

Run a throwaway interactive shell in Ubuntu; --rm deletes it when you exit:

docker run -it --rm ubuntu:24.04 bash
root@4b1e2c3d:/# cat /etc/os-release | head -2
root@4b1e2c3d:/# exit

Example output

PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"