└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Docker-Cheatsheet 2 | 3 | This repository contains all the Docker knowledge you need to get started as a beginner. 4 | 5 | ## Docker Installation 6 | 7 | ### Install Docker using the following command on your Linux machine: 8 | ```bash 9 | sudo apt install docker.io 10 | ``` 11 | ### Installing Docker on Windows 12 | 13 | 1. **Download Docker Desktop:** 14 | - Install the Docker Desktop for Windows by clicking [HERE](https://desktop.docker.com/win/stable/Docker%20Desktop%20Installer.exe). 15 | 16 | 2. **Install Docker Desktop:** 17 | - Run the Docker Desktop installer. 18 | - Follow the installation instructions on the screen. 19 | - Once the installation is complete, start Docker Desktop from the Start menu. 20 | 21 | 3. **Configuration:** 22 | - Docker Desktop will launch and complete the initial configuration. 23 | - You may need to enable virtualization in your BIOS/UEFI settings if it's not already enabled. 24 | 25 | 4. **Verify Installation:** 26 | - Open a Command Prompt or PowerShell. 27 | - Run the following command to verify that Docker is installed correctly: 28 | ```bash 29 | docker --version 30 | ``` 31 | - You should see the Docker version information displayed. 32 | 33 | ## Docker Commands 34 | 35 | ### Container Management 36 | 1. **Run a Container:** 37 | ```bash 38 | docker container run --publish {host_port}:{container_port} --detach --name {container_name} {image} {bash} 39 | ``` 40 | - Example: `8080:80` forwards traffic from port 8080 on the host to port 80 in the container. 41 | - `--detach` runs the container in the background. 42 | - `--publish` maps host ports to container ports. 43 | 44 | 2. **Stop a Container:** 45 | ```bash 46 | docker container stop {name_or_ID} 47 | ``` 48 | 49 | 3. **Start a Container:** 50 | ```bash 51 | docker container start {name_or_ID} 52 | ``` 53 | 54 | 4. **List Running Containers:** 55 | ```bash 56 | docker container ls 57 | ``` 58 | 59 | 5. **List All Containers:** 60 | ```bash 61 | docker container ls -a 62 | ``` 63 | 64 | 6. **Show Container Processes:** 65 | ```bash 66 | docker container top {name_or_ID} 67 | ``` 68 | 69 | 7. **Inspect a Container:** 70 | ```bash 71 | docker container inspect {name_or_ID} 72 | ``` 73 | 74 | 8. **View Container Logs:** 75 | ```bash 76 | docker container logs {name_or_ID} 77 | ``` 78 | 79 | 9. **Show Live CPU Usage:** 80 | ```bash 81 | docker container stats 82 | ``` 83 | 84 | ### Interactive Mode 85 | 10. **Run a Container in Interactive Mode:** 86 | ```bash 87 | docker container run -it --name {container_name} {image} /bin/bash 88 | ``` 89 | - `-it` flags for interactive terminal (tty). 90 | 91 | 11. **Start an Interactive Container:** 92 | ```bash 93 | docker start -ai {container_name} 94 | ``` 95 | - `-a` for attach. 96 | - `-i` for interactive. 97 | 98 | ### Image Management 99 | 12. **List Images:** 100 | ```bash 101 | docker image ls 102 | ``` 103 | 104 | 13. **Remove an Image:** 105 | ```bash 106 | docker image rm {image_name} 107 | ``` 108 | - Or: `docker rmi {image_name}` 109 | - Use `-f` to force delete. 110 | 111 | 14. **Pull an Image:** 112 | ```bash 113 | docker pull {image_name} 114 | ``` 115 | - Example: `alpine` is a small Linux distro (4MB). 116 | - Note: Alpine does not include bash by default, use `sh` instead. 117 | 118 | ### Network Management 119 | 15. **Inspect Container IP:** 120 | ```bash 121 | docker container inspect --format '{{.NetworkSettings.IPAddress}}' {name_or_ID} 122 | ``` 123 | 124 | 16. **Verify Port Mapping:** 125 | ```bash 126 | docker container port {name_or_ID} 127 | ``` 128 | 129 | 17. **List Networks:** 130 | ```bash 131 | docker network ls 132 | ``` 133 | - Example: `bridge` is the default Docker virtual network. 134 | 135 | 18. **Create a Network:** 136 | ```bash 137 | docker network create {network_name} 138 | ``` 139 | 140 | 19. **Run a Container in a Specific Network:** 141 | ```bash 142 | docker container run -d --name {container_name} {image} --network {network_name} 143 | ``` 144 | 145 | 20. **Connect a Container to a Network:** 146 | ```bash 147 | docker network connect {network_name} {container_name} 148 | ``` 149 | 150 | 21. **Ping Between Containers:** 151 | ```bash 152 | docker container exec -it {container_1} ping {container_2} 153 | ``` 154 | 155 | ### Building Custom Docker Images 156 | 22. **Build an Image:** 157 | ```bash 158 | docker build -t {image_name} -f {Dockerfile} . 159 | ``` 160 | - `-t` tags the image. 161 | - `-f` specifies the Dockerfile. 162 | - `.` indicates the current directory contains the files needed for the build. 163 | 164 | 23. **Run a Custom Image:** 165 | ```bash 166 | docker container run {image_name} 167 | ``` 168 | 169 | --- 170 | 171 | Author: **Purva Patel** 172 | --------------------------------------------------------------------------------