Docker Cheatsheet 101

Docker Cheatsheet 101

Getting Started with Docker

Docker is a platform for developing, shipping, and running applications inside containers. Containers package software along with dependencies and configurations, enabling consistency across environments.

Basic Commands

  • Images
    • List images: docker images
    • Build an image: docker build -t <image_name> .
    • Remove an image: docker rmi <image_id>
    • Tag an image: docker tag <image_id> <repository>/<tag>
  • Containers
    • Create a container: docker create <image_name>
    • Run a container: docker run <image_name>
    • List running containers: docker ps
    • List all containers: docker ps -a
    • Start a container: docker start <container_id>
    • Stop a container: docker stop <container_id>
    • Remove a container: docker rm <container_id>
    • Execute command in a container: docker exec -it <container_id> <command>
  • Networks
    • List networks: docker network ls
    • Create a network: docker network create <network_name>
    • Connect a container to a network: docker network connect <network_name> <container_id>
    • Disconnect a container from a network: docker network disconnect <network_name> <container_id>

Dockerfile Basics

A Dockerfile is a text document that contains all the commands to assemble an image.

Example Dockerfile:

FROM ubuntu:latest
RUN apt-get update && apt-get install -y python3
COPY . /app
WORKDIR /app
CMD ["python3", "app.py"]

Common Instructions:

  • FROM: Base image
  • RUN: Execute a command
  • COPY: Copy files from host to container
  • WORKDIR: Set the working directory
  • CMD: Default command to run

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications.

Basic Commands:

  • Start services: docker-compose up
  • Stop services: docker-compose down
  • List services: docker-compose ps

Example docker-compose.yml:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
  db:
    image: postgres

Clean Up

  • Remove unused data: docker system prune
  • Remove all stopped containers: docker container prune
  • Remove all unused images: docker image prune -a

References

This cheatsheet provides a quick reference to essential Docker commands, helping you manage images, containers, networks, and more. For comprehensive tutorials and examples, visit the Docker documentation and tutorials available online.

Leave a Comment

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