DEV Community

Wycliffe A. Onyango
Wycliffe A. Onyango

Posted on

100 Days of DevOps: Day 41

Creating and Running a Custom Apache Docker Container on App Server 3

Completing the exercise involves a sequence of steps: creating a Dockerfile, building a custom Docker image from it, and then running a container from that image. The provided log showcases the correct commands and their outputs for each stage.

Step 1: Create the Dockerfile

First, you need to create the Dockerfile with the specified requirements. The file should be named Dockerfile (with a capital D) and located at /opt/docker/Dockerfile.

  1. Navigate to the correct directory:

    cd /opt/docker
    
  2. Open the file for editing using sudo vi:

    sudo vi Dockerfile
    
  3. Add the following content to the file. This code uses ubuntu:24.04 as the base image, installs Apache2, changes the listening port to 5003, exposes the port, and starts the service.

    # Use ubuntu:24.04 as the base image
    FROM ubuntu:24.04
    
    # Install apache2
    RUN apt-get update && \
        apt-get install -y apache2
    
    # Configure Apache to listen on port 5003
    RUN sed -i 's/^Listen 80$/Listen 5003/' /etc/apache2/ports.conf
    
    # Expose port 5003
    EXPOSE 5003
    
    # Start Apache in the foreground
    CMD ["apache2ctl", "-D", "FOREGROUND"]
    

Step 2: Build the Docker Image

Once the Dockerfile is saved, you can build the image. This process reads the instructions from the Dockerfile and creates a new, reusable image.

  1. From the /opt/docker directory, use the docker build command. The -t flag tags the image with a name, and the . specifies the current directory as the build context.

    sudo docker build -t nautilus-apache .
    
  2. The output will show the build process, including downloading the base image, running each command in the Dockerfile, and finally, tagging and exporting the image. A successful build is indicated by the FINISHED status.


Step 3: Run the Docker Container

The final step is to run a container from the newly created image.

  1. Use the docker run command with the following flags:
    • -d runs the container in detached mode, so it runs in the background.
    • -p 5003:5003 maps port 5003 on the host machine to port 5003 inside the container, making the Apache server accessible.
  2. Execute the command:

    sudo docker run -d -p 5003:5003 nautilus-apache
    
  3. Upon success, the command will print a long container ID, confirming that the container is running in the background and is ready to serve requests on port 5003.

Top comments (0)