-
-
Notifications
You must be signed in to change notification settings - Fork 836
Document how to add npm (or yarn) #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@maxhelias do you think we can merge this one? |
I unresolve some of the suggested changes because they have not been apply, unless that has changed and I apologize. |
Sorry, I forgot all about this PR. I've accepted the suggested changes! |
@dunglas Like that, it seems fair to me |
I tried the suggested configuration but HMR not working. |
If you are using symfony ux, you can encounter an issue with dependencies located in vendor not found.
I'm working on a fix, that copy vendor volume from php image to the temporary node and then copy the build result to caddy but any help would be welcome |
If you did not solve it yourself already, I solved it like this: ## pre stage "php_build" (notice this is not the final stage, it will be used as a base for the final stage later on)
FROM php:${PHP_VERSION}-fpm-alpine AS symfony_php_build
# ... original lines ...
## final stage "node"
FROM node:${NODE_VERSION}-alpine AS symfony_node
WORKDIR /srv/app
# this COPY makes sure we get all files including vendor from the `symfony_php_build` stage
COPY --from=symfony_php_build /srv/app .
RUN yarn install
## If you are building your code for production
# RUN npm ci --only=production
RUN yarn run build
## final stage "php"
FROM symfony_php_build AS symfony_php
COPY --from=symfony_node /srv/app/public/build public/build
## final stage "caddy"
# ... original lines ... |
Any plans to fix and merger this? |
e2c2e08
to
1d347dd
Compare
I updated the PR to move forward on this topic, however the remark about Symfony UX is still valid, I haven't thought about the problem yet |
In #271 I introduced a dev stage. Maybe could we install Node in this dev image? |
Create build-css-js.md Update build-css-js.md Update README.md Update .dockerignore Update docs/build-css-js.md Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> Update docs/build-css-js.md Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> Update docs/build-css-js.md Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> Update docs/build-css-js.md Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> Update docs/build-css-js.md Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> Update docs/build-css-js.md Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> Update build-css-js.md Update build-css-js.md Update build-css-js.md Update build-css-js.md Rework
Hi, Thank you for this PR. I followed the steps and it works fine. Regards. |
Also, another issue I have:
It started to appear when I added Symfony UX dependencies.
Any ideas on how to solve this? |
Hi @Spomky, Hum, It's been a while since I took up the subject and I remember that Symfony UX packages were a problem for the compilation. For SymfonyUX, I think we should rethink the approach with either making a new build stage or including nodejs in the dev stage |
Hello @maxhelias Hereafter a working configuration for NPM, heavily inspired by the work of @CedricHerzog in #365 (with some extra parameters). --- Dockerfile 2023-02-05 09:59:06.335530332 +0100
+++ Dockerfile 2023-02-05 10:01:48.285534169 +0100
@@ -4,6 +4,11 @@
# https://docs.docker.com/develop/develop-images/multistage-build/#stop-at-a-specific-build-stage
# https://docs.docker.com/compose/compose-file/#target
+ARG PHP_VERSION=8.2
+ARG CADDY_VERSION=2.6
+ARG NODE_VERSION=19
+
+
# Builder images
FROM composer/composer:2-bin AS composer
@@ -19,7 +24,7 @@
--with github.com/dunglas/vulcain/caddy
# Prod image
-FROM php:8.2-fpm-alpine AS app_php
+FROM php:${PHP_VERSION}-fpm-alpine AS app_composer
# Allow to use development versions of Symfony
ARG STABILITY="stable"
@@ -101,6 +106,22 @@
chmod +x bin/console; sync; \
fi
+# node "stage"
+FROM node:${NODE_VERSION}-alpine AS symfony_node
+
+COPY --link --from=app_composer /srv/app /app/
+
+WORKDIR /app
+
+RUN npm install --force
+RUN npm run build
+## If you are building your code for production
+# RUN npm ci --only=production
+
+FROM app_composer AS app_php
+COPY --from=symfony_node --link /app/public/build /srv/app/public/build/
+
+
# Dev image
FROM app_php AS app_php_dev
@@ -119,10 +140,11 @@
RUN rm -f .env.local.php
# Caddy image
-FROM caddy:2.6-alpine AS app_caddy
+FROM caddy:${CADDY_VERSION}-alpine AS app_caddy
WORKDIR /srv/app
COPY --from=app_caddy_builder --link /usr/bin/caddy /usr/bin/caddy
COPY --from=app_php --link /srv/app/public public/
COPY --link docker/caddy/Caddyfile /etc/caddy/Caddyfile
+COPY --from=symfony_node --link /app/public/build /srv/app/public/build/
|
For anyone trying to get this to work:
|
Managed to get this to work with HMR as well:
Haven't tested the config in production yet. |
Hi @packagespace, I confirm it works fine in a dev env. You rock! |
@Spomky Thanks for the feedback. I'll have a chance to try it in production soon but since the build seems to work fine in the node stage I don't think there will be any issues. I looked at the thread you linked and I'm curious about a few things now (bear in mind I'm not very experienced with Docker and node):
FROM node:${NODE_VERSION}-alpine AS symfony_node
COPY --link --from=app_composer /srv/app/package*.json /app/
COPY --link --from=app_composer /srv/app/vendor /app/vendor
WORKDIR /app
RUN npm install --force
COPY --link --from=app_composer /srv/app/assets /app/assets
COPY --link --from=app_composer /srv/app/webpack.config.js /app/
RUN npm run build
|
It does not work, Any update or solution? |
@lchhieu What's wrong? I tried it a while ago and everything worked fine. If you have any errors or concerns, don't hesitate to post them - it's hard to help you like this! |
I tried add this code before caddy image but public/build does not copied
|
I did it a few weeks, I followed this thread and found a way to make it work: in # Prod image
- FROM php:8.2-fpm-alpine AS app_php
+ FROM php:${PHP_VERSION}-fpm-alpine AS app_composer then under of all the # node "stage"
FROM node:${NODE_VERSION}-alpine AS symfony_node
COPY --link --from=app_composer /srv/app /app/
WORKDIR /app
RUN npm install --force
RUN npm run build
## If you are building your code for production
# RUN npm ci --only=production
FROM app_composer AS app_php
COPY --from=symfony_node --link /app/public/build /srv/app/public/build/ and add this line at the bottom of Dockerfile (dont know if its relevant because we already had this line before): COPY --from=symfony_node --link /app/public/build /srv/app/public/build/ And in node:
build:
context: .
target: symfony_node
volumes:
- ./:/app
ports:
- target: 8080
published: 8080
protocol: tcp
command: 'sh -c "npm install; npm run dev-server -- --server-type https --client-web-socket-url https://localhost:8080/ws --host 0.0.0.0 --public https://localhost:8080"' And you can had the versions envvar at the top of ARG PHP_VERSION=8.2
ARG CADDY_VERSION=2.6
ARG NODE_VERSION=19 In your case this line |
@Jioleoo Could you give me your full Dockerfile. Although I followed your idea but public/build still not exist
|
#syntax=docker/dockerfile:1.4
# The different stages of this Dockerfile are meant to be built into separate images
# https://docs.docker.com/develop/develop-images/multistage-build/#stop-at-a-specific-build-stage
# https://docs.docker.com/compose/compose-file/#target
ARG PHP_VERSION=8.2
ARG CADDY_VERSION=2.6
ARG NODE_VERSION=19
# Builder images
FROM composer/composer:2-bin AS composer
FROM mlocati/php-extension-installer:latest AS php_extension_installer
# Build Caddy with the Mercure and Vulcain modules
FROM caddy:${CADDY_VERSION}-builder-alpine AS app_caddy_builder
RUN xcaddy build \
--with github.com/dunglas/mercure \
--with github.com/dunglas/mercure/caddy \
--with github.com/dunglas/vulcain \
--with github.com/dunglas/vulcain/caddy
# Prod image
FROM php:${PHP_VERSION}-fpm-alpine AS app_composer
# Allow to use development versions of Symfony
ARG STABILITY="stable"
ENV STABILITY ${STABILITY}
# Allow to select Symfony version
ARG SYMFONY_VERSION=""
ENV SYMFONY_VERSION ${SYMFONY_VERSION}
ENV APP_ENV=prod
WORKDIR /srv/app
# php extensions installer: https://github.com/mlocati/docker-php-extension-installer
COPY --from=php_extension_installer --link /usr/bin/install-php-extensions /usr/local/bin/
# persistent / runtime deps
RUN apk add --no-cache \
acl \
fcgi \
file \
gettext \
git \
;
RUN set -eux; \
install-php-extensions \
apcu \
intl \
opcache \
zip \
;
###> recipes ###
###> doctrine/doctrine-bundle ###
RUN apk add --no-cache --virtual .pgsql-deps postgresql-dev; \
docker-php-ext-install -j"$(nproc)" pdo_pgsql; \
apk add --no-cache --virtual .pgsql-rundeps so:libpq.so.5; \
apk del .pgsql-deps
###< doctrine/doctrine-bundle ###
###< recipes ###
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
COPY --link docker/php/conf.d/app.ini $PHP_INI_DIR/conf.d/
COPY --link docker/php/conf.d/app.prod.ini $PHP_INI_DIR/conf.d/
COPY --link docker/php/php-fpm.d/zz-docker.conf /usr/local/etc/php-fpm.d/zz-docker.conf
RUN mkdir -p /var/run/php
COPY --link docker/php/docker-healthcheck.sh /usr/local/bin/docker-healthcheck
RUN chmod +x /usr/local/bin/docker-healthcheck
HEALTHCHECK --interval=10s --timeout=3s --retries=3 CMD ["docker-healthcheck"]
COPY --link docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint
ENTRYPOINT ["docker-entrypoint"]
CMD ["php-fpm"]
# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
ENV COMPOSER_ALLOW_SUPERUSER=1
ENV PATH="${PATH}:/root/.composer/vendor/bin"
COPY --from=composer --link /composer /usr/bin/composer
# prevent the reinstallation of vendors at every changes in the source code
COPY --link composer.* symfony.* ./
RUN set -eux; \
if [ -f composer.json ]; then \
composer install --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress; \
composer clear-cache; \
fi
# copy sources
COPY --link . ./
RUN rm -Rf docker/
RUN set -eux; \
mkdir -p var/cache var/log; \
if [ -f composer.json ]; then \
composer dump-autoload --classmap-authoritative --no-dev; \
composer dump-env prod; \
composer run-script --no-dev post-install-cmd; \
chmod +x bin/console; sync; \
fi
# node "stage"
FROM node:${NODE_VERSION}-alpine AS symfony_node
COPY --link --from=app_composer /srv/app /app/
WORKDIR /app
RUN npm install --force
RUN npm run build
## If you are building your code for production
# RUN npm ci --only=production
FROM app_composer AS app_php
COPY --from=symfony_node --link /app/public/build /srv/app/public/build/
# Dev image
FROM app_php AS app_php_dev
ENV APP_ENV=dev XDEBUG_MODE=off
VOLUME /srv/app/var/
RUN rm "$PHP_INI_DIR/conf.d/app.prod.ini"; \
mv "$PHP_INI_DIR/php.ini" "$PHP_INI_DIR/php.ini-production"; \
mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
COPY --link docker/php/conf.d/app.dev.ini $PHP_INI_DIR/conf.d/
RUN set -eux; \
install-php-extensions \
xdebug \
;
RUN rm -f .env.local.php
# Caddy image
FROM caddy:${CADDY_VERSION}-alpine AS app_caddy
WORKDIR /srv/app
COPY --from=app_caddy_builder --link /usr/bin/caddy /usr/bin/caddy
COPY --from=app_php --link /srv/app/public public/
COPY --link docker/caddy/Caddyfile /etc/caddy/Caddyfile
COPY --from=symfony_node --link /app/public/build /srv/app/public/build/ |
Why not add node directly on php container ? Symfony at this stage has requered NODE and yarn. I can't see reason to not use node in dev container. |
@rowanparker @dunglas @maxhelias Considering we have a working configuration for this as per @Spomky's and my comments (#186 (comment) #186 (comment) #186 (comment) #186 (comment)), should we see if this PR can be modified and merged in order to avoid confusion as seen above? If opening up a new PR would be easier to parse, I wouldn't mind taking care of it either. |
Hi @packagespace, I didn't really take the time for this topic but I reread the whole thread. I think opening a new PR with your work will be easier to analyze and compare. Thanks for your time on this |
Could you give me an example for branch master? I tried this: COPY --from=symfony_node --link /app/public/build /srv/app/public/build/ But after build the public directory contains index.php only |
Hi all, Have we got anything new on this please? Best, Laurent |
The reworking of the project for FrankenPHP calls this integration into question. What's more, with the new AssetMapper component, it might be more interesting to concentrate on it. Thank you for your contribution in spite of everything. And if anyone would like to try with AssetMapper, a PR is welcome. |
As per #129