The Ultimate Docker CLI guide for developers

Your essential reference for interacting with Docker images, containers, volumes, networks and more. Whether you’re just starting or need a quick command reminder, this guide has you covered.

Registry Login & Logout

To push or pull images from a private registry, you need to authenticate first. Docker uses credential helpers to store login info securely.

Log in to Docker Hub (or any registry)

Prompts for username and password. You must be logged in to push images.

docker login

Log in to a custom / private registry

Include the registry URL to log in to a private registry.

docker login myregistry.example.com

If your registry uses a different port:

docker login myregistry.example.com:5000

Once logged in, you can push/pull using full image references:

docker push myregistry.example.com/myuser/myimage:latest

docker pull myregistry.example.com/myuser/myimage:latest

Important: Make sure the image is tagged correctly before pushing:

docker tag local-image myregistry.example.com/myuser/myimage:latest

Log out

Removes stored credentials for the registry.

docker logout

# Or logout from a specific registry
docker logout myregistry.example.com

Docker images

Commands for building, managing, tagging, pushing and cleaning up Docker images.

Build an image from a Dockerfile

Builds a Docker image from your local context.

docker build -t image-name .

Remember: the . at the end of the docker build command defines the build context, not just the location of your Dockerfile. Docker will send everything in that folder to the build engine unless excluded. To avoid sending unnecessary files, large folders or sensitive data (like .env, node_modules, or credentials), make sure to define a proper .dockerignore file. It helps keep your builds clean, fast and secure.

List local images

Displays all images available locally on your machine.

docker images

Remove an image

Deletes an image from your system.

docker rmi image-name

# Or by ID
docker rmi IMAGE_ID

Remove dangling (untagged) images

Cleans up intermediate and untagged images to save space.

docker image prune

Tag an image

Gives an existing image a new name and optionally a new tag.

docker tag my-image:latest myrepo/my-image:v1

Tag for a remote registry

To push to a remote registry, include the full registry path in the tag.

docker tag my-image myregistry.com/myuser/my-image:latest

Retag an existing image

You can create a new tag from an existing local image.

docker tag old-image-name new-image-name:new-tag

See tags of a local image

Docker doesn’t have a native command to list all tags of an image locally. You’ll only see the tags that you created explicitly:

docker images

To list all available tags for a remote image (like on Docker Hub) use the web interface or a tool like skopeo.

Pull image from Docker Hub

Downloads an image from Docker Hub.

docker pull nginx

Push image to Docker Hub

Uploads your image to a remote repository (you must be logged in).

docker push myrepo/my-image:v1

Docker Containers

Commands to run, manage, pause, stop and remove containers.

Create and run a container

Starts a new container in detached mode (-d) with a given name.

docker run -d --name my-container my-image

Run a container with a shell (interactive)

Runs a container interactively and gives you a shell inside.

docker run -it my-image

Assign a name to a new container

Gives a human-readable name to your container for easier reference.

docker run --name my-nginx nginx

Rename an existing container

Changes the name of an existing container.

docker rename old-name new-name

Stop a running container

Gracefully stops the container.

docker stop my-container

Start a stopped container

Restarts a previously stopped container.

docker start my-container

Pause and unpause a container

Freezes or resumes all processes within a container (useful for maintenance).

docker pause my-container
docker unpause my-container

Remove a container

Deletes a container. Make sure it’s stopped first.

docker rm my-container

Remove all stopped containers

Cleans up exited containers to keep your environment tidy.

docker container prune

Access a running container

Opens an interactive shell inside the container.

docker exec -it my-container bash

Inspect and Logs

Tools to debug, inspect and monitor your containers and images.

View logs from a container

Shows the output (stdout/stderr) of your container.

docker logs my-container

Follow logs live (like tail -f)

Useful to monitor a container’s output in real-time.

docker logs -f my-container

Inspect details of a container/image

Returns detailed JSON about a container or image configuration.

docker inspect my-container

docker inspect my-image

Check containers resource usage (live stats)

Shows live CPU, memory and I/O usage of running containers.

docker stats

Volumes

Volumes help you persist and share data between containers or between a container and the host.

Create a volume

Creates a named volume for data persistence.

docker volume create my-volume

List volumes

Displays all volumes available on the system.

docker volume ls

Mount a volume to a new container

Attaches the volume to a specific path in the container.

docker run -v my-volume:/data ubuntu

Inspect a volume

Shows details of a volume like mount point and usage.

docker volume inspect my-volume

Remove a volume

Deletes a volume (must not be in use).

docker volume rm my-volume

Remove unused volumes

Cleans up volumes not used by any containers.

docker volume prune

Networking

Create isolated networks for your containers to communicate securely.

List networks

Shows all available Docker networks.

docker network ls

Create a new network

Creates a custom network for container communication.

docker network create my-network

Run a container in a specific network

Starts a container attached to a custom network.

docker run --network=my-network nginx

Connect a container to an existing network

Links a running container to a different network.

docker network connect my-network my-container

Disconnect a container from a network

Unlinks the container from a specific network.

docker network disconnect my-network my-container

Inspect a network

Shows the containers connected and network settings.

docker network inspect my-network

Docker Compose (Basics)

Use docker-compose to manage multi-container applications declaratively.

Start services

Starts all services defined in docker-compose.yml. Use -d to run in detached mode.

docker-compose up

# Or in detached mode:
docker-compose up -d

Stop services

Stops and removes containers, networks and volumes defined by the Compose file.

docker-compose down

Rebuild services

Rebuilds the images and restarts containers. Useful after code changes.

docker-compose up --build

View logs from compose services

Shows logs for all services. Use -f to follow logs in real-time.

docker-compose logs

# Or to follow logs:
docker-compose logs -f

Execute a command in a running service

Runs an interactive command like bash inside a service container.

docker-compose exec service-name bash

Cleanup

Clean your Docker environment by removing unused or unnecessary resources.

Remove all stopped containers, unused networks, dangling images and build cache

Frees up disk space safely.

docker system prune

Remove everything (containers, networks, volumes, images) – Use with caution !

Dangerous but powerful cleanup. Only run if you know what you’re doing.

docker system prune -a --volumes

Need a cheat sheet?

Save this file. Keep it handy. Print it. It’s your quick-reference Docker toolkit.

Fede.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top