Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Docker Docker & Containers: From Zero to Production Docker Images Dockerfile Module 2 — Docker Images

Building Your First Image with Dockerfile

Reviewed & accurate
AI Summary

What You'll Learn

Beginner

  • How to write a Dockerfile
  • How to build an image from a Dockerfile
  • The core instructions: FROM, COPY, RUN, CMD
Dockerfile FROM ubuntu RUN apt-get... COPY . /app docker build Image myapp:v1 (read-only layers) docker run Container running instance (writable top layer) push Registry Docker Hub (shared store) You write this Build produces this Run produces this Share here
The Docker workflow: Dockerfile → Image → Container → Registry

The Dockerfile

A Dockerfile is a text file with instructions to build an image.

Dockerfiledockerfile
FROM node:18-alpine

WORKDIR /app

COPY package.json .
RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "server.js"]

What Each Instruction Does

InstructionWhat it does
FROMBase image to start from (required, must be first)
WORKDIRSets the working directory
COPYCopies files from host into the image
RUNRuns a command during build (creates a layer)
EXPOSEDocuments which port the container listens on
CMDDefault command when the container starts

Building the Image

Terminalbash
# Build an image tagged "myapp:1.0"
docker build -t myapp:1.0 .

# The "." is the build context

Running Your Image

Terminalbash
docker run -d -p 3000:3000 --name my-app myapp:1.0
curl http://localhost:3000
The dot matters
The . at the end of docker build is the build context — the directory Docker sends to the daemon. COPY . . copies from this context.

.dockerignore

.dockerignoretext
node_modules
npm-debug.log
.git
.env
Dockerfile
docker-compose.yml
*.md

Practical Exercise

Create a directory: mkdir myapp && cd myapp
Create server.js with a simple HTTP server
Create package.json
Create the Dockerfile
Build: docker build -t myapp:1.0 .
Run: docker run -d -p 3000:3000 myapp:1.0

Key Takeaways

  • A Dockerfile is a text file with build instructions. Each line creates a layer.
  • Core instructions: FROM, COPY, RUN, CMD.
  • docker build -t name:tag . builds an image.
  • The . is the build context.
  • Always use .dockerignore to exclude unnecessary files.
Previously: Lesson 10 covered pulling images.
Today: You learned: Write a Dockerfile, build an image, run a container from it.
Next: Lesson 12 covers all Dockerfile instructions.
Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments