Jwqq
Jwqq

Reputation: 1087

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!

Upvotes: 22

Views: 38840

Answers (3)

Melroy van den Berg
Melroy van den Berg

Reputation: 3166

I would use the node docker image (eg. slim) which is build on top of Debian. And use apt to just install PHP cli.

FROM node:lts-slim
ENV NODE_ENV production
ARG DEBIAN_FRONTEND=noninteractive

WORKDIR /app

# Install PHP (used for post-deployment commands)
RUN apt update && apt install -y php-cli php-intl php-mbstring php-xml php-zip

# Continue with your NodeJS package....

COPY package.json package.json
COPY package-lock.json package-lock.json

.....

Upvotes: 0

adnan ahmady
adnan ahmady

Reputation: 970

I think in this case the better way is to use node docker image and PHP docker image together like below and not install 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 rebuilding from scratch is required in your Dockerfile

Update: I have noticed recently that the node is relocated into the include path, at least it looks like it on the Alpine, so that you can replace /usr/local/lib/node with /usr/local/include/node for the alpine images.

for node:21-alpine the configuration is like this.

FROM node:21-alpine AS node
FROM php:8.3-fpm-alpine

RUN apk add --no-cache libstdc++ libgcc

COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules
COPY --from=node /usr/local/include/node /usr/local/include/node
COPY --from=node /usr/local/share/man/man1/node.1 /usr/local/share/man/man1/node.1
COPY --from=node /usr/local/share/doc/node /usr/local/share/doc/node
COPY --from=node /usr/local/bin/node /usr/local/bin/node
COPY --from=node /opt/ /opt/
RUN ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm
RUN ln -s /usr/local/lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx
RUN ln -s /opt/yarn-$(ls /opt/ | grep yarn | sed 's/yarn-//')/bin/yarn /usr/local/bin/yarn
RUN ln -s /opt/yarn-$(ls /opt/ | grep yarn | sed 's/yarn-//')/bin/yarnpkg /usr/local/bin/yarnpkg

Caution: You must install libstdc++ and libgcc packages on the Alpine so the node works properly.

the $(ls /opt/ | grep yarn | sed 's/yarn-//') part is for finding the image yarn version under the /opt/ path.

Upvotes: 50

German
German

Reputation: 1695

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.

Upvotes: 20

Related Questions