Apache container requires manual "a2ensite *" on every run

I’m sure what I’m doing has a simple solution, but I’m pulling my hair out trying to find it.

I’m running the wordpress (on apache) and mysql images in docker-compose. I’ve mounted the directories I believe are appropriate ( “/var/ww/html” and “etc/apache2/sites-available” ). so I may modify them outside the containers, and everything connects when I spin it up. The only issue is I have to manually enter the wordpress container and enable all my virtualhosts file via ‘a2ensite’. I’ve tried using bash scripts to run these commands via ‘docker exec’ and repeatedly get “site [SITENAME] does not exist” error. I added a bash script inside the container and tried running that the same way, with the same results. I figured I can mount the “/” directory of the container to my host machine but docker-compose complains when I try. Is there a better way of accomplishing this?

My docker-compose file:

version: '3.1'

services:
  wordpress_ajh:
    container_name: wp_ajh
    image: localhost:5000/wp_ajh:latest
    restart: always
    ports:
      - '80:80'
      - '443:443'
    volumes:
      - /ajh/wp/html/:/var/www/html/
      - /ajh/wp/sites-available/:/etc/apache2/sites-available/

  mysql:
    container_name: db_ajh
    image: localhost:5000/db_ajh:latest
    restart: always
    volumes:
      - /ajh/db/:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: [REDACTED]
      MYSQL_USER: root

docker --version
Docker version 18.06.0-ce, build 0ffa825

docker-compose --version
docker-compose version 1.22.0, build f46880fe

uname -a
Linux debian-vps 4.9.0-7-amd64 #1 SMP Debian 4.9.110-1 (2018-07-05) x86_64 GNU/Linux

Hi :slight_smile:

Why do you need to modify the virtualhost?
Normally you would add a virtualhost into the image, that listens on port 80 and points to the directory ( and/or 443 if you handle ssl in your image ) and thats it?

Hi terpz,

Thanks for your response. My thinking was that I’d like to be able to run my setup with a freshly pulled image if necessary. I wanted to have all my configuration saved in the volumes on my host machine. I did end up pushing the configuration to the image on my private registry, as you said, but don’t want to be so dependent on the custom image. Is this outside the philosophy of Docker?

If your goal is only to serve some web pages, then there should be no need to build a image your self, even php bundles with apache: https://hub.docker.com/r/library/php/tags/

but i dont know what your demands are for your container :slight_smile:

1 Like

Hi again,

I ended up just pushing the changes to private registry as a workaround. No biggie, I don’t think what I’m trying to do is currently supported. Thanks for your help though!

Creating your own image is the right thing to do, in this case. A docker image should represent a specific application. The official images are starting points - you should build your own images atop them, and use your images in your compose files or deployment stacks.

2 Likes

I don’t think using the word should is especially helpful since it suggests that if you don’t you are doing something wrong. If a community image meets your needs then I don’t see any compelling reason to rebuild it yourself unless you are prepared to assume the responsibility for its lifecycle. Sometimes that the right thing to do, other times it’s an unnecessary overhead…

Hi, try add in wordpress_ajh:

services:
  wordpress_ajh:
    build:
     context: .
     dockerfile: Dockerfile

in Dockerfile

RUN a2enmod ssl
COPY ./your-ssl/* /etc/ssl/your-ssl/
COPY ./sites-available/* /etc/apache2/sites-available/
RUN a2dissite 000-default.conf
RUN a2dissite default-ssl.conf
RUN a2ensite your-site.conf
RUN a2ensite yoursite-ssl.conf
CMD ["apachectl", "-D", "FOREGROUND"]