3

I am trying to run some basic html pages using httpd docker image.

Dockerfile

FROM httpd:alpine

COPY ./views /usr/local/apache2/htdocs/    # where my html pages stored

COPY httpd.conf /usr/local/apache2/conf/httpd.conf

RUN httpd -v

EXPOSE 7074

httpd.conf

ServerName localhost
Listen 7074
LoadModule mpm_event_module modules/mod_mpm_event.so

docker-compose

version: '3'

frontend_image:
    image: frontend_image
    build: 
      context: ./frontend_image
      dockerfile: Dockerfile
    network_mode: "host"
    env_file: "./api.env"
    depends_on: 
      - apigateway

Then : sudo docker-compose up --build

RUN httpd -v gives:

Server version: Apache/2.4.43 (Unix)
Server built:   Apr 24 2020 15:46:58

But

Project_frontend_image_1 exited with code 1

How can I add an Entry-point to httpd, as I do not have apachectl2 in /usr/sbin.

refered : Docker run Exited 1 httpd

Edit :

I have tried docker run -dit --name my-apache-app -p 7575:80 -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4 and it works.

Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94
  • 1
    Are there any further log messages? Why do you need it to run with `network_mode: host`? (This will prevent it from connecting to the `apigateway` container, for example, which may be related to your problem.) – David Maze May 17 '20 at 17:10
  • @DavidMaze No logs, I have tried it. I have removed `network_mode: host`, yet problem there. I used this as its easy to connect all services from localhost with ports. – Sachith Muhandiram May 17 '20 at 17:15
  • 1
    Try to run the docker image in interactive mode (-it) which will provide any errors if there are – Janitha Tennakoon May 21 '20 at 10:28
  • @janitha000 I have added `stdin_open: true` and `tty: true` to my docker-compose file, yet same result. https://stackoverflow.com/questions/36249744/interactive-shell-using-docker-compose – Sachith Muhandiram May 21 '20 at 10:34

2 Answers2

3

It seems this is a problem with httpd.conf file rather than the Docker image.

As you can run docker run -dit --name my-apache-app -p 7575:80 -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4 and access to apache service.

Run above command and login to running container : sudo docker exec -it container_id /bin/sh

cat /usr/local/apache2/conf/httpd.conf

Copy the content and past in your httpd.conf, change port.

No need to add CMD[""] things as httpd:alpine base image does it.

Just COPY httpd.conf /usr/local/apache2/conf/httpd.conf in Dockerfile.

Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94
Pasan
  • 121
  • 2
2

This is the script executed by default by the httpd alpine image:

2.4/alpine/httpd-foreground

#!/bin/sh
set -e

# Apache gets grumpy about PID files pre-existing
rm -f /usr/local/apache2/logs/httpd.pid

exec httpd -DFOREGROUND "$@"

Try and add an echo "test" before and after the rm command to check that:

  • the CMD script is actually called
  • the rm command is not the one causing an error (and an exit of the script with status 1)

docker-library/httpd issue 127 mentions a similar issue, solved with the Apache ErrorLog directive (presumably similar to one used in issue 133, with a mounted httpd-vhosts.conf.


I have tried docker run -dit --name my-apache-app -p 7575:80 -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4 and it works.

Then modify your docker-compose.yml to include the same mount (see documentation):

frontend_image:
    image: frontend_image
    volumes:
      - ./:/usr/local/apache2/htdocs/
    build: 
      context: ./frontend_image
      dockerfile: Dockerfile
    network_mode: "host"
    env_file: "./api.env"
    depends_on: 
      - apigateway
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I have added `ErrorLog`, but still same. Where should I place this bash script? – Sachith Muhandiram May 20 '20 at 02:27
  • 1
    @SachithMuhandiram As seen in https://github.com/docker-library/httpd/blob/1f4f7973d49205836c6f71d397382159291aefb4/2.4/alpine/Dockerfile#L156, your own Dockerfile should include `COPY httpd-foreground /usr/local/bin/`, with `httpd-foreground` modified as indicated in the answer, for testing. – VonC May 20 '20 at 10:26
  • 1
    @SachithMuhandiram Also, see me edit to include the missing volume in your `docker-compose.yml` – VonC May 20 '20 at 10:30
  • What do you mean by missing volume? – Sachith Muhandiram May 20 '20 at 10:37
  • 1
    @SachithMuhandiram I refer to your own edit: https://stackoverflow.com/revisions/61853292/2. You added a volume: `-v "$PWD":/usr/local/apache2/htdocs/`. I propose to add the same volume in your `docker-compose.yml` to see if a `docker-compose up` will work (like your `docker run` command was working) – VonC May 20 '20 at 10:39
  • Modified all the locations as suggested, yet same result. – Sachith Muhandiram May 20 '20 at 11:06
  • 1
    @SachithMuhandiram OK. Did you modify `httpd-foreground` and `COPY` it in your `Dockerfile`, in order to get some trace at runtime? – VonC May 20 '20 at 11:37
  • Yes, I have `httpd-foreground` in my working directory and `COPY httpd-foreground /usr/local/bin/` and `CMD ["/bin/sh","-c","httpd-foreground","-DFOREGROUND"]` – Sachith Muhandiram May 20 '20 at 12:30
  • `echo "test"` gives `frontend_image_1 | test` before and after `rm` – Sachith Muhandiram May 21 '20 at 03:24
  • 1
    @SachithMuhandiram Since the `docker run` works with `httpd:2.4` image, can you try the same `docker run`, but with the `frontend_image` image? – VonC May 21 '20 at 16:33