diff --git a/Containerization & Orchestration/Docker/2-Commands.md b/Containerization & Orchestration/Docker/2-Commands.md index 7e8b83e..a3f0cb0 100755 --- a/Containerization & Orchestration/Docker/2-Commands.md +++ b/Containerization & Orchestration/Docker/2-Commands.md @@ -1,73 +1,92 @@ -# Docker Commands +# Docker Commands Guide ## 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. +- **`/var/lib/docker/`**: The main Docker data directory. +- **`/var/lib/docker/containers/`**: Directory containing container configurations and files. +- **`/var/lib/docker/volumes/`**: Directory where Docker volumes are stored. + +## Installing Docker on Ubuntu + +To install Docker on Ubuntu, run the following commands: -## 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 +# Update the package list and install required packages +sudo apt update && sudo apt install -y ca-certificates curl -# Add the repository to Apt sources: +# Create a directory for Docker's GPG key and download it +sudo install -m 0755 -d /etc/apt/keyrings +curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc +sudo chmod a+r /etc/apt/keyrings/docker.asc + +# Add Docker's official 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 + +# Update the package list and install Docker +sudo apt update && sudo apt install -y 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. +```bash +docker login +``` ### 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. +```bash +docker pull # Pull a Docker image from a repository +docker images # List all Docker images on your system +docker rmi -f # Force remove a Docker image +docker save -o # Save a Docker image as a file +docker load -i # Load a Docker image from a file +``` ### 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. +```bash +docker run # Run a Docker container with specified options +docker run # Run a Docker container +docker run -it # Run a container in interactive mode with a terminal +docker run -dit # Run a container in detached mode with a terminal +docker exec -it # Access the shell of a running container +docker stop # Stop a running Docker container +docker rm # Remove a stopped Docker container +docker rm -f # Forcefully stop and remove a Docker container +docker ps -aq # List the IDs of all containers (running and stopped) +docker ps -aq -f status=exited # List the IDs of all exited containers +docker container prune # Remove all stopped containers +docker commit # Create a new image from a container’s changes +docker inspect # Display detailed information about a container +docker inspect --format '{{ .NetworkSettings.IPAddress }}' # Get the IP address of a container +docker cp :/ # Copy a file from the local system to a container +docker cp :/ # Copy a file from a container to the local system +docker stats # Display a live stream of container resource usage statistics +docker run -dit --name server --restart=always ubuntu # Run a container that automatically restarts on server or service restarts +docker build -t : # Build a 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. +```bash +docker volume ls # List all Docker volumes +docker volume create # Create a new Docker volume +docker volume inspect # Display detailed information about a volume +docker run -dit --name -v : # Run a container with a mounted 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. +```bash +docker network ls # List all Docker networks +docker network create # Create a new 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 specified network +docker network connect # Connect an existing container to a network +docker network disconnect # Disconnect a container from a network +``` \ No newline at end of file