Skip
Arish's avatar

3. Running Your First Container


Your First Container

bash
1docker run hello-world

This command:

  1. Checks for image locally
  2. Downloads from Docker Hub if missing
  3. Creates container from image
  4. Runs the container
  5. Outputs message and exits

Running Interactive Containers

bash
1# Run Ubuntu interactively
2docker run -it ubuntu bash
3
4# Inside container
5root@abc123:/# ls
6root@abc123:/# cat /etc/os-release
7root@abc123:/# exit

Running in Background

bash
1# Run Nginx web server
2docker run -d -p 8080:80 nginx
3
4# Visit http://localhost:8080

Common Flags

FlagDescription
-dDetached mode (background)
-itInteractive terminal
-pPort mapping
--nameName the container
--rmRemove after exit

Example

bash
1docker run -d --name my-nginx -p 8080:80 nginx