Your First Container
bash
1docker run hello-worldThis command:
- Checks for image locally
- Downloads from Docker Hub if missing
- Creates container from image
- Runs the container
- 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:/# exitRunning in Background
bash
1# Run Nginx web server
2docker run -d -p 8080:80 nginx
3
4# Visit http://localhost:8080Common Flags
| Flag | Description |
|---|---|
-d | Detached mode (background) |
-it | Interactive terminal |
-p | Port mapping |
--name | Name the container |
--rm | Remove after exit |
Example
bash
1docker run -d --name my-nginx -p 8080:80 nginx