Docker Cheat-sheet

Docker Cheat-sheet

Table of contents

No heading

No headings in the article.

  1. Pulling an image:

    • Syntax: docker pull <image_name>:<tag>

    • Example: docker pull ubuntu:latest

  2. Building an image from a Dockerfile:

    • Syntax: docker build -t <image_name> <path_to_dockerfile>

    • Example: docker build -t myapp:1.0 .

  3. Running a container:

    • Syntax: docker run <options> <image_name>

    • Example: docker run -d --name mycontainer myapp:1.0

  4. Listing Docker images:

    • Syntax: docker images

    • Example: docker images

  5. Removing an image:

    • Syntax: docker rmi <image_name>

    • Example: docker rmi myapp:1.0

  6. Listing running containers:

    • Syntax: docker ps

    • Example: docker ps

  7. Listing all containers (including stopped ones):

    • Syntax: docker ps -a

    • Example: docker ps -a

  8. Starting a stopped container:

    • Syntax: docker start <container_id>

    • Example: docker start mycontainer

  9. Stopping a running container:

    • Syntax: docker stop <container_id>

    • Example: docker stop mycontainer

  10. Removing a container:

    • Syntax: docker rm <container_id>

    • Example: docker rm mycontainer

  11. Running a command inside a container:

    • Syntax: docker exec -it <container_id> <command>

    • Example: docker exec -it mycontainer ls

  12. Creating a Docker volume:

    • Syntax: docker volume create <volume_name>

    • Example: docker volume create myvolume

  13. Listing Docker volumes:

    • Syntax: docker volume ls

    • Example: docker volume ls

  14. Removing a Docker volume:

    • Syntax: docker volume rm <volume_name>

    • Example: docker volume rm myvolume

  15. Creating a Docker network:

    • Syntax: docker network create <network_name>

    • Example: docker network create mynetwork

  16. Listing Docker networks:

    • Syntax: docker network ls

    • Example: docker network ls

  17. Removing a Docker network:

    • Syntax: docker network rm <network_name>

    • Example: docker network rm mynetwork

  18. Running a container in a specific network:

    • Syntax: docker run --network=<network_name> <image_name>

    • Example: docker run --network=mynetwork myapp:1.0

  19. Running Docker Compose:

    • Syntax: docker-compose up

    • Example: docker-compose up

  20. Stopping and removing containers defined in a Docker Compose file:

    • Syntax: docker-compose down

    • Example: docker-compose down

  21. Inspecting a Docker container:

    • Syntax: docker inspect <container_id>

    • Example: docker inspect mycontainer

  22. Copying files between the host and a container:

    • Syntax: docker cp <host_path> <container_id>:<container_path>

    • Example: docker cp myfile.txt mycontainer:/app

  23. Viewing the logs of a container:

    • Syntax: docker logs <container_id>

    • Example: docker logs mycontainer

  24. Attaching to a running container's terminal:

    • Syntax: docker attach <container_id>

    • Example: docker attach mycontainer

  25. Pausing and unpausing a running container:

    • Syntax: docker pause <container_id> and docker unpause <container_id>

    • Example: docker pause mycontainer and docker unpause mycontainer

  26. Renaming a container:

    • Syntax: docker rename <old_name> <new_name>

    • Example: docker rename mycontainer newcontainer

  27. Pushing an image to a Docker registry:

    • Syntax: docker push <image_name>:<tag>

    • Example: docker push myapp:1.0

  28. Pruning unused Docker resources:

    • Syntax: docker system prune

    • Example: docker system prune

  29. Inspecting a Docker volume:

    • Syntax: docker volume inspect <volume_name>

    • Example: docker volume inspect myvolume

  30. Running a container in the background with a specific name:

    • Syntax: docker run -d --name <container_name> <image_name>

    • Example: docker run -d --name mycontainer myapp:1.0

These examples demonstrate the syntax and provide one-line commands for various Docker operations. Remember to replace <image_name>, <tag>, <container_id>, <host_path>,<old_name>, <new_name>, <volume_name>, and <network_name> with your own names as needed.


Thank you for reading. I hope you were able to understand and learn something new from my blog.

Happy Learning!

Please follow me on Hashnode and do connect with me on LinkedIn ArnavSingh

Did you find this article valuable?

Support Arnav Singh by becoming a sponsor. Any amount is appreciated!