The first Docker workflow to understand is:
Dockerfile -> docker build -> image -> docker run -> containerThe 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
| Object | What It Means |
|---|---|
| Dockerfile | Text file with instructions for building an image |
| Build context | Files available to the build, usually the project directory |
| Image | Read-only package created from the Dockerfile and build context |
| Container | Running 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 processDockerfile
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-alpinekeeps the example readable. For production, choose and review explicit base image tags deliberately instead of copying examples blindly.
| Instruction | Meaning |
|---|---|
FROM | Start from a base image |
WORKDIR | Set the working directory inside the image |
COPY | Copy files from the build context into the image |
RUN | Run a command while building the image |
CMD | Default command when a container starts |
Key Insight:
RUNhappens at build time.CMDis 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 pathDuring 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.0docker run
--name my-app container name
-p 8080:3000 host port 8080 -> container port 3000
my-app:1.0 image to runAt this point, Docker starts the container process using the image filesystem and the runtime settings you provided.
Image vs Container
| Question | Image | Container |
|---|---|---|
| Is it running? | No | Yes |
| Is it reusable? | Yes | Usually one runtime instance |
| Is it read-only? | Mostly yes | Has a writable layer while running |
| Analogy | Class / template | Object / running instance |
You can create many containers from the same image:
Image: my-app:1.0
|
+--> Container A
+--> Container B
+--> Container CBeginner Mistakes
| Mistake | Better Mental Model |
|---|---|
| Editing files inside a running container and expecting the image to change | Rebuild the image from the Dockerfile |
Confusing docker build and docker run | Build creates the image; run creates the container |
| Putting secrets into a Dockerfile | Use runtime configuration or a secrets mechanism |
Using latest everywhere | Use explicit tags when repeatability matters |
Forgetting the final . in docker build | The . is the build context |
TL;DR
Dockerfiledefines how to build the image.docker buildcreates an image from the Dockerfile and build context.- An image is a read-only package, not a running app.
docker runcreates 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.