DEV Community

janak0ff
janak0ff

Posted on

Day 45: Fixing a Broken Docker Build – KodeKloud Engineer Challenge

The Nautilus DevOps team is working to create new images per requirements shared by the development team. One of the team members is working to create a Dockerfile on App Server 1 in Stratos DC. While working on it she ran into issues in which the docker build is failing and displaying errors. Look into the issue and fix it to build an image as per details mentioned below:
a. The Dockerfile is placed on App Server 1 under /opt/docker directory.

b. Fix the issues with this file and make sure it is able to build the image.

c. Do not change base image, any other valid configuration within Dockerfile, or any of the data been used — for example, index.html.

Note: Please note that once you click on FINISH button all the existing containers will be destroyed and new image will be built from your Dockerfile.


Introduction

Welcome back to my 100 Days of DevOps journey! Today marks Day 45, and we're diving into Dockerfile troubleshooting – a critical skill for any DevOps engineer working with containerized applications.


📋 Challenge Overview

The Nautilus DevOps team is working to create new images per requirements shared by the development team. One of the team members ran into issues where the docker build was failing and displaying errors. Our task was to fix the Dockerfile on App Server 1 in Stratos DC and ensure the image builds successfully.

Task Requirements:

  1. Dockerfile is placed on App Server 1 under /opt/docker directory
  2. Fix the issues with this file and make sure it is able to build the image
  3. Do not change the base image, any other valid configuration, or any data being used (e.g., index.html)

🔍 The Problem

The original Dockerfile had the following issues:

FROM httpd:2.4.43

RUN sed -i "s/Listen 80/Listen 8080/g" /usr/local/apache2/conf/httpd.conf

RUN sed -i '/LoadModule\ ssl_module modules\/mod_ssl.so/s/^#//g' conf/httpd.conf

RUN sed -i '/LoadModule\ socache_shmcb_module modules\/mod_socache_shmcb.so/s/^#//g' conf/httpd.conf

RUN sed -i '/Include\ conf\/extra\/httpd-ssl.conf/s/^#//g' conf/httpd.conf

RUN cp certs/server.crt /usr/local/apache2/conf/server.crt

RUN cp certs/server.key /usr/local/apache2/conf/server.key

RUN cp html/index.html /usr/local/apache2/htdocs/
Enter fullscreen mode Exit fullscreen mode

❌ Issue 1: Incorrect Paths in sed Commands

The sed commands were using conf/httpd.conf instead of the full path /usr/local/apache2/conf/httpd.conf. Since the working directory inside the container is not guaranteed to be /usr/local/apache2, the commands would fail.

Fix: Use full paths in all sed commands:

RUN sed -i '/LoadModule\ ssl_module modules\/mod_ssl.so/s/^#//g' /usr/local/apache2/conf/httpd.conf
Enter fullscreen mode Exit fullscreen mode

❌ Issue 2: Using RUN cp Instead of COPY

This was the main issue:

RUN cp certs/server.crt /usr/local/apache2/conf/server.crt
Enter fullscreen mode Exit fullscreen mode

Why this fails:

  • RUN executes commands inside the container during build time
  • At that moment, the container doesn't know about local files like certs/ or html/
  • RUN cp fails because the files aren't inside the container yet

Fix: Use COPY to transfer files from the local build context:

COPY certs/server.crt /usr/local/apache2/conf/server.crt
Enter fullscreen mode Exit fullscreen mode

❌ Issue 3: Missing Directories and Files

The certs and html directories didn't exist, and SSL certificates and index.html were not present.

Fix: Create directories and generate required files:

mkdir -p certs html
echo "<h1>Welcome to Nautilus</h1>" > html/index.html
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
    -keyout certs/server.key \
    -out certs/server.crt \
    -subj "/C=US/ST=State/L=City/O=Organization/CN=localhost"
Enter fullscreen mode Exit fullscreen mode

🛠️ The Solution

Step 1: SSH to App Server 1

ssh tony@stapp01
# Password: Ir0nM@n
Enter fullscreen mode Exit fullscreen mode

Step 2: Navigate to Docker Directory

cd /opt/docker
Enter fullscreen mode Exit fullscreen mode

Step 3: Create Required Directories and Files

# Create directories
mkdir -p certs html

# Create index.html
echo "<h1>Welcome to Nautilus</h1>" > html/index.html

# Generate SSL certificates
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
    -keyout certs/server.key \
    -out certs/server.crt \
    -subj "/C=US/ST=State/L=City/O=Organization/CN=localhost"
Enter fullscreen mode Exit fullscreen mode

Step 4: Fix the Dockerfile

FROM httpd:2.4.43

RUN sed -i "s/Listen 80/Listen 8080/g" /usr/local/apache2/conf/httpd.conf

RUN sed -i '/LoadModule\ ssl_module modules\/mod_ssl.so/s/^#//g' /usr/local/apache2/conf/httpd.conf

RUN sed -i '/LoadModule\ socache_shmcb_module modules\/mod_socache_shmcb.so/s/^#//g' /usr/local/apache2/conf/httpd.conf

RUN sed -i '/Include\ conf\/extra\/httpd-ssl.conf/s/^#//g' /usr/local/apache2/conf/httpd.conf

COPY certs/server.crt /usr/local/apache2/conf/server.crt

COPY certs/server.key /usr/local/apache2/conf/server.key

COPY html/index.html /usr/local/apache2/htdocs/index.html
Enter fullscreen mode Exit fullscreen mode

Step 5: Build the Image

docker build -t httpd:custom /opt/docker/
Enter fullscreen mode Exit fullscreen mode

📊 Expected Output

[root@stapp01 docker]# docker build -t httpd:custom /opt/docker/
[+] Building 9.0s (13/13) FINISHED
 => [internal] load build definition from Dockerfile
 => [internal] load metadata for docker.io/library/httpd:2.4.43
 => [1/8] FROM docker.io/library/httpd:2.4.43
 => [2/8] RUN sed -i "s/Listen 80/Listen 8080/g" /usr/local/apache2/conf/httpd.conf
 => [3/8] RUN sed -i '/LoadModule\ ssl_module modules\/mod_ssl.so/s/^#//g' /usr/local/apache2/conf/httpd.conf
 => [4/8] RUN sed -i '/LoadModule\ socache_shmcb_module modules\/mod_socache_shmcb.so/s/^#//g' /usr/local/apache2/conf/httpd.conf
 => [5/8] RUN sed -i '/Include\ conf\/extra\/httpd-ssl.conf/s/^#//g' /usr/local/apache2/conf/httpd.conf
 => [6/8] COPY certs/server.crt /usr/local/apache2/conf/server.crt
 => [7/8] COPY certs/server.key /usr/local/apache2/conf/server.key
 => [8/8] COPY html/index.html /usr/local/apache2/htdocs/index.html
 => exporting to image
 => => writing image sha256:31aa770b164e
 => => naming to docker.io/library/httpd:custom
Enter fullscreen mode Exit fullscreen mode

🔑 Key Learnings

1. RUN vs COPY

  • RUN executes commands inside the container during build time
  • COPY transfers files from the local build context into the image
  • Use COPY for files that exist on your host machine

2. Always Use Full Paths

  • When modifying files inside a container, always use absolute paths
  • This ensures the command works regardless of the current working directory

3. Image Tagging

  • Always use -t to give your image a meaningful name
  • This makes it easier to reference and run containers later

4. Build Context

  • All files used in COPY commands must exist in the build context
  • The build context is the directory specified in the docker build command

📋 Final Dockerfile

FROM httpd:2.4.43

RUN sed -i "s/Listen 80/Listen 8080/g" /usr/local/apache2/conf/httpd.conf

RUN sed -i '/LoadModule\ ssl_module modules\/mod_ssl.so/s/^#//g' /usr/local/apache2/conf/httpd.conf

RUN sed -i '/LoadModule\ socache_shmcb_module modules\/mod_socache_shmcb.so/s/^#//g' /usr/local/apache2/conf/httpd.conf

RUN sed -i '/Include\ conf\/extra\/httpd-ssl.conf/s/^#//g' /usr/local/apache2/conf/httpd.conf

COPY certs/server.crt /usr/local/apache2/conf/server.crt

COPY certs/server.key /usr/local/apache2/conf/server.key

COPY html/index.html /usr/local/apache2/htdocs/index.html
Enter fullscreen mode Exit fullscreen mode

✅ Task Summary

Requirement Status
Dockerfile in /opt/docker
Fixed sed paths with full paths
Created certs directory
Generated SSL certificates
Created html directory
Created index.html
Changed RUN cp to COPY
Docker image builds successfully

Top comments (0)