Skip to content

Instantly share code, notes, and snippets.

@CodeLeom
Last active October 15, 2024 21:59
Show Gist options
  • Select an option

  • Save CodeLeom/81c8c60678d9cf8d1ed4ec00e4a10374 to your computer and use it in GitHub Desktop.

Select an option

Save CodeLeom/81c8c60678d9cf8d1ed4ec00e4a10374 to your computer and use it in GitHub Desktop.
Docker cheat sheet

Docker Cheat Sheet for Developers

1. Basic Docker Commands

  • docker --version: Check Docker version.
  • docker help: Show Docker commands and options.
  • docker info: Display system-wide information about Docker.

2. Images

  • docker images: List all local images.
  • docker pull <image>: Download an image from Docker Hub.
  • docker build -t <image_name> .: Build an image from a Dockerfile in the current directory.
  • docker rmi <image>: Remove an image.
  • docker tag <source_image> <new_image>: Tag an image with a new name.

3. Containers

  • docker run <image>: Run a container from an image.
  • docker run -it <image>: Run a container in interactive mode.
  • docker run -d <image>: Run a container in detached mode.
  • docker ps: List running containers.
  • docker ps -a: List all containers (including stopped ones).
  • docker stop <container>: Stop a running container.
  • docker start <container>: Start a stopped container.
  • docker restart <container>: Restart a container.
  • docker rm <container>: Remove a stopped container.
  • docker exec -it <container> <command>: Run a command inside a running container.

4. Container Management

  • docker logs <container>: View logs of a container.
  • docker top <container>: Display the processes running in a container.
  • docker stats <container>: Display a live stream of resource usage statistics.
  • docker inspect <container>: View details about a container or image.
  • docker rename <old_name> <new_name>: Rename a container.

5. Networking

  • docker network ls: List all Docker networks.
  • docker network create <network_name>: Create a new network.
  • docker network connect <network> <container>: Connect a container to a network.
  • docker network disconnect <network> <container>: Disconnect a container from a network.
  • docker network inspect <network>: View details about a network.

6. Volumes

  • docker volume ls: List all volumes.
  • docker volume create <volume_name>: Create a volume.
  • docker volume inspect <volume>: Display detailed information about a volume.
  • docker volume rm <volume>: Remove a volume.

7. Dockerfile Essentials

  • FROM <image>: Specify the base image.
  • COPY <source> <destination>: Copy files/directories to the image.
  • RUN <command>: Execute a command during the image build process.
  • CMD ["executable", "param1", "param2"]: Set the default command to execute when the container starts.
  • ENTRYPOINT ["executable", "param1", "param2"]: Set a command that cannot be overridden at runtime.
  • EXPOSE <port>: Inform Docker that the container listens on the specified port.
  • ENV <key>=<value>: Set environment variables.

8. Docker Compose

  • docker-compose up: Start services defined in a docker-compose.yml file.
  • docker-compose down: Stop services and remove containers, networks, and volumes.
  • docker-compose build: Build or rebuild services.
  • docker-compose logs: View output from services.
  • docker-compose exec <service> <command>: Execute a command in a running service container.

9. Cleaning Up

  • docker system prune: Remove unused data (containers, networks, images, and volumes).
  • docker container prune: Remove all stopped containers.
  • docker image prune: Remove unused images.
  • docker volume prune: Remove unused volumes.

10. Docker Hub

  • docker login: Log into Docker Hub.
  • docker logout: Log out of Docker Hub.
  • docker push <username>/<image>: Push an image to Docker Hub.
  • docker pull <username>/<image>: Pull an image from Docker Hub.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment