Docker 🛳

Installation :

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo 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 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 "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

1. Running a Container

  • Basic Run Command:

    docker run <options> <image>
  • Example: Run a container in detached mode with port mapping:

    docker run -d -p 443:443 --name <name> <docker_image_name>
    • -d: Run container in detached mode.

    • -p 443:443: Map port 443 on the host to port 443 on the container.

2. List Running Containers

  • Command:

    docker ps
    • Lists all running containers.

3. List All Containers (Including Stopped)

  • Command:

    docker ps -a

4. Stopping a Container

  • Command:

    docker stop <container_name_or_id>
    • Stops a running container.

  • Example:

    docker stop python

5. Starting a Container

  • Command:

    docker start <container_name_or_id>

6. Remove a Container

  • Command:

    docker rm <container_name_or_id>

7. Remove a Running Container (Force)

  • Command:

    docker rm -f <container_name_or_id>

8. Viewing Container Logs

  • Command:

    docker logs <container_name_or_id>

9. Run a Container with a Specific Command

  • Example: Running a container with a custom command:

    docker run -d --name mycontainer ubuntu sleep 3600
    • This runs the Ubuntu image and starts the container with the command sleep 3600 (for 1 hour).

10. Building a Docker Image

  • Command:

    docker build -t <image_name> <path_to_dockerfile>
  • Example:

    docker build -t my_image .

11. List All Docker Images

  • Command:

    docker images

12. Remove a Docker Image

  • Command:

    docker rmi <image_name_or_id>

13. Viewing Docker Info

  • Command:

    docker info
    • Displays information about your Docker installation and environment.

14. Accessing a Running Container (Interactive Mode)

  • Command:

    docker exec -it <container_name_or_id> /bin/bash
    • Starts an interactive bash session inside the container.

15. Viewing Docker Version

  • Command:

    docker --version

16. Remove Unused Docker Resources (Images, Containers, Volumes)

  • Command:

    docker system prune

17. Building and Running a Docker Compose Application

  • Command:

    docker-compose up
    • Starts the services defined in docker-compose.yml.

18. Stopping All Containers

  • Command:

    docker stop $(docker ps -q)

19. Accessing a Container's File System

  • Command:

    docker cp <container_name_or_id>:<container_path> <host_path>

20. Viewing Docker Container Statistics

  • Command:

    docker stats
    • Displays real-time statistics about containers' CPU and memory usage.

Last updated