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.
-
Navigate to the correct directory:
cd /opt/docker -
Open the file for editing using
sudo vi:
sudo vi Dockerfile -
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.
-
From the
/opt/dockerdirectory, use thedocker buildcommand. The-tflag tags the image with a name, and the.specifies the current directory as the build context.
sudo docker build -t nautilus-apache . 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
FINISHEDstatus.
Step 3: Run the Docker Container
The final step is to run a container from the newly created image.
- Use the
docker runcommand with the following flags:-
-druns the container in detached mode, so it runs in the background. -
-p 5003:5003maps port 5003 on the host machine to port 5003 inside the container, making the Apache server accessible.
-
-
Execute the command:
sudo docker run -d -p 5003:5003 nautilus-apache 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 (1)
No need to run the container nor use expose keyword.
just making the Dockerfile was the task. And verifying with
docker buildhelps.I used "double quotes" inside the sed, RUN command uses /bin/sh with no escaping, resulting in this!:
RUN sed -i "s/Listen 80/Listen 3000/" /etc/apache2/ports.confbecame this
/bin/sh -c “sed -i "s/findstr/replacestr" /etc/apache2/ports.conf”