# Docker Commands and Concepts ## Docker Concepts - **Stateless**: Do not save your data (like Nginx). - **Stateful**: Save your data. ## Docker Data Directory - `/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. ## Docker Installtion (Ubuntu) ```bash apt update && apt install ca-certificates curl && install -m 0755 -d /etc/apt/keyrings && curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc && chmod a+r /etc/apt/keyrings/docker.asc # Add the repository to Apt sources: echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ sudo tee /etc/apt/sources.list.d/docker.list > /dev/null apt update && apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin ``` ## Docker CLI Commands ### Authentication - `docker login`: Login to Docker Hub with CLI. ### Image Management - `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. ### Container Management - `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. ### Volume Management - `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. ### Network Management - `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.