PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 1, 2022

[FIXED] Dockerfile with LAMP running (Ubuntu)

 March 01, 2022     docker, lamp     No comments   

Issue

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



Answered By - kstromeiraos
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing