UrmLmn
UrmLmn

Reputation: 1257

Docker container exiting immediately after running php code

I'm currently developing a RESTful API with the Slim framework and I'm trying to run it on a docker container. The problem, however, is that the script runs only once and than automatically exits from docker with the exit code 0, i.e. it catches the first http request for the index.php page, returns it and then it exits.

I'm sure, there must be a way to run apache on a container, so that every time I make a request for a specific URL(like localhost:8080/api/something) on my local browser, so that the apache server that is running in the container would handle those requests. However, since I didn't have any previous experience with docker, I'm currently not able to solve this.

Here's my Dockerfile:

FROM php:7.0-cli
COPY . /usr/src/my_app
WORKDIR /usr/src/my_app/public
CMD [ "php", "./index.php" ]

Here's my Docker-compose file:

version: '2'

services:
   app:
      build: ./app
      volumes:
         - ./app:/usr/src/app
      ports:
         - 5001:80

Can someone tell my how to change my Dockerfile or Docker-compose file, so that the apache server does not exits after the first requests, but it still runs and listens for other requests.

Thank you in advance!

Upvotes: 2

Views: 3541

Answers (1)

Jay Blanchard
Jay Blanchard

Reputation: 34426

Here is an example of an image with Apache configured to run PHP7:

FROM ubuntu:latest
MAINTAINER webmaster@localhost

RUN apt-get update
RUN apt-get install php php-mysql php-cgi libapache2-mod-php -y


COPY your/web/stuff/ var/www/html/

CMD /usr/sbin/apache2ctl -D FOREGROUND

In this case I am installing the PHP mods for Apache and running the webserver in the foreground. Here is an example of a build command for this Dockerfile:

docker build -t "myApache" .

Now that I have an image I can run the container with this run command:

docker run -p 8080:80 -p 4433:443 --name my_web_server -P -d myApache

With this running you should be able to access the web server on port 8080 (http://localhost:8080)

Upvotes: 5

Related Questions