keepcalculator

06 Numbers and Powers

Run Apache in Docker with a Dockerfile

Card No. 06·70753 · Updated · Standard formula, shown below

This guide walks you through creating a Dockerfile that builds a custom Apache web server image, so you can serve your own static website or application in a consistent, portable container. You’ll start with a base image, copy your files in, expose the right port, and run the server—then test it in your browser. Before you begin, you need Docker installed and running on your machine (Docker Desktop on Windows/Mac, or the Docker engine on Linux), and a basic folder with the HTML files you want to serve.

What You’ll Need

  • Docker installed (check with docker --version).
  • A terminal or command prompt.
  • A project folder containing your website files (e.g., index.html).
  • Basic familiarity with the command line (cd, ls, etc.).

Step-by-Step

  1. Create a project directory. Open a terminal and run mkdir my-apache-site then cd my-apache-site. This will hold your Dockerfile and your website files.
  2. Add your website files. Place your HTML, CSS, JS, or other static files in this folder. For this guide, create a simple index.html with any content you like (e.g., <h1>Hello from Docker Apache</h1>).
  3. Create the Dockerfile. In the same folder, create a file named Dockerfile (no extension). Open it in a text editor.
  4. Write the base image line. Start with FROM httpd:2.4. This pulls the official Apache HTTP Server image, which already has Apache installed and configured to serve files from /usr/local/apache2/htdocs/.
  5. Copy your files into the image. Add COPY index.html /usr/local/apache2/htdocs/. If you have multiple files, you can copy the whole folder: COPY . /usr/local/apache2/htdocs/. Be careful not to copy the Dockerfile itself into the web root unless you want it public.
  6. Expose the port. Add EXPOSE 80. This tells Docker that the container will listen on port 80, but it doesn’t publish it to your host yet—that happens when you run the container.
  7. Set the default command (optional). The official httpd image already has a default command that starts Apache in the foreground, so you don’t need to add a CMD or ENTRYPOINT. If you want to be explicit, you can add CMD ["httpd-foreground"].
  8. Build the image. In the terminal, run docker build -t my-apache-site . (note the dot). This reads the Dockerfile and creates an image named my-apache-site.
  9. Run the container. Run docker run -d -p 8080:80 --name my-apache-container my-apache-site. The -d runs it detached, -p 8080:80 maps host port 8080 to container port 80, and --name gives it a friendly name.
  10. Test it. Open a browser and go to http://localhost:8080. You should see your index.html page. If not, check the troubleshooting section below.
  11. Stop and remove the container when done. Run docker stop my-apache-container and then docker rm my-apache-container. To remove the image as well, use docker rmi my-apache-site.

Common Mistakes

  • Forgetting the dot in docker build. The dot specifies the build context (the current folder). Without it, Docker doesn’t know what to send to the daemon.
  • Copying the Dockerfile into the web root. If you do COPY . /usr/local/apache2/htdocs/, your Dockerfile becomes publicly accessible. Use a .dockerignore file to exclude it, or copy only the needed subfolders.
  • Not exposing the port correctly. The EXPOSE instruction is documentation; you still need -p when running. Also, make sure the host port (left side) is not already in use.
  • Using a different base image without knowing its document root. The official httpd image uses /usr/local/apache2/htdocs/. If you use ubuntu and install Apache manually, the path is usually /var/www/html/—adjust accordingly.
  • Running the container in the foreground and losing your prompt. Without -d, the container runs in the foreground; you can press Ctrl+C to stop it, but you can’t use the same terminal for other commands.

Tips

  • Use a .dockerignore file. Create a file named .dockerignore in your project folder and add Dockerfile, .git, and any large or unnecessary files. This speeds up builds and keeps your image clean.
  • Tag your images meaningfully. Instead of my-apache-site, use something like my-apache-site:v1. This helps when you have multiple versions.
  • Mount a volume for development. Instead of rebuilding every time you change a file, run with -v $(pwd):/usr/local/apache2/htdocs/ (on Windows use %cd%). This mounts your local folder into the container, so changes appear instantly.
  • Check the logs. If something fails, run docker logs my-apache-container to see Apache’s error messages.
  • Keep the image small. Use the official httpd:alpine tag (e.g., FROM httpd:2.4-alpine) for a smaller footprint, but be aware that some modules may not be included.

Troubleshooting

  • Page doesn’t load at localhost:8080. First, check if the container is running with docker ps. If it’s not, run docker ps -a to see if it exited. Then check logs with docker logs my-apache-container—common errors include missing files or permission issues.
  • Port already in use. If you get an error like Bind for 0.0.0.0:8080 failed: port is already allocated, change the host port, e.g., -p 8081:80, and try again.
  • Build fails with “COPY failed: stat … no such file or directory”. Make sure the file or folder you’re copying exists in the same directory as the Dockerfile. Also check the path inside the container—it must be absolute.
  • Permission denied when copying. On some systems, the Docker daemon runs as root, but your files may have restrictive permissions. You can run chmod -R a+r on your project folder, but be careful with sensitive files.
  • Apache starts but shows the default page. This usually means your files weren’t copied to the right location. Double-check the COPY path and rebuild. Also ensure your index.html is in the root of the htdocs folder, not in a subfolder.
  • Container exits immediately. Run docker logs to see the error. A common cause is a missing httpd-foreground command if you overrode the default CMD. If you added a custom CMD, make sure it starts Apache in the foreground, e.g., CMD ["httpd-foreground"].

Frequently Asked Questions

Can I use a different port than 80?

Yes. The container internally listens on port 80, but you can map any host port to it, such as -p 3000:80. Then access http://localhost:3000.

How do I add custom Apache configuration?

You can copy a custom httpd.conf file into the image: COPY my-httpd.conf /usr/local/apache2/conf/httpd.conf. Alternatively, mount it as a volume when running: -v $(pwd)/my-httpd.conf:/usr/local/apache2/conf/httpd.conf.

What if I need PHP or MySQL?

For PHP, use a different base image like php:7.4-apache or php:8.2-apache. For MySQL, run a separate container and link them via a Docker network. This keeps concerns separated.

How do I update my website after building the image?

If you changed files, you need to rebuild the image: docker build -t my-apache-site . then stop and remove the old container and run a new one. Alternatively, use a volume mount during development to avoid rebuilding.

Is it safe to expose the container to the internet?

Exposing port 80 to the internet means anyone can access your site. Ensure your site has no sensitive files, and consider using a reverse proxy like Nginx or Traefik for better security and HTTPS termination. Docker itself does not provide a firewall.

Can I run multiple Apache containers at once?

Yes, as long as you map different host ports, e.g., -p 8080:80 and -p 8081:80. Each container is isolated.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →