Search code examples
dockerlamp

Dockerfile with LAMP running (Ubuntu)


I'm trying to create a Docker (LAMP) image with the following

Dockerfile:

FROM ubuntu:latest
RUN  apt-get update \
  && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    apache2 \
    mysql-server \
    php7.0 \
    php7.0-bcmath \
    php7.0-mcrypt
COPY start-script.sh /root/
RUN chmod +x /root/start-script.sh && /root/start-script.sh

start-script.sh:

#!/bin/bash
service mysql start
a2enmod rewrite
service apache2 start

I build it with:

docker build -t resting/ubuntu .

Then run it with:

docker run -it -p 8000:80 -p 5000:3306 -v $(pwd)/html:/var/www/html resting/ubuntu bash

The problem is, the MYSQL and Apache2 service are not started.
If I run /root/start-script.sh manually in the container, port 80 maps fine to port 8000, but I couldn't connect to MYSQL with 127.0.0.1:5000.

How can I ensure that the services are running when I spin up a container with the image, and map MYSQL out to my host machine?


Solution

  • You need to change the execution of the script to a CMD instruction.

    FROM ubuntu:latest
    RUN  apt-get update \
      && DEBIAN_FRONTEND=noninteractive apt-get install -y \
        apache2 \
        mysql-server \
        php7.0 \
        php7.0-bcmath \
        php7.0-mcrypt
    COPY start-script.sh /root/
    RUN chmod +x /root/start-script.sh 
    CMD /root/start-script.sh
    

    Althought this works, this is not the right way to manage containers. You should have one container for your Apache2 and another one for MySQL.

    Take a look to this article that build a LAMP stack using Docker-Compose: https://www.kinamo.be/en/support/faq/setting-up-a-development-environment-with-docker-compose