ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Useful Commands in Docker

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='3' AND `tutorial_submenu`='118' AND `tutorial_status`=1 LIMIT 1

Useful Commands in Docker

Here’s a list of useful Docker commands that can help you work more efficiently with Docker containers, images, networks, and volumes.

Container Commands

  1. List Running Containers

    docker ps

    Shows all currently running containers.

  2. List All Containers (Including Stopped)

    docker ps -a

    Shows all containers, including stopped ones.

  3. Run a New Container

    docker run -it ubuntu

    Runs a container interactively (-it allows you to interact with the container) with the ubuntu image.

  4. Start a Container

    docker start <container_id>

    Starts a stopped container (replace <container_id> with the actual container ID).

  5. Stop a Running Container

    docker stop <container_id>

    Stops a running container.

  6. Restart a Container

    docker restart <container_id>

    Restarts a container.

  7. Remove a Container

    docker rm <container_id>

    Removes a container (it must be stopped first).

  8. Attach to a Running Container

    docker attach <container_id>

    Attach your terminal to a running container.

  9. Execute a Command in a Running Container

    docker exec -it <container_id> <command>

    Runs a command inside a running container. Example:

    docker exec -it <container_id> bash

  10. View Container Logs

    docker logs <container_id>

    Shows the logs of a container (useful for debugging).

Image Commands

  1. List Docker Images

    docker images

    Lists all Docker images available on your system.

  2. Pull an Image from Docker Hub

    docker pull <image_name>

    Example:

    docker pull ubuntu

  3. Build an Image from a Dockerfile

    docker build -t <image_name> .

    Builds an image from the current directory (where the Dockerfile is located). Example:

    docker build -t my-ubuntu-image .

  4. Remove an Image

    docker rmi <image_id>

    Removes an image from the system (useful for cleanup).

  5. Tag an Image

    docker tag <image_id> <new_image_name>

    Tags an existing image with a new name. Example:

    docker tag my-ubuntu-image myusername/my-ubuntu-image

  6. Push an Image to Docker Hub

    docker push <image_name>

    Pushes an image to a Docker registry (like Docker Hub). Example:

    docker push myusername/my-ubuntu-image

  7. View Image Details

    docker inspect <image_id>

    Provides detailed information about an image or container.

Volume Commands

  1. List Docker Volumes

    docker volume ls

    Lists all volumes available on your system.

  2. Create a Docker Volume

    docker volume create <volume_name>

    Creates a new volume. Example:

    docker volume create my-volume

  3. Remove a Docker Volume

    docker volume rm <volume_name>

    Removes a Docker volume.

  4. Inspect a Volume

    docker volume inspect <volume_name>

    Shows detailed information about a specific volume.

  5. List Mounted Volumes in a Container

    docker inspect <container_id> --format='{{json .Mounts}}'

Network Commands

  1. List Docker Networks

    docker network ls

    Lists all networks created by Docker.

  2. Create a Docker Network

    docker network create <network_name>

    Creates a new Docker network.

  3. Connect a Container to a Network

    docker network connect <network_name> <container_name>

    Connects a running container to a network.

  4. Disconnect a Container from a Network

    docker network disconnect <network_name> <container_name>

    Disconnects a container from a network.

  5. Inspect a Docker Network

    docker network inspect <network_name>

    Shows detailed information about a network.

Docker Compose Commands

  1. Start Docker Compose Services

    docker-compose up

    Starts all the services defined in a docker-compose.yml file.

  2. Start Docker Compose Services in Detached Mode

    docker-compose up -d

    Starts the services in detached mode (in the background).

  3. Stop Docker Compose Services

    docker-compose down

    Stops and removes all containers, networks, and volumes defined in the docker-compose.yml file.

  4. Build Docker Compose Services

    docker-compose build

    Builds the images for the services defined in docker-compose.yml.

  5. View Docker Compose Logs

    docker-compose logs

    Displays the logs of the services started by Docker Compose.

  6. Run a One-Off Command with Docker Compose

    docker-compose run <service_name> <command>

    Runs a one-off command for a service. For example:

    docker-compose run web bash

System Commands

  1. View Docker System Information

    docker info

    Displays detailed information about the Docker system, including the version, storage driver, number of containers, images, etc.

  2. View Docker Version

    docker --version

    Displays the current version of Docker installed on your system.

  3. Clean Up Docker System (Remove Unused Containers, Images, and Volumes)

    docker system prune

    Cleans up unused Docker objects, including containers, images, and volumes. You can add the -a flag to also remove unused images:

    docker system prune -a

  4. View Disk Usage

    docker system df

    Displays the disk usage of Docker objects (images, containers, volumes).

Docker Logs and Monitoring

  1. View Container Logs

    docker logs <container_id>

    Shows the logs of a specific container.

  2. Tail the Logs in Real-Time

    docker logs -f <container_id>

    Follows and streams the logs of a running container.

  3. View Resource Usage (CPU, Memory, etc.)

    docker stats

    Displays real-time resource usage statistics (CPU, memory, etc.) of running containers.

Helpful Shortcuts

  • Run a container in detached mode (background):

    docker run -d <image_name>

  • Get the shell of a running container:

    docker exec -it <container_id> /bin/bash

  • Show all networks:

    docker network ls

  • Show all volumes:

    docker volume ls

Conclusion

These are some of the most useful Docker commands that help you manage and interact with containers, images, networks, and volumes effectively. Whether you're starting containers, building images, or performing system cleanup, these commands are essential for daily Docker operations.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql