What are Docker labels?
Why Interviewers Ask This
Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.
Answer
Docker labels are key-value metadata attached to Docker objects (images, containers, networks, volumes). They do not affect the container's behavior but provide useful metadata for organization, tooling, and filtering. Defining labels in Dockerfile: LABEL maintainer="team@company.com"\nLABEL version="1.0" description="MyApp API Service"\nLABEL org.opencontainers.image.source="https://github.com/org/repo"\nLABEL build.date="2024-01-15". Using OCI standard labels (org.opencontainers.image.*) is recommended for interoperability. Add labels at runtime: docker run --label env=production --label team=backend myapp. In Compose: labels: - "traefik.enable=true" - "traefik.http.routers.myapp.rule=Host(myapp.example.com)" (Traefik reverse proxy reads these to auto-configure routing). Filter by label: docker ps --filter label=env=production; docker images --filter label=version=1.0. Inspect labels: docker inspect --format "{{json .Config.Labels}}" mycontainer. Common use cases: CI/CD metadata (build number, git commit SHA, branch), Prometheus scrape configuration, reverse proxy routing (Traefik/Nginx), runbook/documentation links, team ownership, compliance/security metadata.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.
Previous
What is the difference between COPY and RUN in terms of Docker layers?
Next
What is init in Docker containers?