-
Notifications
You must be signed in to change notification settings - Fork 0
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 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.
You can list your local Docker images using:
docker imagesor
docker image ls -aTo 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}}"- Tags are used to identify different versions of an image.
- You can assign a tag while building an image using the
-toption:
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 images helps with debugging and understanding metadata.
- Right-click any image tag in your Docker desktop GUI and choose Inspect.
- Use the command:
docker inspect <image_ID>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 myimageThis mounts the named volume myvolume into the container at /app/data.
Mount is a general term. Docker supports three mount types:
-
volume- Docker-managed persistent storage. -
bind- Maps a specific host folder to the container. -
tmpfs- Temporary storage in RAM (disappears after container stops).
Bind Mount Example:
docker run -v /home/user/code:/app/code myimageExplicit Mount Syntax (same as above):
docker run --mount type=bind,source=/home/user/code,target=/app/code myimageA Docker volume:
- Lives outside the container
- Is managed by Docker
- Can be reused across containers
- Is useful for persistent data storage
Dangling images are images that are not tagged and not referenced by any container. They are usually leftovers from failed builds or old versions.
To remove all dangling images:
docker image pruneTo remove all unused images, not just dangling ones:
docker image prune -a# 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"]docker build . # Build from current directory
docker build -t myimage . # Tag it while buildingdocker search pythondocker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]Example:
docker tag big-start-collectibles:v1 big-start-collectibles:v1-pythondocker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]Example:
docker run -d -p 5000:5000 myimage:latestThis 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.