Docker Commands
Source: https://docs.docker.com/reference/cli/docker/
Manage Containers
docker run [OPTIONS] IMAGE [COMMAND]
Creates and runs a new container from an image.
docker run -d -p 80:80 --name my-nginx nginx
-d: Detached mode (runs in the background).-p <host_port>:<container_port>: Publishes a container’s port to the host.-it: Creates an interactive terminal session.--name <name>: Assigns a name to the container.-v <host_path>:<container_path>: Mounts a host volume into the container.--rm: Automatically removes the container when it exits.
docker ps [OPTIONS]
Lists running containers.
docker ps
-a: Shows all containers (running and stopped).-q: Only displays container IDs.
docker stop <container_id_or_name>
Stops one or more running containers.
docker stop my-nginx
docker start <container_id_or_name>
Starts one or more stopped containers.
docker start my-nginx
docker rm [OPTIONS] <container_id_or_name>
Removes one or more containers.
docker rm my-nginx
-f: Forces the removal of a running container.
docker logs [OPTIONS] <container_id_or_name>
Fetches the logs of a container.
docker logs my-nginx
-f: Follows the log output.--tail <number>: Shows the last N lines of the logs.
docker exec [OPTIONS] <container_id_or_name> <command>
Runs a command in a running container.
docker exec -it my-nginx /bin/bash
-it: Creates an interactive terminal session.
docker inspect <container_id_or_name>
Displays detailed, low-level information on a container.
docker inspect my-nginx
Manage Images
docker images
Lists all locally stored images.
docker images
docker build [OPTIONS] .
Builds a Docker image from a Dockerfile.
docker build -t myapp:latest .
-t <name:tag>: Sets the name and tag for the image.-f <path_to_dockerfile>: Specifies the path to the Dockerfile.--no-cache: Builds the image without using the cache.
docker pull <image_name:tag>
Downloads an image from a registry (e.g., Docker Hub).
docker pull ubuntu:latest
docker rmi [OPTIONS] <image_id_or_name>
Removes one or more images.
docker rmi myapp:latest
-f: Forces the removal of an image.
System
docker system prune [OPTIONS]
Removes unused Docker objects.
docker system prune
-a: Removes all unused images, not just dangling ones.--volumes: Removes unused volumes.