The -p (or --publish) flag in docker run is used when you want to map a port on your host
machine to a port inside the container. This is necessary if you want to access a service
running inside the container from outside (e.g., browser, curl, Postman, ).
Code : docker container run -p <host_port>:<container_port> <image_name>
Example1 : docker container run -d -p 8080:80 nginx
Example2 : docker container run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql
Example3 : docker container run -it -p 8888:8888 jupyter/base-notebook
----------------------------------------------------------------------------------------------------------------------
-e = environment variable
• Sets an environment variable inside the container.
• Useful for configuration.
• Example:
• docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=secret mysql
→ Sets MYSQL_ROOT_PASSWORD=secret so MySQL knows the root password.
-----------------------------------------------------------------------------------------------------------------------------
- -it = interactive + terminal
• Combines two flags:
o -i → interactive (keeps STDIN open).
o -t → pseudo-TTY (gives you a shell-like terminal).
• Useful when you want to interact with the container.
• Example:
• docker run -it python:3.11 bash
→ You’re inside the container with a shell.
Perfect Let’s build a cheat sheet of Docker basics, grouped into categories with simple
explanations.
1. Docker Info & Help
• docker --version → Show installed Docker version.
• docker info → Show system info (containers, images, storage).
• docker help → Show help for all commands.
• docker <command> --help → Show help for a specific command.
2. Images
• docker images → List downloaded images.
• docker pull <image> → Download an image from Docker Hub.
o Example: docker pull nginx
• docker rmi <image> → Remove an image.
• docker build -t <name> . → Build an image from a Dockerfile.
3. Containers
• docker ps → List running containers.
• docker ps -a → List all containers (including stopped).
• docker run <image> → Create + start container.
o Options:
▪ -d → run in background (detached).
▪ -it → interactive mode (useful for shells).
▪ -p <host:container> → map ports.
▪ -e KEY=value → set environment variable.
▪ --name <name> → give container a custom name.
• docker start <container> → Start an existing container.
• docker stop <container> → Stop a running container.
• docker restart <container> → Restart a container.
• docker rm <container> → Remove a stopped container.
4. Data (Volumes & Files)
• docker volume ls → List volumes.
• docker run -v <host_path>:<container_path> <image> → Mount a local folder inside
container.
o Example: -v $(pwd):/app → maps current folder into container at /app.
• docker cp <container>:/path/file.txt ./ → Copy file from container to host.
• docker cp ./file.txt <container>:/path/ → Copy file from host to container.
5. Logs & Debugging
• docker logs <container> → Show container logs.
• docker exec -it <container> bash → Open a shell inside running container.
• docker inspect <container> → Detailed info (JSON).
• docker top <container> → Show processes running inside container.
6. Cleanup
• docker system df → Show disk usage.
• docker system prune → Remove stopped containers, unused networks, dangling
images.
• docker image prune → Remove unused images.
• docker container prune → Remove stopped containers.
Quick Example (tying it all together):
# Run MySQL in background with password & port mapping
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=secret --name mydb mysql
# Check logs
docker logs mydb
# Open shell
docker exec -it mydb bash
# Stop and remove
docker stop mydb && docker rm mydb