Docker Insights and Quick Reference

Docker Insights and Quick Reference

Basic Docker Concepts and Terms:

  1. Docker Container: A runtime instance of a Docker image.

  2. Docker Image: A lightweight, stand-alone, executable package that includes everything needed to run a piece of software.

  3. Dockerfile: A text document that contains all the commands a user could call on the command line to assemble an image.

  4. Docker Hub: A cloud-based registry service where Docker users and partners create, test, store, and distribute container images.

  5. Docker Compose: A tool for defining and running multi-container Docker applications.

Basic Docker Commands:

  • Display Docker version.

    docker --version:

  • Display system-wide information.

    docker info:

  • Run a Docker container from an image.

    docker run image:

  • List running Docker containers.

    docker ps:

  • List all Docker containers including stopped

    docker ps -a:

  • Stop a running container.

    docker stop container_id:

  • Remove a Docker container.

    docker rm container_id:

  • List Docker images.

    docker images:

  • Remove a Docker image.

    docker rmi image_id:

  • Pull an image from a Docker registry (Docker Hub by default).

    docker pull image:

  • Push an image to a Docker registry.

    docker push image:

  • Execute a command in a running container.

    docker exec -it container_id command:

  • Fetch the logs of a container.

    docker logs container_id:

  • Starts one or more stopped containers.

    docker start:

  • Stops one or more running containers.

    docker stop:

  • Builds an image from a Dockerfile.

    docker build:

  • Pulls an image or a repository from a registry.

    docker pull:

  • Pushes an image or a repository to a registry.

    docker push:

  • Exports a container’s filesystem as a tar archive.

    docker export:

  • Runs a command in a run-time container.

    docker exec:

  • Searches the Docker Hub for images.

    docker search:

  • Attaches to a running container.

    docker attach:

  • Creates a new image from a container’s changes.

    docker commit:

Intermediate Docker Commands:

  • Run a Docker container in detached mode.

    docker run -d image:

  • Map a port from the host to a container.

    docker run -p host_port:container_port image:

  • Mount a volume from the host to a container.

    docker run -v host_volume:container_volume image:

  • To Set environment variables in a container.

    docker run -e VAR=VALUE image:

  • Return low-level information on Docker objects.

    docker inspect container_id/image_id:

  • Build a Docker image with a tag from a Dockerfile in the current directory.

    docker build -t tag .:

  • Tag an image with a new tag.

    docker tag image new_tag:

Dockerfile Commands:

  • Set the base image.

    FROM image:

  • To Run a command

    RUN command: .

  • Set a default command that will run when the container starts.

    CMD command:

  • To set environment variables.

    ENV VAR=VALUE:

  • To copy files from a source to the container's filesystem at the destination.

    ADD source destination:

  • To copy new files or directories from a source and add them to the filesystem of the container at the destination.

    COPY source destination:

  • To allow you to configure a container that will run as an executable.

    ENTRYPOINT command:

  • To add metadata to an image.

    LABEL:

  • To inform Docker that the container listens on the specified network ports at runtime.

    EXPOSE:

  • To allow you to configure a container that will run as an executable.

    ENTRYPOINT:

  • To create a mount point with the specified name and marks it as holding externally mounted volumes from a native host or other containers.

    VOLUME:

  • Sets the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile.

    USER:

  • Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow it in the Dockerfile.

    WORKDIR:

  • Defines a variable that users can pass at build-time to the builder with the docker build command.

    ARG:

  • Adds a trigger instruction when the image is used as the base for another build. ONBUILD:

Docker Compose Commands:

  • Create and start containers.

    docker-compose up:

  • Stop and remove containers, networks, images, and volumes.

    docker-compose down:

  • Build or rebuild services.

    docker-compose build:

  • View output from containers.

    docker-compose logs:

  • Restart services.

    docker-compose restart:

  • Pause services.

    docker-compose pause:

  • Unpause services.

    docker-compose unpause:

  • Starts existing containers for a service.

    docker-compose start:

  • Stops running containers without removing them.

    docker-compose stop:

  • Validates and views the compose file.

    docker-compose config:

Docker Networking

  • List networks.

    docker network ls:

  • Create a network.

    docker network create network:

  • Remove a network.

    docker network rm network:

  • Display detailed information on one or more networks.

    docker network inspect network:

  • Bridge: Docker's default networking driver. If you don't specify a driver, this is the type of network you are creating.

  • Host: For standalone containers, remove network isolation between the container and the Docker host.

  • Overlay: Networks connect multiple Docker daemons together and enable swarm services to communicate with each other

  • Macvlan: Assigns a MAC address to a container, making it appear as a physical device on your network.

Docker Volumes:

  • List volumes.

    docker volume ls:

  • Create a volume.

    docker volume create volume:

  • Remove a volume.

    docker volume rm volume:

  • Display detailed information on one or more volumes.

    docker volume inspect volume:

Docker Object Commands:

  • Manages images.

    docker image:

  • Manages containers.

    docker container:

  • Manages networks.

    docker network:

  • Manages volumes.

    docker volume:

  • Manages Docker secrets.

    docker secret:

  • Manages plugins.

    docker plugin:

Docker Advanced Commands:

  • Show the history of an image.

    docker history image:

  • Save an image to a tar archive.

    docker save image > file:

  • Load an image from a tar archive.

    docker load < file:

  • Create a new image from a container’s changes.

    docker commit container image:

Docker Troubleshooting and Monitoring

  • Display a live stream of container(s) resource usage statistics.

    docker stats:

  • Display the space usage of Docker daemon entities.

    docker system df:

  • Return low-level information on Docker objects.

    docker inspect:

  • Get real-time events from the server.

    docker events:

  • Fetch the logs of a container.

    docker logs:

Docker System Commands:

  • Displays system-wide information.

    docker info:

  • Shows the Docker version information.

    docker version:

  • Shows Docker disk usage.

    docker system df:

  • Get's real-time events from the server.

    docker system events:

  • Removes unused data.

    docker system prune:

Docker Security

  • Create a secret from a file.

    docker secret create secret file:

  • List secrets.

    docker secret ls:

  • Remove a secret.

    docker secret rm secret: