Skip to main content

Docker

Introduction

This cheat sheet provides a quick reference for some common Docker commands and concepts. Docker is a platform for developing, shipping, and running applications in containers.

Installation

To use Docker, you need to install it on your system. Installation methods may vary depending on your operating system. Refer to the official Docker documentation for installation instructions.

Docker Concepts

Images

  • Pull an image from Docker Hub:

    docker pull image_name:tag
  • List all locally available images:

    docker images
  • Remove an image:

    docker rmi image_name:tag

Containers

  • Create and start a container from an image:

    docker run -d --name my_container image_name:tag
  • List all running containers:

    docker ps
  • List all containers (including stopped ones):

    docker ps -a
  • Stop a running container:

    docker stop container_id
  • Remove a container (must be stopped):

    docker rm container_id

Volumes

  • Create a Docker volume:

    docker volume create my_volume
  • List Docker volumes:

    docker volume ls
  • Remove a Docker volume:

    docker volume rm my_volume

Networking

  • List Docker networks:

    docker network ls
  • Create a custom Docker network:

    docker network create my_network

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file to configure application services and environments.

Docker Compose Commands

  • Start containers defined in a Compose file:

    docker-compose up -d
  • Stop containers defined in a Compose file:

    docker-compose down
  • List running Compose services:

    docker-compose ps
  • View logs for services:

    docker-compose logs

Dockerfile

Dockerfile is a script that contains instructions for building a Docker image.

Building Images with Dockerfile

  • Build an image from a Dockerfile in the current directory:

    docker build -t my_image:tag .
  • Build an image from a Dockerfile in a specific directory:

    docker build -t my_image:tag /path/to/dockerfile/dir

Conclusion

This cheat sheet covers some basic Docker commands and concepts. Docker offers a wide range of features and functionality; refer to the Docker documentation for more in-depth information and advanced usage.