Docker and Docker Compose Cheat Sheet: A Handy Guide for Developers

Docker and Docker Compose Cheat Sheet: A Handy Guide for Developers


Docker Basics:

  • Docker Image: A lightweight, standalone, executable package that includes everything needed to run an application, including code, libraries, dependencies, and configurations.
  • Docker Container: An instance of a Docker image that is running as a process on the host system.
  • Dockerfile: A text file that contains instructions for building a Docker image.
  • Docker Registry: A place to store and share Docker images, such as Docker Hub.


Docker Commands:

  • docker run: Runs a Docker container from an image. Example: docker run hello-world
  • docker build: Builds a Docker image from a Dockerfile. Example: docker build -t my-image .
  • docker images: Lists all Docker images on the host system. Example: docker images
  • docker ps: Lists all running Docker containers on the host system. Example: docker ps
  • docker stop: Stops a running Docker container. Example: docker stop my-container
  • docker rm: Removes a stopped Docker container. Example: docker rm my-container
  • docker rmi: Removes a Docker image. Example: docker rmi my-image
  • docker exec: Runs a command inside a running Docker container. Example: docker exec my-container ls


Dockerfile Instructions:

  • FROM: Specifies the base image for the Docker image. Example: FROM ubuntu:latest
  • RUN: Executes a command inside the Docker image during the build process. Example: RUN apt-get update && apt-get install -y python
  • COPY: Copies files or directories from the host system to the Docker image. Example: COPY app.py /app/
  • EXPOSE: Exposes a port for the Docker container. Example: EXPOSE 8000
  • CMD: Specifies the default command to run when a Docker container is started. Example: CMD ["python", "app.py"]


Docker Compose

Docker Compose is a tool used to run multi-container Docker applications. It allows you to define and run multiple containers as a single application, providing an easy way to manage, configure, and deploy your containers


Docker Compose Basics

  • Docker Compose: A tool for defining and running multi-container Docker applications.
  • docker-compose.yml: A YAML file that defines the services, networks, and volumes for a Docker Compose application.
  • Service: A containerized application that can be run as part of a Docker Compose application.
  • Network: A named network that connects the services in a Docker Compose application.
  • Volume: A named volume that can be used to share data between containers in a Docker Compose application.


Basic commands:

  • docker-compose up - starts all the services defined in the docker-compose.yml file.
  • docker-compose down - stops and removes all the containers, networks, and volumes created by docker-compose up.
  • docker-compose build - builds or rebuilds the Docker images of the services defined in the docker-compose.yml file.
  • docker-compose ps - lists all the running containers for the services defined in the docker-compose.yml file.
  • docker-compose logs - shows the logs of the containers for the services defined in the docker-compose.yml file.
  • docker-compose start - starts the containers for the services defined in the docker-compose.yml file.
  • docker-compose stop - stops the containers for the services defined in the docker-compose.yml file.
  • docker-compose restart - restarts the containers for the services defined in the docker-compose.yml file.
  • docker-compose pause - pauses the containers for the services defined in the docker-compose.yml file.
  • docker-compose unpause - unpauses the containers for the services defined in the docker-compose.yml file.
  • docker-compose exec - runs a command inside a running container for a service defined in the docker-compose.yml file.
  • docker-compose pull - pulls the latest images for the services defined in the docker-compose.yml file.
  • docker-compose config - validates and displays the configuration of the services defined in the docker-compose.yml file.
  • docker-compose kill - sends a SIGKILL signal to the containers for the services defined in the docker-compose.yml file.
  • docker-compose rm - removes the stopped containers for the services defined in the docker-compose.yml file.


Configuration:

  • docker-compose.yml: Default configuration file for Docker Compose.
  • -f/--file flag: Specifies an alternate compose file.
  • --project-name flag: Specifies an alternate project name.


Services:

  • services: Defines a list of services to run in the application.
  • build: Specifies the path to the Dockerfile for building an image.
  • image: Specifies the name of the image to use.
  • container_name: Specifies the name of the container.
  • ports: Exposes ports on the host machine.
  • volumes: Mounts directories or files from the host machine to the container.
  • environment: Sets environment variables for the service.


Networks:

  • networks: Defines a list of custom networks to create.
  • driver: Specifies the network driver to use.
  • name: Specifies the name of the network.
  • external: Specifies an existing network to connect to.


Volumes:

  • volumes: Defines a list of named volumes to create.
  • driver: Specifies the volume driver to use.
  • name: Specifies the name of the volume.
  • external: Specifies an existing volume to use.


Docker Compose Service Configuration

A Docker Compose service can be configured with the following options:

  • build: Specifies the path to the Dockerfile used to build the service image.
  • image: Specifies the name of the Docker image to use for the service.
  • ports: Specifies the port mapping between the host system and the container.
  • environment: Specifies environment variables to be set in the container.
  • networks: Specifies the networks to connect the container to.
  • volumes: Specifies the volumes to mount in the container.


Docker Compose File Structure

A Docker Compose file consists of the following sections:

  • version: The version of the Docker Compose file format to use.Example: version: '3'
  • services: The services to run as part of the Docker Compose application.Example:

services:
? app:
? ? build: .
? ? ports:
? ? ? - "8000:8000"
? ? environment:
? ? ? DEBUG: "true"        

  • networks: The networks to create and connect the services in the Docker Compose application.Example:

networks:
? app-network:
? ? driver: bridge
        

  • volumes: The volumes to create and share between the containers in the Docker Compose application.Example:

volumes:
? data:
? ? driver: local        


Examples:

Example 1: Simple web application

version: '3'
services:
? web:
? ? build: .
? ? ports:
? ? ? - "5000:5000"        

This configuration defines a single service named "web" that builds an image from the Dockerfile in the current directory and exposes port 5000 on the host machine.


Example 2: Multi-container application with custom network and volumes

version: '3'
services:
? web:
? ? build: .
? ? ports:
? ? ? - "5000:5000"
? ? volumes:
? ? ? - type: bind
? ? ? ? source: ./app
? ? ? ? target: /app
? ? networks:
? ? ? - mynet
? db:
? ? image: postgres
? ? volumes:
? ? ? - db_data:/var/lib/postgresql/data
? ? networks:
? ? ? - mynet
networks:
? mynet:
volumes:
? db_data:        

This configuration defines two services named "web" and "db" that use a custom network named "mynet" and a named volume named "db_data". The "web" service builds an image from the Dockerfile in the current directory, exposes port 5000 on the host machine, mounts the ./app directory as a volume in the container, and connects to the "mynet" network. The "db" service uses the "postgres" image, mounts the "db_data" volume, and connects to the "mynet" network.






#Docker #DockerCompose #DockerCheatSheet #DockerTutorial #DockerCommands #DockerVolume #DockerNetwork #DockerServices #DockerBuild #DockerImage #DockerHub #DockerContainer #Dockerfile #DockerComposeCheatSheet #DockerComposeTutorial #DockerComposeCommands #DockerComposeServices #DockerComposeNetwork #DockerComposeVolume #DockerTipsAndTricks

要查看或添加评论,请登录

Sanket Bhalke的更多文章

社区洞察

其他会员也浏览了