Docker Command Reference

·1287 words

This reference is for readers who can already connect to Docker Engine and need to look up common commands quickly. It focuses on the Docker CLI; available features may vary by Docker Desktop, Docker Engine, Compose plugin, Swarm, version, and platform. If you are new to images and containers, start with Docker Basics.

Commands are grouped by task. Do not run the page as one script. Replace angle-bracket values such as <container> before running a command; shell variables prefixed with $ must also be set in the current shell.

Connection and Environment
#

TaskCommandNotes
Check the client and Enginedocker versionShows both Client and Server. Seeing only Client information usually means the Engine is not reachable.
Inspect Engine informationdocker infoShows details about the connected Engine, storage driver, and runtime environment.
Show helpdocker --help; docker <command> --helpLists top-level commands or options for a subcommand, such as docker run --help.
List connection targetsdocker context lsShows Docker CLI contexts.
Show the current targetdocker context showConfirms which Docker Engine receives commands.

On Linux, use the access method configured during installation, such as sudo, if your user cannot access the Docker socket. Do not ignore the root-level access granted by the docker group just to avoid sudo; see the installation guide.

Images
#

TaskCommandNotes
List local imagesdocker imagesLists images on this host; docker image ls is the corresponding management command.
Search Docker Hubdocker search <term>Searches public repositories; search results are not a security or compatibility review.
Pull an imagedocker pull <image>:<tag>Retrieves a tagged image from a registry. Prefer the required version; latest does not mean automatic updates.
Tag an imagedocker tag <source-image>:<tag> <registry>/<namespace>/<image>:<tag>Adds another name to a local image; it does not duplicate or upload the image.
Sign in to a registrydocker login <registry>Enter credentials when prompted. Do not put passwords directly in commands or scripts. The registry argument can be omitted for Docker Hub.
Push an imagedocker push <registry>/<namespace>/<image>:<tag>Pushes to a repository where you have write access. Verify the registry, namespace, and tag first.
Save an image archivedocker save -o image.tar <image>:<tag>Exports an image and its related tags for offline transfer.
Load an image archivedocker load -i image.tarRestores an image from an archive created by docker save.
Remove one local imagedocker rmi <image>:<tag>Removes a local image reference/data; removal may be blocked while a container uses it.

docker save and docker load handle images, not container runtime state. See Getting and Distributing Docker Images for the full workflow.

Containers
#

TaskCommandNotes
Run a foreground containerdocker run --name <name> <image>:<tag>Displays the program output. When the program exits, the container stops but is not removed by default.
Run interactivelydocker run --rm -it <image>:<tag> sh-i keeps input open; -t allocates a terminal. --rm removes the container on exit. The image must include sh.
Run in the backgrounddocker run -d --name <name> <image>:<tag>Returns a container ID. The container exits when its main process ends.
Publish a portdocker run -d --name <name> -p 127.0.0.1:8080:80 <image>:<tag>Maps host loopback port 8080 to container port 80. Omitting the host IP publishes on all interfaces; assess exposure first.
List running containersdocker psStopped containers are hidden by default.
List all containersdocker ps -aIncludes exited containers; add --filter name=<name> to filter. Name filtering can match substrings.
Read container logsdocker logs <container>Reads output written to the container’s standard output and error streams.
Follow logsdocker logs -f --tail 100 <container>Starts with the last 100 lines and follows new output. Press Ctrl-C to stop following; this does not stop the container.
Run a command in a running containerdocker exec -it <container> shThe container must be running and include sh.
Inspect a containerdocker inspect <container>Prints JSON configuration and state; it may include sensitive environment variables.
View resource usagedocker statsStreams CPU, memory, network, and block I/O statistics for running containers. Press Ctrl-C to exit.
Stop a containerdocker stop <container>Sends a stop signal, then forcefully terminates the process after the grace period.
Start a stopped containerdocker start <container>Starts the same existing container; it does not create a new one.
Remove a stopped containerdocker rm <container>Removes the container and its writable layer, not mounted volumes. Confirm its identity and data requirements first.

The writable layer is removed with its container. Store data that must persist in a volume or bind mount; do not use docker rm -v on an unverified container. See Docker Data Persistence.

Networks and Volumes
#

TaskCommandNotes
List networksdocker network lsLists networks on the current Engine.
Inspect a networkdocker network inspect <network>Shows network configuration and connected endpoints.
Create a bridge networkdocker network create <network>Creates a bridge network by default, on the current Engine only.
List volumesdocker volume lsLists volumes managed by the current Engine.
Inspect a volumedocker volume inspect <volume>Shows volume configuration, mountpoint, and other details.
Create a volumedocker volume create <volume>Creates a named volume that has an independent lifecycle from containers.
Mount a volume in a containerdocker run --mount type=volume,src=<volume>,dst=<path> <image>:<tag>Mounts an existing volume at a container path. Only data written under that mount path goes into the volume.

Before removing a network or volume, confirm that it is no longer needed. Removing a volume deletes its data. See the network guide and data persistence guide.

Dockerfile and Compose
#

TaskCommandNotes
Build an imagedocker build -t <image>:<tag> <build-context>The build context is usually the directory containing the Dockerfile. Use .dockerignore to exclude files that should not be sent.
Show build optionsdocker build --helpBuild options can vary by builder and platform.
Validate Compose configurationdocker compose configMerges and parses Compose configuration. Review output before sharing because interpolated secrets may appear.
Start a Compose projectdocker compose up -dCreates or updates services in the current Compose project and runs them in the background.
Build and startdocker compose up -d --buildBuilds images required by the configuration before starting the project.
List project containersdocker compose psShows container state for the current project.
Follow service logsdocker compose logs -f --tail 100 <service>Follows logs for a service; <service> is its name in the Compose file.
Run a command in a servicedocker compose exec <service> shRuns in an already running service container; the container must include sh.
Stop and remove project containers and networksdocker compose downDoes not remove named volumes or images by default. Avoid adding -v casually; it removes named volumes used by the Compose project and their data.

The effects of rebuilding, restarting, and retaining data depend on the configuration. Start troubleshooting with docker compose config, ps, and logs. See the Compose Flask + Redis guide for a complete application workflow.

Boundaries
#

  • Values in <...> are placeholders. Replace them with real values; inspect an existing resource before removing anything with a conflicting name or tag.
  • Docker CLI commands act on the Engine selected by the current context. With a remote context, create, remove, pull, and other operations affect that remote Engine or registry.
  • This reference omits bulk cleanup commands such as docker system prune and docker volume prune. They may remove resources still needed by other projects.
  • Check docker <command> --help for options supported by your installed version. Compose configuration fields and Swarm Stack fields are not identical; do not use Compose commands here as a Stack deployment workflow.

References
#