Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Docker image for PHP and Node?

I am trying to create a Docker container for my Angular app that has a PHP file in it. Angular requires npm so I need to have Node.js installed. I don't need Apache for my project, just pure php should work fine.

My understanding is I should have a docker-compose like this:

FROM node:latest

..install php here

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app
RUN npm install

COPY . /usr/src/app

I am not sure how to install PHP in my case, Can anyone point me to the right direction? Thanks a lot!

like image 269
Jwqq Avatar asked Jun 09 '17 01:06

Jwqq


2 Answers

I think in this case the better way is using node docker image and PHP docker image together like bellow and not installing one of them by using apt-get install

FROM node:latest AS node
FROM php:7.4-fpm

COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules
COPY --from=node /usr/local/bin/node /usr/local/bin/node
RUN ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app
RUN npm install

COPY . /usr/src/app

in this way you don't need to install either node or PHP package every time that you change your code and rebuild from scratch is required in your Dockerfile

like image 149
adnan ahmady Avatar answered Sep 19 '22 14:09

adnan ahmady


I suggest you do it differently. Since php is longer than install, use the php image and install node.

FROM php:5.6-apache

RUN apt-get update && apt-get install -y nodejs npm
#WORKDIR is /var/www/html
COPY . /var/www/html/
RUN npm install

And then you have apache2 provides .php files.

Update for 2021
It is recommended to use php:7.4-apache or newer.

like image 23
German Avatar answered Sep 19 '22 14:09

German