Essential Docker Commands

Build an Image

docker build reads a Dockerfile and creates an image. The -t flag tags it as name:version, and the final . sets the build context to the current directory.

# Build and tag an image from the Dockerfile in the current folder
docker build -t my-nginx-app:v1 .

Multiple Dockerfiles? Use -f to pick a specific file:

# Build using an alternate Dockerfile in the current directory
docker build -f Dockerfile.single -t my-nginx-app:v1 .
Flag / argumentWhat it doesExample
-tNames the image (name:version)-t my-nginx-app:v1
.Build context sent to the Docker daemon. (current folder)
-fPath to a non-default Dockerfile-f Dockerfile.single

Run a Container

docker run starts a container from an image. -d runs it in the background, --name labels it, and -p maps a host port to a container port (HOST:CONTAINER).

# Start nginx in the background, name it, and map host port 8080 to container port 80
docker run -d --name nginx-demo -p 8080:80 my-nginx-app:v1
FlagWhat it doesExample
-dDetached mode — background-d
--nameCustom container name--name nginx-demo
-pHost-to-container port map-p 8080:80

Clean Up

Remove one container or image by name, or wipe everything during a reset:

# Force-remove a single container by name
docker rm -f containerName

# Force-remove a single image by name and tag
docker rmi -f imageName:v

# Remove every container (running and stopped)
docker rm -f $(docker ps -a -q)

# Remove every image on the system
docker rmi -f $(docker images -a -q)

-f skips prompts. Bulk commands pass IDs from docker ps -a -q (containers) or docker images -a -q (images).

When to Use Which Command

GoalCommand
Dockerfile → reusable imagedocker build -t name:version .
Run in backgrounddocker run -d ...
Reach app from browserdocker run -p HOST:CONTAINER ...
Delete one itemdocker rm -f or docker rmi -f
Full resetBulk docker rm / docker rmi with -q

Key Takeaway docker build creates images from Dockerfiles, docker run turns images into live containers, and cleanup commands keep your machine from filling up with stale layers. Build once, run with a port map, and remove what you no longer need.