Docker Commands: From Beginner to Advanced for DevOps Engineers

Docker is a powerful tool for containerization, which is essential for modern DevOps practices. Here’s a breakdown of Docker commands from beginner to advanced levels:

Beginner Commands

  1. docker --version
    Check the installed Docker version.

  2. docker pull <image>
    Download a Docker image from Docker Hub.
    Example: docker pull ubuntu

  3. docker run <image>
    Create and start a container from an image.
    Example: docker run ubuntu

  4. docker ps
    List running containers.

  5. docker ps -a
    List all containers, including stopped ones.

  6. docker stop <container_id>
    Stop a running container.
    Example: docker stop my_container

  7. docker start <container_id>
    Start a stopped container.

  8. docker rm <container_id>
    Remove a stopped container.

  9. docker rmi <image>
    Remove an image.
    Example: docker rmi ubuntu

  10. docker exec -it <container_id> /bin/bash
    Access a running container’s shell interactively.

  11. docker logs <container_id>
    View the logs of a container.

Intermediate Commands

  1. docker build -t <image_name>:<tag> .
    Build an image from a Dockerfile.
    Example: docker build -t myapp:latest .

  2. docker tag <source_image> <target_image>
    Tag an image with a new name.
    Example: docker tag myapp:latest myapp:v1.0

  3. docker-compose up
    Start up services defined in a docker-compose.yml file.
    You may use -d for detached mode.

  4. docker-compose down
    Stop and remove containers defined in a docker-compose.yml file.

  5. docker network ls
    List Docker networks.

  6. docker network inspect <network_name>
    Get detailed information about a network.

  7. docker volume ls
    List Docker volumes.

  8. docker volume inspect <volume_name>
    Get detailed information about a volume.

  9. docker-compose build
    Build or rebuild services defined in a docker-compose.yml file.

  10. docker save -o <file>.tar <image>
    Save an image to a tar archive.
    Example: docker save -o myapp.tar myapp:latest

  11. docker load -i <file>.tar
    Load an image from a tar archive.
    Example: docker load -i myapp.tar

Advanced Commands

  1. docker run --rm <image>
    Automatically remove the container when it exits.

  2. docker volume create <volume_name>
    Create a new Docker volume.

  3. docker network create <network_name>
    Create a new Docker network.

  4. docker run --network <network_name> <image>
    Run a container connected to a specific network.

  5. docker-compose -f <file.yml> up
    Specify a different docker-compose file.

  6. docker exec -it <container_id> <command>
    Run a specific command in a running container.
    Example: docker exec -it my_container ls /

  7. docker system prune
    Remove unused data, including stopped containers, networks, images, and optionally, volumes.

  8. docker info
    Display system-wide information about Docker.

  9. docker inspect <container_or_image>
    Return detailed information in JSON format about containers or images.

  10. docker-compose logs
    View logs from all services in a docker-compose setup.

  11. docker-compose up --build
    Build images before starting containers.

  12. docker service create --name <service_name> <image>
    Create a new service in Docker Swarm mode.

  13. docker stack deploy -c <file>.yml <stack_name>
    Deploy a stack to Docker Swarm mode.

  14. docker swarm init
    Initialize a Docker Swarm cluster.

  15. docker swarm join
    Join a node to a Docker Swarm cluster.

  16. docker secret create <secret_name> <file>
    Create a Docker secret from a file for use in Docker Swarm.

  17. docker secret inspect <secret_name>
    Inspect a Docker secret.

  18. docker secret rm <secret_name>
    Remove a Docker secret.

These commands cover a broad spectrum of Docker operations and should give you a solid foundation for working with Docker in various scenarios. 

Post a Comment

0 Comments