|
| 1 | +# Day 31 – Dockerfile: Build Your Own Images |
| 2 | + |
| 3 | +## Task |
| 4 | +Today's goal is to **write Dockerfiles and build custom images**. |
| 5 | + |
| 6 | +This is the skill that separates someone who uses Docker from someone who actually ships with Docker. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Expected Output |
| 11 | +- A markdown file: `day-31-dockerfile.md` |
| 12 | +- All Dockerfiles you create |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## Challenge Tasks |
| 17 | + |
| 18 | +### Task 1: Your First Dockerfile |
| 19 | +1. Create a folder called `my-first-image` |
| 20 | +2. Inside it, create a `Dockerfile` that: |
| 21 | + - Uses `ubuntu` as the base image |
| 22 | + - Installs `curl` |
| 23 | + - Sets a default command to print `"Hello from my custom image!"` |
| 24 | +3. Build the image and tag it `my-ubuntu:v1` |
| 25 | +4. Run a container from your image |
| 26 | + |
| 27 | +**Verify:** The message prints on `docker run` |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +### Task 2: Dockerfile Instructions |
| 32 | +Create a new Dockerfile that uses **all** of these instructions: |
| 33 | +- `FROM` — base image |
| 34 | +- `RUN` — execute commands during build |
| 35 | +- `COPY` — copy files from host to image |
| 36 | +- `WORKDIR` — set working directory |
| 37 | +- `EXPOSE` — document the port |
| 38 | +- `CMD` — default command |
| 39 | + |
| 40 | +Build and run it. Understand what each line does. |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +### Task 3: CMD vs ENTRYPOINT |
| 45 | +1. Create an image with `CMD ["echo", "hello"]` — run it, then run it with a custom command. What happens? |
| 46 | +2. Create an image with `ENTRYPOINT ["echo"]` — run it, then run it with additional arguments. What happens? |
| 47 | +3. Write in your notes: When would you use CMD vs ENTRYPOINT? |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +### Task 4: Build a Simple Web App Image |
| 52 | +1. Create a small static HTML file (`index.html`) with any content |
| 53 | +2. Write a Dockerfile that: |
| 54 | + - Uses `nginx:alpine` as base |
| 55 | + - Copies your `index.html` to the Nginx web directory |
| 56 | +3. Build and tag it `my-website:v1` |
| 57 | +4. Run it with port mapping and access it in your browser |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +### Task 5: .dockerignore |
| 62 | +1. Create a `.dockerignore` file in one of your project folders |
| 63 | +2. Add entries for: `node_modules`, `.git`, `*.md`, `.env` |
| 64 | +3. Build the image — verify that ignored files are not included |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +### Task 6: Build Optimization |
| 69 | +1. Build an image, then change one line and rebuild — notice how Docker uses **cache** |
| 70 | +2. Reorder your Dockerfile so that frequently changing lines come **last** |
| 71 | +3. Write in your notes: Why does layer order matter for build speed? |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## Hints |
| 76 | +- Build: `docker build -t name:tag .` |
| 77 | +- The `.` at the end is the build context |
| 78 | +- `COPY . .` copies everything from host to container |
| 79 | +- Nginx serves files from `/usr/share/nginx/html/` |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## Submission |
| 84 | +1. Add your Dockerfiles and `day-31-dockerfile.md` to `2026/day-31/` |
| 85 | +2. Commit and push to your fork |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## Learn in Public |
| 90 | +Share your custom Docker image or Nginx screenshot on LinkedIn. |
| 91 | + |
| 92 | +`#90DaysOfDevOps` `#DevOpsKaJosh` `#TrainWithShubham` |
| 93 | + |
| 94 | +Happy Learning! |
| 95 | +**TrainWithShubham** |
0 commit comments