Skip to content

Nihal009/Docker_CheatSheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Docker Guide and Cheatsheet

1. Introduction to Docker

Docker is a platform that allows you to develop, ship, and run applications inside containers. Containers are lightweight, portable, and ensure consistency across multiple development, testing, and production environments.

2. Installation

  • Windows and Mac: Install Docker Desktop from Docker's official website.
  • Linux:
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io

3. Basic Commands

  • Check Docker Version:
    docker --version
  • Pull an Image:
    docker pull <image_name>
  • Run a Container:
    docker run -it <image_name> /bin/bash
  • List Running Containers:
    docker ps
  • List All Containers:
    docker ps -a
  • Stop a Container:
    docker stop <container_id>
  • Remove a Container:
    docker rm <container_id>
  • Remove an Image:
    docker rmi <image_name>

4. Dockerfile

A Dockerfile is a script containing a series of instructions on how to build a Docker image.

Example Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the current directory contents into the container at /usr/src/app
COPY . .

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

5. Building and Running a Docker Image

  • Build an Image:
    docker build -t <image_name> .
  • Run a Container from an Image:
    docker run -p 4000:80 <image_name>

6. Docker Compose

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

Example docker-compose.yml:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  redis:
    image: "redis:alpine"
  • Run Docker Compose:
    docker-compose up

7. Networking

  • 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>

8. Volumes

Volumes are used to persist data generated by and used by Docker containers.

  • Create a Volume:
    docker volume create <volume_name>
  • List Volumes:
    docker volume ls
  • Run a Container with a Volume:
    docker run -v <volume_name>:/path/in/container <image_name>

9. Common Commands Cheatsheet

Command Description
docker --version Check Docker version
docker pull <image> Pull an image from Docker Hub
docker build -t <image_name> . Build an image from a Dockerfile
docker run -it <image_name> /bin/bash Run a container interactively
docker ps List running containers
docker ps -a List all containers
docker stop <container_id> Stop a container
docker rm <container_id> Remove a container
docker rmi <image_name> Remove an image
docker-compose up Run Docker Compose
docker volume create <volume_name> Create a volume
docker network create <network_name> Create a network

10. Best Practices

  • Keep Dockerfile instructions ordered logically and layer them from less to more frequently changing.
  • Use .dockerignore to avoid adding unnecessary files to the Docker image.
  • Keep images small by using slim or alpine base images.
  • Use multi-stage builds to reduce image size and improve build times.
  • Always specify explicit versions of base images to ensure consistency.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors