Docker Cheat sheet

Docker Cheat sheet

We have completed the Docker and Docker-compose hands-on and we have learned something interesting from it.

Now it's time to take our Docker skills to the next level by creating a comprehensive cheat sheet of all the commands we've learned so far.

So let's make a cheat sheet that will include commands for both Docker and Docker-Compose, as well as brief explanations of their usage.

Docker Commands

Build an image

  • Build an image using a file named Dockerfile -

    docker build .

  • Build an image using a file named Dockerfile -

    docker build -f "Dockerfile_client" .

  • Give a name to the image

    docker build -t "any_name" .

Manage images

  • List images

    docker image ls

  • Remove an image

    docker image rm <img_name>

  • Tag an image

    docker tag <img_name>:tag

  • Display the image history

    docker history <img_name>

  • Display low-level information about an image

    docker inspect <img_name>

  • Save an image to a tar file

    docker save <image_name> --output <filename.tar>

  • Load an image from a tar archive

    docker load --input <filename.tar>

Create container

  • Create a container from an image

    docker create <img_name>

  • Create and run a container from an image

    docker run <img_name>

  • Create and run a container and give a name to it

    docker run --name <container_name> <img_name>

  • Create and run a container and run it in detached mode.

    docker run -d <img_name>

    docker run --detach <img_name>

  • Create and run a container and set environment variables.

    docker run -e VAR_NAME=VALUE <img_name>

    docker run --env VAR_NAME=VALUE <img_name>

  • Create and run a container and specify a network,

    docker run --network="<any_network>" <img_name>

  • Create and run a container and map the host's port number to the container's port number.

    docker run -p <host_port_no:cntnr_port_no> <img_name>

    docker run -p 8080:8080 <img_name>

  • Create and run a container and remove it when it stops

    docker run --rm <img_name>

  • Create and run a container and mount a shared directory

    docker run -v C:/Downloads/:/home/ubuntu/work <img_name>

  • Create and run a container and go in it

    docker run -it <img_name>

Manage containers

  • Stop a container

    docker stop <container>

  • Start a stopped container

    docker start <container>

  • Pause a container

    docker pause <container>

  • Unause a container

    docker unpause <container>

  • Restart a running or stopped container

    docker restart <container>

  • Export a container to a tar file

    docker export <container> > <filename>.tar

    docker export --output="<filename>.tar" <container>

  • Execute some commands in a running container

    docker exec -it <container> bash

  • Create a new image from a container

    docker commit <container> <img_name>

  • List all running containers

    docker ps

  • List all running and stopped containers

    docker -ps -a

  • Display low-level information about a container

    docker inspect <container>

  • Gather the logs for a container

    docker logs <container>

  • Show container resource usage statistics

    docker stats <container>

Registry Commands

  • Log in to a registry

    docker login

  • Log out from a registry

    docker logout

  • Pull an image from a registry

    docker pull <img_name>

  • Push an image to a registry

    docker push <repo/img_name>:<tag>

Network Commands

  • Create a new network

    docker network create <networkname>

  • Remove a specified network

    docker network rm <networkname>

  • List all networks

    docker network ls

  • Connect a container to a network

    docker network connect <networkname> <container>

  • Disconnect a container from a network

    docker network disconnect <networkname> <container>

  • Display detailed information about a network

    docker network inspect <networkname

Docker-Compose Commands

  • To build images for services

    docker-compose build - Build all services

    docker-compose build <web> - Build a single service named web

  • To create docker containers

    docker-compose up -d - Create all containers

    docker-compose up -d <web> - Create a single container named web

  • To stop and delete all containers, network and associated images

    docker-compose down - Stop and delete all containers

    docker-compose down <web> - Stop and delete a single container named web

  • To list all containers created with their status, port bindings and command.

    docker-compose ps

  • To execute a command to the running container.

    docker-compose exec <container> ls -l

  • To start a stopped container.

    docker-compose start - Start all containers

    docker-compose start <web> - Start single container named web

  • To stop running containers

    docker-compose stop - Stop all containers

    docker-compose stop <web> - Stop single container named web

  • To restart containers

    docker-compose restart - Restart all containers

    docker-compose restart <web> - Restart single container named web

  • To pause running containers

    docker-compose pause - Pause all containers

    docker-compose pause <web> - Pause single container named web

  • To start paused containers

    docker-compose unpause - Start all paused containers

    docker-compose unpause <web> - Start a single paused container named web

  • To remove stopped containers

    docker-compose rm - Remove all stopped containers

    docker-compose rm <web> - Remove a single stopped container named web

That's all for docker and docker-compose. We can use this cheatsheet anytime we work docker and docker-compose

Thanks for reading!

#devops#90DaysOfDevops#TrainWithShubham

Let's connect on Linkedin - linkedin.com/in/namya-khullar-7b5758200