Skip to content

Docker Basics and Syntax Reference

AJprogramming123 edited this page Jun 20, 2025 · 1 revision

Docker Basics and Syntax Reference

This documentation explains key Docker concepts and syntax based on real-world scenarios and examples. It covers image layers, volumes, mounts, tagging, building, and inspecting Docker containers and images.


Docker Images and Layers

Docker images are built in layers. Each instruction in a Dockerfile (RUN, COPY, ADD, ENV, etc.) creates a new layer on top of the previous one.

  • Intermediate image layers are the layers created during the build process before the final image is assembled.
  • These layers represent the state of the filesystem after each Dockerfile step.
  • Docker caches intermediate layers to speed up future builds.
  • If a Dockerfile step hasn't changed, Docker reuses that layer instead of rebuilding it.

View Local Images

You can list your local Docker images using:

docker images

or

docker image ls -a

Custom Formatting

To view your images in a table format with more details:

docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}\t{{.CreatedSince}}\t{{.CreatedAt}}\t{{.Digest}}"

Image Tags and Labels

  • Tags are used to identify different versions of an image.
  • You can assign a tag while building an image using the -t option:
docker build -t big-star-collectibles:v1 .
  • You can also tag an existing image:
docker tag big-star-collectibles:v1 big-star-collectibles:v1-python
  • Tagging during build with no cache:
docker build --no-cache -t big-star-collectibles:v2 .

Inspecting Docker Images

Inspecting images helps with debugging and understanding metadata.

GUI Method

  1. Right-click any image tag in your Docker desktop GUI and choose Inspect.

CLI Method

  1. Use the command:
docker inspect <image_ID>

Volumes and Mounts

What is a Volume?

A volume is a directory stored outside the container, managed by Docker. It’s used to persist data, so even if the container is deleted, the data remains.

Volumes are especially useful for:

  • Databases
  • Logs
  • User uploads

Example:

docker run -v myvolume:/app/data myimage

This mounts the named volume myvolume into the container at /app/data.

What is a Mount?

Mount is a general term. Docker supports three mount types:

  1. volume - Docker-managed persistent storage.
  2. bind - Maps a specific host folder to the container.
  3. tmpfs - Temporary storage in RAM (disappears after container stops).

Bind Mount Example:

docker run -v /home/user/code:/app/code myimage

Explicit Mount Syntax (same as above):

docker run --mount type=bind,source=/home/user/code,target=/app/code myimage

Volume Summary

A Docker volume:

  • Lives outside the container
  • Is managed by Docker
  • Can be reused across containers
  • Is useful for persistent data storage

Dangling Images

Dangling images are images that are not tagged and not referenced by any container. They are usually leftovers from failed builds or old versions.

Prune Dangling Images

To remove all dangling images:

docker image prune

To remove all unused images, not just dangling ones:

docker image prune -a

Common Dockerfile Syntax Explained

# Use an official Python runtime as the base image
FROM python:3.12-rc-bookworm

# Set the working directory in the container to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install the required packages
RUN pip install --no-cache-dir -r requirements.txt

# Set the environment variable for Flask
ENV FLASK_APP=app.py

# Run the command to start the Flask application
CMD ["flask", "run", "--host=0.0.0.0"]

Common Docker CLI Commands

Build an Image

docker build .                # Build from current directory
docker build -t myimage .     # Tag it while building

Search for an Image on Docker Hub

docker search python

Tag an Image

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Example:

docker tag big-start-collectibles:v1 big-start-collectibles:v1-python

Run a Container

docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]

Example:

docker run -d -p 5000:5000 myimage:latest

Summary

This documentation provides a practical reference for building, tagging, running, inspecting, and managing Docker images, containers, and volumes. Use it as a cheat sheet while working with Docker locally or in production environments.

Clone this wiki locally