How to use docker-compose with Apache httpd example

Docker-compose and Apache httpd

Let’s say you want to serve website files from your local system, using the open-source Apache HTTP server. It’s easy to start an Apache Docker image; just issue an imperative docker run command in PowerShell or a Linux terminal window.

However, the run command can become long and unwieldly with heavily parameterized references to:

  • mapped Docker volumes;
  • environment variables;
  • secrets and credentials;
  • kernel memory constraints;
  • CPU count limitations;
  • temporary file system mounts; and
  • logging options.

Furthermore, these commands often must run multiple times a day. It’s tedious and a waste of time to type everything out on the command line every time.

That’s where the docker-compose.yaml file comes in.

Rather than type out an incredibly long terminal command, you store all of the configuration information in the docker-compose.yaml file.

The docker-compose up command

Then to run the container, you simply issue the docker-compose up command. This command then uses the YAML file for container configuration.

For example, take the following, modestly long docker run command. This single command configures the docker container with a name, port allocations, volume mapping and an image assignment.

Apache and Docker tutorials

Master the fundamentals of Apache, Docker and Kubernetes technology.

There are also additional switches that cause the container to run as a daemon process, and to remove the container after it finished its run.

It’s a lot of text to type into a command prompt, and I assure you, this is just the tip of the iceberg when parameterizing an Apache httpd Docker image:

docker@httpd /c/example/rock-paper-docker 
$ docker run -d --name my-apache-app -p 8080:80 -v $(PWD)/website:/usr/local/apache2/htdocs/ httpd:latest

Example httpd docker-compose.yaml file

All of the port configuration, container naming and Docker volume mapping can be placed into a docker-compose.yaml file. Here’s an example:

version: '3.9'
services:
  apache:
    image: httpd:latest
    container_name: my-apache-app
    ports:
    - '8080:80'
    volumes:
    - ./website:/usr/local/apache2/htdocs

Apache docker-compose up command

With the file configured, simply issue a docker-compose up command in the same directory as the docker-compose.yaml file and all of the specified services will run as Docker containers.

docker@httpd /c/example/rock-paper-docker 
$ docker-compose up -d
Creating network "rock-paper-docker_default" with the default driver
Pulling apache (httpd:latest)...
latest: Pulling from library/httpdDigest: sha256:2d1f8839d6127e400ac5f65481d8a0f17ac46a3b91de40b01e649c9a0324dea0
Status: Downloaded newer image for httpd:latest
Creating my-apache-app ...
Creating my-apache-app ... done

When this docker-compose up command completes, the Apache httpd Docker image becomes available on port 8080, with the web server hosting files from the folder specified in the configuration file’s volumes section.

apache docker website hosting

Our Apache docker-compose file instructs the Docker httpd container to host web pages on port 8080.

Apache http docker-compose steps

The steps to follow to use docker-compose with the Apache Docker httpd image are:

  1. Create a file named docker-compose.yaml
  2. Configure Apache httpd Docker container settings in the YAML file
  3. Run the docker-compose up command in the same folder as the YAML file
  4. Access your application through the running Docker httpd container
Apache docker-compose.yaml file for httpd

This simple Apache docker-compose.yaml file example configures a Docker httpd container.

Benefits of docker-compose

When you use docker-compose, you don’t just relieve the need to remember long, drawn out and easily mistyped terminal commands. You also gain the ability to check your docker-compose.yaml file into a source code repository like GitHub or GitLab. From there, you can easily share the configuration file with other team members, maintain a change history and implement a versioning strategy.

This Apache docker-compose example provides a simply introduction to the concept of declarative Docker configuration with YAML files. The more complicated your configuration gets, the more extensive the YAML file will become. But no matter how complicated an Apache docker-compose.yaml file might be, it will always be easier to maintain than an imperative command that runs in a terminal window or command prompt.

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close