Intermediate Interview Questions

CMD vs ENTRYPOINT?

CMD sets the default command and is replaced by arguments to docker run. ENTRYPOINT sets the executable that always runs; CMD (or docker run args) become its arguments. Use the exec (JSON) form so the process receives signals.

COPY vs ADD?

Both copy files into the image. ADD can also download URLs and auto-extract local tar archives, which makes it less predictable. Use COPY unless you need those features.

How do you persist data?

With volumes (managed by Docker, recommended for databases) or bind mounts (a host folder, handy for development). Data in the container's writable layer is lost when the container is removed.

How do containers communicate?

Put them on the same user-defined network (Compose does this automatically). Docker's DNS resolves container/service names, so an API reaches the database at db:5432. Publish ports only for traffic coming from outside.

What is Docker Compose?

A tool that defines a multi-container application (services, networks, volumes, environment) in compose.yaml and manages it with docker compose up / down. Mostly used for local development and simple single-host deployments.

How do you make builds faster?

  • Order layers: dependency files and install steps before copying source code.
  • Use .dockerignore.
  • Use BuildKit cache mounts and CI build caches.
  • Use multi-stage builds and slim base images.