R
Railway•2y ago
Medim

My API can't read env vars

hey, im trying to deploy a php-fpm api and i got a local .env file and i got the same .env at the railway project vars, but my api doesn't seem to be able to access it My custom dockerfile:
FROM composer:2.4.4 AS composer
FROM php:7.4-fpm as base
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
RUN apt-get update
RUN apt-get install -y \
nginx \
supervisor \
zlib1g-dev \
libzip-dev \
libjpeg-dev \
libxml2-dev \
libonig-dev \
libicu-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev
RUN docker-php-ext-install gd soap zip intl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg

FROM base as config
COPY default.conf /etc/nginx/conf.d/default.conf
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

FROM config as app
ARG AWS_S3_ID AWS_S3_SECRET AWS_S3_REGION AWS_S3_MAIN_BUCKET AWS_S3_ENV
ENV AWS_S3_ID=$AWS_S3_ID AWS_S3_SECRET=$AWS_S3_SECRET AWS_S3_REGION=$AWS_S3_REGION AWS_S3_MAIN_BUCKET=$AWS_S3_MAIN_BUCKET AWS_S3_ENV=$AWS_S3_ENV
WORKDIR /var/www/html

COPY . .

RUN composer install --ignore-platform-req=ext-bcmath --no-dev

EXPOSE 8080
CMD ["/usr/bin/supervisord"]
FROM composer:2.4.4 AS composer
FROM php:7.4-fpm as base
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
RUN apt-get update
RUN apt-get install -y \
nginx \
supervisor \
zlib1g-dev \
libzip-dev \
libjpeg-dev \
libxml2-dev \
libonig-dev \
libicu-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev
RUN docker-php-ext-install gd soap zip intl
RUN docker-php-ext-configure gd --with-freetype --with-jpeg

FROM base as config
COPY default.conf /etc/nginx/conf.d/default.conf
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

FROM config as app
ARG AWS_S3_ID AWS_S3_SECRET AWS_S3_REGION AWS_S3_MAIN_BUCKET AWS_S3_ENV
ENV AWS_S3_ID=$AWS_S3_ID AWS_S3_SECRET=$AWS_S3_SECRET AWS_S3_REGION=$AWS_S3_REGION AWS_S3_MAIN_BUCKET=$AWS_S3_MAIN_BUCKET AWS_S3_ENV=$AWS_S3_ENV
WORKDIR /var/www/html

COPY . .

RUN composer install --ignore-platform-req=ext-bcmath --no-dev

EXPOSE 8080
CMD ["/usr/bin/supervisord"]
Im reading the vars in code using
$_ENV['AWS_S3_ENV']
$_ENV['AWS_S3_ENV']
What am i doing wrong?
Solution:
nvm. using $_SERVER worked
Jump to solution
45 Replies
Percy
Percy•2y ago
Project ID: de7aa210-2a7c-457e-9476-6c30744f4f10
Medim
MedimOP•2y ago
de7aa210-2a7c-457e-9476-6c30744f4f10
Brody
Brody•2y ago
oh so you arent actually using these variables during build, only after the app has started
Medim
MedimOP•2y ago
yeye
Brody
Brody•2y ago
then you dont need them in the dockerfile at all
Medim
MedimOP•2y ago
wat
Brody
Brody•2y ago
show me your service variables
Medim
MedimOP•2y ago
Brody
Brody•2y ago
show me how your code is accessing the variables
Medim
MedimOP•2y ago
Brody
Brody•2y ago
whats this $_ENV thingy
Medim
MedimOP•2y ago
to access the env var
Brody
Brody•2y ago
doesnt php use getenv
Medim
MedimOP•2y ago
just if u wanna mutate it i guess
Medim
MedimOP•2y ago
Medim
MedimOP•2y ago
nvm
Brody
Brody•2y ago
try it?
Medim
MedimOP•2y ago
but theres also AUGH
Brody
Brody•2y ago
also try echoing all the environment vars you know, super basic debugging stuff 🙂
Medim
MedimOP•2y ago
...not possible lmfao
Brody
Brody•2y ago
it has to be
Medim
MedimOP•2y ago
yeah, blame slim-php/php-fpm/composer cant log shit there
Brody
Brody•2y ago
log it somewhere else
Medim
MedimOP•2y ago
trust me, i can't gotta be trial and error
Brody
Brody•2y ago
bruh slap a single index.php file in an /app folder and print the environment variables
Medim
MedimOP•2y ago
yeah, thats a good option
Brody
Brody•2y ago
not possible my ass
Medim
MedimOP•2y ago
not to console atleast
Brody
Brody•2y ago
prints to browser window, it dont matter obviously dont use the real credentials for this type of testing
Brody
Brody•2y ago
^
Medim
MedimOP•2y ago
.. it crashes on the first one but i tried $_ENV gonna try $_SERVER and getenv also .. nothing works FeelsCry
Solution
Medim
Medim•2y ago
nvm. using $_SERVER worked
Medim
MedimOP•2y ago
now u ask me why? idk
Brody
Brody•2y ago
<?php print_r($_ENV); ?>
<?php print_r($_ENV); ?>
Medim
MedimOP•2y ago
hmm I'm wondering why $_SERVER instead of $_ENV
Brody
Brody•2y ago
youve mucked something up somewhere
Medim
MedimOP•2y ago
yeah I needed a custom package to read env vars cause php-fpm can't do it by itself it seems https://github.com/vlucas/phpdotenv
Brody
Brody•2y ago
kinda cringe php
Medim
MedimOP•2y ago
it worked flawlessly thanks @Brody
Brody
Brody•2y ago
no problem, though i dont know if i helped
Medim
MedimOP•2y ago
since i can't pay you, here is a 🌟 for you
Brody
Brody•2y ago
awh thanks
Medim
MedimOP•2y ago
gonna close this thread
Want results from more Discord servers?
Add your server