From 2867960928a2e4fd56b4adef3df910f565542583 Mon Sep 17 00:00:00 2001 From: RadinPirouz Date: Fri, 17 May 2024 23:02:17 +0330 Subject: [PATCH] Update Docker Docs --- Docker/Docker.md | 154 +++++++++++++++-------------------------------- 1 file changed, 49 insertions(+), 105 deletions(-) diff --git a/Docker/Docker.md b/Docker/Docker.md index 6d1c9a6..741a2e8 100644 --- a/Docker/Docker.md +++ b/Docker/Docker.md @@ -1,120 +1,64 @@ -## Docker Cheat Sheet +# Docker Commands and Concepts -### Docker Data Directory -- **State Less:** Don't save your data like Nginx. -- **State Full:** Save your data. - - `/var/lib/docker/` ==> Docker data directory +## Docker Concepts -### Docker Commands +- **Stateless**: Do not save your data (like Nginx). +- **Stateful**: Save your data. -#### Login to Docker Hub -```bash -docker login -``` +## Docker Data Directory -#### Pull Docker Image -```bash -docker pull -``` +- `/var/lib/docker/`: Docker data directory. +- `/var/lib/docker/containers/`: Container configuration and file directory. +- `/var/lib/docker/volumes`: Directory where docker volumes are saved. -#### Show Pulled Images -```bash -docker images -``` +## Docker CLI Commands -#### Run Docker Container -```bash -docker run -``` -- **Options**: - - `-it`: Run interactively and allocate a pseudo-TTY. - - `-dit`: Run in detached mode with interactive TTY. +### Authentication -#### Go to Container Shell -```bash -docker exec -it bash -``` +- `docker login`: Login to Docker Hub with CLI. -#### Remove Docker Container -```bash -docker rm -``` +### Image Management -#### Stop Docker Container -```bash -docker stop -``` +- `docker pull `: Pull a Docker image. +- `docker images`: Show pulled images. +- `docker rmi -f `: Remove an image. +- `docker save -o `: Save image as an external file. +- `docker load -i `: Import a Docker image. -#### Stop and Remove Docker Container -```bash -docker rm -f -``` +### Container Management -#### List Docker Containers -```bash -docker ps -aq -``` +- `docker run `: Run a Docker container. + - `docker run`: Run Docker (after run exited). + - `docker run -it`: Run and give bash to me. + - `docker run -dit`: Run and give bash to me and run in the background. +- `docker exec -it `: Go to container shell. +- `docker rm `: Remove Docker container. +- `docker stop `: Stop Docker container. +- `docker rm -f `: Stop and remove Docker container. +- `docker ps -aq`: Give all Docker container IDs. +- `docker ps -aq -f status=exited`: Give all Docker container IDs with exited status. +- `docker container prune`: Remove all stopped containers. +- `docker commit `: Make a custom Docker image. +- `docker inspect `: Get all data about container information. +- `docker inspect --format '{{ .NetworkSettings.IPAddress }}' `: Get IP of the container. +- `docker cp :/`: Copy from local to container. +- `docker cp :/ `: Copy from container to local. +- `docker stats`: Monitor Docker stats. +- `docker run -dit --name server --restart=always ubuntu`: Run container again after restarting the service or main server. +- `docker build -t : `: Build Docker image from a Dockerfile. -#### List Exited Docker Containers -```bash -docker ps -aq -f status=exited -``` +### Volume Management -#### Remove Exited Docker Containers -```bash -docker rm $(docker ps -aq -f status=exited) -``` +- `docker volume ls`: List all volumes. +- `docker volume create `: Create a volume for Docker. +- `docker volume inspect `: Give information about the volume. +- `docker run -dit --name -v : `: Run Docker image and save target location data in volume. -#### Remove All Stopped Containers -```bash -docker container prune -``` +### Network Management -#### Container Configuration and Files -- `/var/lib/docker/containers/` ==> Container config and file directory - -#### Get Container Information -```bash -docker inspect -``` - -#### Get Container IP Address -```bash -docker inspect --format '{{ .NetworkSettings.IPAddress }}' -``` - -#### Copy Files Between Local and Container -```bash -docker cp :/ -docker cp :/ -``` - -#### Monitor Docker Container Statistics -```bash -docker stats -``` - -#### Run Container with Restart Policy -```bash -docker run -dit --name server --restart=always ubuntu -``` - -#### Create Custom Docker Image -```bash -docker commit -``` - -#### Save Docker Image to External File -```bash -docker save -o -``` - -#### Remove Docker Image -```bash -docker rmi -f -``` - -#### Load Docker Image from File -```bash -docker load -i -``` +- `docker network ls`: List all Docker networks. +- `docker network create `: Create a Docker network. +- `docker network create --subnet / --gateway --driver= `: Create network with custom settings. +- `docker run -dit --name --network `: Run a container and connect it to a custom network. +- `docker network connect `: Connect a container to a custom network. +- `docker network disconnect `: Disconnect a network from a container.