Docker Basic Commands

Table of Contents

- [Introduction](#introduction) - [Basic Commands](#basic-commands) - [Install Docker Desktop and Docker Engine](#install-docker-desktop-and-docker-engine) - [Remove completely Docker](#remove-completely-docker) - [List containers](#list-containers) - [Pull an image](#pull-an-image) - [Push an image](#push-an-image) - [Inspect and pull an image with GHCR](#inspect-and-pull-an-image-with-ghcr) - [See a docker image (no download)](#see-a-docker-image-no-download) - [Build a container](#build-a-container) - [List all available docker images](#list-all-available-docker-images) - [Run a container](#run-a-container) - [Run a new command in an existing container](#run-a-new-command-in-an-existing-container) - [Bash shell into container](#bash-shell-into-container) - [Pass arguments with a bash script and a Dockerfile](#pass-arguments-with-a-bash-script-and-a-dockerfile) - [Copy files from a container to the local computer](#copy-files-from-a-container-to-the-local-computer) - [Delete all the containers, images and volumes](#delete-all-the-containers-images-and-volumes) - [Kill all the Docker processes](#kill-all-the-docker-processes) - [Output full logs for all containers](#output-full-logs-for-all-containers) - [Resources Usage](#resources-usage) - [Examine containers with size](#examine-containers-with-size) - [Examine disks usage](#examine-disks-usage) - [Wasted Resources](#wasted-resources) - [Prune the Docker logs](#prune-the-docker-logs) - [Prune the Docker containers](#prune-the-docker-containers) - [Remove unused and untagged local container images](#remove-unused-and-untagged-local-container-images) - [Clean up and delete all unused container images](#clean-up-and-delete-all-unused-container-images) - [Clean up container images based on a given timeframe](#clean-up-container-images-based-on-a-given-timeframe) - [Command Combinations](#command-combinations) - [Kill all running containers](#kill-all-running-containers) - [Stop all running containers](#stop-all-running-containers) - [Delete all stopped containers](#delete-all-stopped-containers) - [Delete all images](#delete-all-images) - [Update and stop a container in a crash-loop](#update-and-stop-a-container-in-a-crash-loop) - [References](#references) *** ## Introduction We present here a quick introduction to Docker. We cover basic commands, as well as command combinations. Understanding the following should give system administrators confidence when it comes to using Docker efficiently. The following can serve as a quick reference guide when deploying workloads on the ThreeFold Grid and using Docker in general. We invite the readers to consult the [official Docker documentation](https://docs.docker.com/) for more information. ## Basic Commands ### Install Docker Desktop and Docker Engine You can install [Docker Desktop](https://docs.docker.com/get-docker/) and [Docker Engine](https://docs.docker.com/engine/install/) for Linux, MAC and Windows. Follow the official Docker documentation for the details. Note that the quickest way to install Docker Engine is to use the convenience script: ``` curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh ``` ### Remove completely Docker To completely remove docker from your machine, you can follow these steps: * List the docker packages ``` dpkg -l | grep -i docker ``` * Purge and autoremove docker ``` apt-get purge -y docker-engine docker docker.io docker-ce docker-ce-cli docker-compose-plugin apt-get autoremove -y --purge docker-engine docker docker.io docker-ce docker-compose-plugin ``` * Remove the docker files and folders ``` rm -rf /var/lib/docker /etc/docker rm /etc/apparmor.d/docker groupdel docker rm -rf /var/run/docker.sock ``` You can also use the command **whereis docker** to see if any Docker folders and files remain. If so, remove them with ### List containers * List only running containers ``` docker ps ``` * List all containers (running + stopped) ``` docker ps -a ``` ### Pull an image To pull an image from [Docker Hub](https://hub.docker.com/): * Pull an image ``` docker pull ``` * Pull an image with the tag ``` docker pull :tag ``` * Pull all tags of an image ``` docker pull -a ``` ### Push an image To pull an image to [Docker Hub](https://hub.docker.com/): * Push an image ``` docker push ``` * Push an image with the tag ``` docker push :tag ``` * Push all tags of an image ``` docker pull -a ``` ### Inspect and pull an image with GHCR * Inspect the docker image ``` docker inspect ghcr.io//: ``` * Pull the docker image ``` docker pull ghcr.io//: ``` ### See a docker image (no download) If you want to see a docker image without downloading the image itself, you can use Quay's [Skopeo tool](https://github.com/containers/skopeo), a command line utility that performs various operations on container images and image repositories. ``` docker run --rm quay.io/skopeo/stable list-tags docker://ghcr.io// ``` Make sure to write the proper information for the repository and the image. To install Skopeo, read [this documentation](install.md). ### Build a container Use **docker build** to build a container based on a Dockerfile * Build a container based on current directory Dockerfile ``` docker build . ``` * Build a container and store the image with a given name * Template ``` docker build -t ":" ``` * Example ``` docker build -t newimage:latest ``` * Build a docker container without using the cache ``` docker build --no-cache ``` ### List all available docker images ``` docker images ``` ### Run a container To run a container based on an image, use the command **docker run**. * Run an image ``` docker run ``` * Run an image in the background (run and detach) ``` docker run -d ``` * Run an image with CLI input ``` docker run -it ``` You can combine arguments, e.g. **docker run -itd**. You can also specify the shell, e.g. **docker run -it /bin/bash** ### Run a new command in an existing container To run a new command in an existing container, use **docker exec**. * Execute interactive shell on the container ``` docker exec -it sh ``` ### Bash shell into container * Bash shell into a container ``` docker exec -i -t /bin/bash ``` * Bash shell into a container with root ``` docker exec -i -t -u root /bin/bash ``` Note: if bash is not available, you can use `/bin/sh` ### Pass arguments with a bash script and a Dockerfile You can do the following to pass arguments with a bash script and a Dockerfile. ```sh # script_example.sh #!/bin/sh echo This is the domain: $env_domain echo This is the name: $env_name echo This is the password: $env_password ``` * File `Dockerfile` ```Dockerfile FROM ubuntu:latest ARG domain ARG name ARG password ENV env_domain $domain ENV env_name $name ENV env_password $password COPY script_example.sh . RUN chmod +x /script_example.sh CMD ["/script_example.sh"] ``` ### Copy files from a container to the local computer ``` docker cp : ``` ### Delete all the containers, images and volumes * To delete all containers: ``` docker compose rm -f -s -v ``` * To delete all images: ``` docker rmi -f $(docker images -aq) ``` * To delete all volumes: ``` docker volume rm $(docker volume ls -qf dangling=true) ``` * To delete all containers, images and volumes: ``` docker compose rm -f -s -v && docker rmi -f $(docker images -aq) && docker volume rm $(docker volume ls -qf dangling=true) ``` ### Kill all the Docker processes * To kill all processes: ``` killall Docker && open /Applications/Docker.app ``` ### Output full logs for all containers The following command output the full logs for all containers in the file **containers.log**: ``` docker compose logs > containers.log ``` ## Resources Usage ### Examine containers with size ``` docker ps -s ``` ### Examine disks usage * Basic mode ``` docker system df ``` * Verbose mode ``` docker system df -v ``` ## Wasted Resources ### Prune the Docker logs ``` docker system prune ``` ### Prune the Docker containers You can use the prune function to delete all stopped containers: ``` docker container prune ``` ### Remove unused and untagged local container images The following is useful if you want to clean up local filesystem: ``` docker image prune ``` ### Clean up and delete all unused container images ``` docker image prune -a ``` ### Clean up container images based on a given timeframe To clean up container images created X hours ago, you can use the following template (replace with a number): ``` docker image prune -a --force --filter "until=h" ``` To clean up container images created before a given date, you can use the following template (replace with the complete date): ``` docker image prune -a --force --filter "until=" ``` Note: An example of a complete date would be `2023-01-04T00:00:00` ## Command Combinations ### Kill all running containers ``` docker kill $(docker ps -q) ``` ### Stop all running containers ``` docker stop $(docker ps -a -q) ``` ### Delete all stopped containers ``` docker rm $(docker ps -a -q) ``` ### Delete all images ``` docker rmi $(docker images -q) ``` ### Update and stop a container in a crash-loop ``` docker update –restart=no && docker stop ``` ## References * Docker Manual - https://docs.docker.com/ * Code Notary - https://codenotary.com/blog/extremely-useful-docker-commands