Echo Kanak

Docker commands cheatsheet

Nov 19, 2024

Quick reference for common Docker + Docker Compose commands.

  • docker ps

    List all running docker containers.

  • docker ps -a

    List all docker containers, including stopped ones.

  • docker images

    List all docker images.

  • docker logs -f {container_id}

    See logs of the running container in real time.

  • docker build -t {image_name} .

    Build a docker image from the Dockerfile in the current directory.

  • docker run -it {image_name}

    Run the Docker image in interactive mode with a terminal session.

  • docker run -it -v {folder-path}:/app {image_name}

    Run the image and mount a host directory into the container. used if we want files generated in the container to appear in the host directory.

  • docker stop {container_id}

    Stop a running container.

  • docker rmi -f {image_id}

    Delete an image (-f forces deletion even if it’s used by a stopped container).

  • docker inspect {container_name/id}

    Return container details in JSON format.

  • docker stats

    Show real-time resource usage of containers.

  • docker container prune

    Remove all stopped containers.

  • docker image prune

    Remove all dangling images. (Docker won’t prune dangling images that still have containers, even if they’re not running. Use docker container prune first.)

  • docker search {image_name}

    Search for an image on Docker Hub and display stats.

  • docker pull {image_name}

    Pull images from Docker Hub.

  • docker login

    Log in to your Docker Hub account.

  • docker push {docker_hub_repo}:{tag_name}

    Push an image to Docker Hub.

  • docker image tag {old_image_name} {new_image_name}

    Rename (re-tag) an image.

Docker Compose Commands

  • docker-compose up -d

    Start services in the background. It won’t rebuild images if they already exist.

  • docker-compose up --build

    Start services, rebuild images even if they exist, and attach logs to the terminal.

  • docker-compose down

    Stop and remove containers started by docker-compose.

  • docker-compose restart {container_name}

    Restart a service container managed by docker-compose.

Docker User Management

  • sudo usermod -aG docker {USER}

    Add a user to the docker group (changes apply after re-login, or use newgrp docker).

  • getent group docker

    Get list of users who are part of the docker group.

Docker commands cheatsheet | Echo Kanak