Write Your First Dockerfile, and Push to Docker Hub

πŸ“ Key Notes & Concepts

Hardware & Setup

  • Hardware Requirements: Docker Desktop requires at least 4 GB of RAM, but 8 to 12 GB of RAM and 4 CPUs are highly recommended for seamless operation on your local machine.

  • Cloud Virtual Machines: If your personal machine lacks the required specs, a great alternative is to spin up an Ubuntu VM on cloud providers like Azure or GCP and install Docker Engine there.

  • Authentication: Docker Desktop handles authentication locally via your desktop login context. If you are using a cloud VM, you will need to manually authenticate via the CLI.

Architecture & Concepts

  • Registries vs. Repositories:
    • A Docker registry (like Docker Hub) contains multiple repositories.
    • Repositories are collections of Docker images.
    • Images within a single repository are differentiated by their β€œtags” or versions.
  • Default Registries: When you pull an image without specifying a full URL, Docker assumes you are pulling from docker.io (Docker Hub) and uses the default library namespace.
  • Container Lifecycle: Containers are designed to only run as long as they are actively processing a task. Once the task finishes (e.g., printing a message), the container will automatically stop and enter an β€œexited” state.
  • Pushing Etiquette: You cannot push a locally named image directly. You must tag the image with your specific registry username, image name, and version tag before it can be pushed successfully.

Dockerfile Layers & Instructions

  • FROM: Tells the Docker daemon what base image to use.

  • CMD & ENTRYPOINT: These instructions only update the metadata of the image. They do not add a new layer to the image.

  • RUN: Executes commands and explicitly creates a new layer on top of the image.


πŸ’» Docker Commands Cheat Sheet

General & Info

  • docker --version or docker version: Verifies your installation and displays the installed version of Docker.

  • docker --help: Lists all available Docker commands and common usage instructions.

  • docker info: Displays wide-ranging characteristics and system information regarding your Docker daemon and installation.

Image Management

  • docker images: Lists all Docker images currently stored locally on your machine.

  • docker pull <image_name>: Downloads the latest image (e.g., ubuntu) from the remote registry to your local machine.

  • docker build -t <image_name> .: Builds a Docker image using the Dockerfile located in your current directory (.), and tags it with a provided name.

  • docker history <image_name>: Displays the history of the image, showing the layers that were added to create it.

  • docker inspect <image_name>: Provides a deep dive into the metadata and specific configuration details associated with the image.

  • docker rmi <image_name> -f: Forcefully removes a specified local Docker image.

Container Management

  • docker run <image_name>: Runs a container using the specified Docker image.

  • docker ps: Lists all actively running containers.

  • docker ps -a: Lists all containers, including those that are stopped or have exited.

Registry & Publishing

  • cat ~/.docker/config.json: Views your local authentication context to check which user is currently logged in.

  • docker login --username <username> --password-stdin: Prompts manual terminal authentication with Docker Hub.

  • docker tag <current_name> <username>/<new_name>:<tag>: Assigns a new alias/tag to an existing image, effectively preparing it to be pushed to your repository.

  • docker push <username>/<image_name>:<tag>: Pushes the correctly tagged image up to your Docker Hub repository.