Essential Docker Commands

TechBlog
Docker

Managing Containers

Containers are the building blocks of Docker. Here are commands to manage container lifecycle:

  • Run a new container:

    docker run -d -p 80:80 nginx

    Runs a container using the nginx image in detached mode, mapping port 80 of the host to port 80 of the container.

  • Start and stop a container:

    docker start my_container 
    docker stop my_container

    Starts or stops an existing container named my_container.

  • Restart a container:

    docker restart my_container
  • Pause and unpause container processes:

    docker pause my_container 
    docker unpause my_container

    Temporarily halts all processes inside a container and resumes them.

  • Kill a container:

    docker kill my_container

    Immediately stops a running container by sending a SIGKILL signal.

  • Remove a container:

    docker rm my_container

    Deletes a container named my_container.

Managing Images

Images serve as the blueprint for containers. Here’s how to work with them:

  • Build an image:

    docker build -t my_image .

    Creates a Docker image named my_image using the Dockerfile in the current directory.

  • Pull an image:

    docker pull ubuntu

    Downloads the latest Ubuntu image from Docker Hub.

  • Push an image:

    docker push my_user/my_image

    Uploads the image my_image to a repository under the username my_user.

  • List and remove images:

    docker images docker rmi my_image

    Displays all locally available images or removes an image.

Managing Networks

Docker networks facilitate communication between containers.

  • Create and remove networks:

    docker network create my_network 
    docker network rm my_network
  • Connect or disconnect a container to/from a network:

    docker network connect my_network my_container 
    docker network disconnect my_network my_container
  • List all networks:

    docker network ls

Volume Management

Volumes are used to persist data between container restarts.

  • Create a volume:

    docker volume create my_volume
  • List and remove volumes:

    docker volume ls 
    docker volume rm my_volume

Information and Logs

These commands provide insights into containers and system state:

  • List running or all containers:

    docker ps 
    docker ps -a
  • Fetch logs and inspect details:

    docker logs my_container 
    docker inspect my_container
  • Monitor resource usage:

    docker stats my_container

Docker Compose

For managing multi-container applications, Docker Compose is invaluable:

  • Start and stop all services in a Compose file:

    docker-compose up 
    docker-compose down

System Commands

Get system-wide information or Docker version details:

  • Display Docker info:

    docker info
  • Check Docker version:

    docker version

Miscellaneous Commands

  • Log in and out of Docker Hub:

    docker login -u my_user -p my_password 
    docker logout
  • Search for images:

    docker search nginx


Sajit Khadka

Sajit Khadka

Sajit Khadka is a software developer and tech enthusiast with a passion for exploring coding challenges and sharing insights from his development journey.

Comments (0)