DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Write a Docker File

Task Overview

As per recent requirements shared by the Nautilus application development team, they need custom images created for one of their projects. Several of the initial testing requirements are already been shared with DevOps team. Therefore, create a docker file /opt/docker/Dockerfile (please keep D capital of Dockerfile) on App server 1 in Stratos DC and configure to build an image with the following requirements:

a. Use ubuntu:24.04 as the base image.

b. Install apache2 and configure it to work on 8089 port. (do not update any other Apache configuration settings like document root etc).


Task Overview

The Nautilus application development team needs a custom Docker image created for one of their projects. We need to create a Dockerfile on App Server 1 with the following requirements:

  1. Use ubuntu:24.04 as the base image
  2. Install apache2 and configure it to work on port 8089
  3. Do not update any other Apache configuration settings

Connect to App Server 1

#Password for tony: Ir0nM@n
ssh tony@stapp01

# switch to super user
sudo -i
Enter fullscreen mode Exit fullscreen mode

Create the Directory and Dockerfile

mkdir -p /opt/docker
vi /opt/docker/Dockerfile
Enter fullscreen mode Exit fullscreen mode

Add the following content:

FROM ubuntu:24.04

RUN apt-get update && \
    apt-get install -y apache2 && \
    sed -i 's/Listen 80/Listen 8089/g' /etc/apache2/ports.conf && \
    sed -i 's/:80>/:8089>/g' /etc/apache2/sites-available/000-default.conf && \
    apt-get clean

EXPOSE 8089

CMD ["apache2ctl", "-D", "FOREGROUND"]
Enter fullscreen mode Exit fullscreen mode

Build the Docker Image (Optional)

To test the Dockerfile:

cd /opt/docker
docker build -t ubuntu-apache:custom .
Enter fullscreen mode Exit fullscreen mode

Test the Container (Optional)

docker run -d -p 8089:8089 --name apache-test ubuntu-apache:custom
curl -I http://localhost:8089/
docker stop apache-test
docker rm apache-test
Enter fullscreen mode Exit fullscreen mode

Dockerfile Explanation

Line Description
FROM ubuntu:24.04 Use Ubuntu 24.04 as base image
RUN apt-get update && ... Update package lists, install apache2, change port to 8089, clean cache
EXPOSE 8089 Inform Docker that container listens on port 8089
CMD ["apache2ctl", "-D", "FOREGROUND"] Start Apache in foreground mode

Complete Command Sequence

ssh tony@stapp01
sudo -i

mkdir -p /opt/docker

cat > /opt/docker/Dockerfile << 'EOF'
FROM ubuntu:24.04

RUN apt-get update && \
    apt-get install -y apache2 && \
    sed -i 's/Listen 80/Listen 8089/g' /etc/apache2/ports.conf && \
    sed -i 's/:80>/:8089>/g' /etc/apache2/sites-available/000-default.conf && \
    apt-get clean

EXPOSE 8089

CMD ["apache2ctl", "-D", "FOREGROUND"]
EOF

cat /opt/docker/Dockerfile
Enter fullscreen mode Exit fullscreen mode

Task Summary

Requirement Status
Dockerfile created at /opt/docker/Dockerfile Completed
Base image: ubuntu:24.04 Completed
Apache2 installed Completed
Apache configured on port 8089 Completed
Other Apache settings unchanged Completed

Key Commands Reference

Command Purpose
mkdir -p /opt/docker Create directory for Dockerfile
FROM ubuntu:24.04 Base image
RUN apt-get install -y apache2 Install Apache2
sed -i 's/Listen 80/Listen 8089/g' /etc/apache2/ports.conf Change HTTP port
EXPOSE 8089 Expose port 8089
CMD ["apache2ctl", "-D", "FOREGROUND"] Start Apache

The Dockerfile has been successfully created at /opt/docker/Dockerfile on App Server 1 and is ready for building the custom image.

Top comments (0)