The first Docker workflow to understand is:

Dockerfile -> docker build -> image -> docker run -> container

The Dockerfile describes the environment. The build creates an image. Running the image creates a container.

For the deeper Dockerfile structure, RUN, CMD, layers, and build-cache details, see Dockerfile Structure and Image Layers.

The Core Objects

ObjectWhat It Means
DockerfileText file with instructions for building an image
Build contextFiles available to the build, usually the project directory
ImageRead-only package created from the Dockerfile and build context
ContainerRunning instance of an image

Workflow Visual

Project folder
 
app/
  Dockerfile
  package.json
  src/
      |
      | docker build -t my-app:1.0 .
      v
Image
 
my-app:1.0
      |
      | docker run my-app:1.0
      v
Container
 
running app process

Dockerfile

A Dockerfile is the recipe for the image.

FROM node:lts-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
CMD ["node", "server.js"]

Note: node:lts-alpine keeps the example readable. For production, choose and review explicit base image tags deliberately instead of copying examples blindly.

InstructionMeaning
FROMStart from a base image
WORKDIRSet the working directory inside the image
COPYCopy files from the build context into the image
RUNRun a command while building the image
CMDDefault command when a container starts

Key Insight: RUN happens at build time. CMD is the default command at container start time.

Build

Build reads the Dockerfile and creates an image.

docker build -t my-app:1.0 .
docker build
  -t my-app:1.0   tag/name for the image
  .               build context path

During the build, Docker reads the Dockerfile, uses the build context, downloads the base image if needed, and creates image layers.

Image

An image is not a running app. It is a read-only package that contains:

  • filesystem layers
  • application files
  • runtime dependencies
  • metadata
  • default startup command
Image: my-app:1.0
 
+----------------------------------+
| app code                         |
+----------------------------------+
| installed dependencies           |
+----------------------------------+
| runtime files                    |
+----------------------------------+
| base image layer                 |
+----------------------------------+

Images are portable units. You can build an image locally, push it to a registry, and run it elsewhere if the target environment supports the container runtime model.

Run

Run creates and starts a container from an image.

docker run --name my-app -p 8080:3000 my-app:1.0
docker run
  --name my-app   container name
  -p 8080:3000    host port 8080 -> container port 3000
  my-app:1.0      image to run

At this point, Docker starts the container process using the image filesystem and the runtime settings you provided.

Image vs Container

QuestionImageContainer
Is it running?NoYes
Is it reusable?YesUsually one runtime instance
Is it read-only?Mostly yesHas a writable layer while running
AnalogyClass / templateObject / running instance

You can create many containers from the same image:

Image: my-app:1.0
   |
   +--> Container A
   +--> Container B
   +--> Container C

Beginner Mistakes

MistakeBetter Mental Model
Editing files inside a running container and expecting the image to changeRebuild the image from the Dockerfile
Confusing docker build and docker runBuild creates the image; run creates the container
Putting secrets into a DockerfileUse runtime configuration or a secrets mechanism
Using latest everywhereUse explicit tags when repeatability matters
Forgetting the final . in docker buildThe . is the build context

TL;DR

  • Dockerfile defines how to build the image.
  • docker build creates an image from the Dockerfile and build context.
  • An image is a read-only package, not a running app.
  • docker run creates a container from an image.
  • A container is the running process plus filesystem, networking, and runtime configuration.

Resources

Dockerfile overview Official Docker explanation of Dockerfiles, common instructions, and image layers.

Build, tag, and publish an image Official Docker guide for building and tagging images.

Docker: What is Docker? Official Docker overview covering images, containers, registries, and docker run.