R
Railway•13mo ago
joe

Dockerfile build doesn't get PORT arg (railway CLI)

My Dockerfile build doesn't get the alleged PORT arg with railway's randomly generated port (i.e. --build-arg="PORT=7545"). I'm deploying with the railway CLI. Project ID: 988c3d59-aff4-487b-8d86-5541ba84fcad Service ID: b53149d1-7adc-4868-862b-46cedbed17bf Dockerfile:
FROM php:8.2-apache

ARG PORT

RUN echo PORT value: $PORT
CMD echo PORT value: $PORT
FROM php:8.2-apache

ARG PORT

RUN echo PORT value: $PORT
CMD echo PORT value: $PORT
Deploy command:
wsl -u root railway up
wsl -u root railway up
No description
No description
7 Replies
Percy
Percy•13mo ago
Project ID: 988c3d59-aff4-487b-8d86-5541ba84fcad,b53149d1-7adc-4868-862b-46cedbed17bf
Brody
Brody•13mo ago
the generated port is only available at run time, there should be no need to use it during build if your app cant listen on the PORT environment variable during runtime, then just set a PORT service variable to the same port that your app does listen on
joe
joe•13mo ago
ok, in a different service (ID: 108e166f-6409-417f-ab3a-fbf9884561f0), I tried setting PORT=8080 in my service variables and the port was accessible in both build and deployment, so my app should be listening on 8080, but I'm getting 503, "Application failed to respond" when I visit the public URL (https://grandiose-friction-production.up.railway.app/) Here's the Dockerfile. It works perfectly on my local machine:
FROM php:8.2-apache

ARG PORT
ENV PORT=$PORT

COPY ./src/ /var/www/html/

# Change the port numbers in apache's config files...

# Comment out all ports that are not in an IfModule block
RUN sed -i '/<IfModule/,/<\/IfModule>/! s/^\(Listen [0-9]*\)/# Disabled in image build step:\n# \1/' /etc/apache2/ports.conf
# Add correct port to end of file
RUN echo "Listen ${PORT}" >> /etc/apache2/ports.conf

# Output file contents for debugging
RUN cat /etc/apache2/ports.conf

# Duplicate the first occurrence of a VirtualHost opening tag and comment out the duplicate
RUN sed -i '/<VirtualHost/{p;h;s/^/# Disabled in image build step:\n# /g}' /etc/apache2/sites-available/000-default.conf
# Change the original with the correct port
RUN sed -E -i "0,/VirtualHost \*:[0-9]+/s//VirtualHost *:${PORT}/" /etc/apache2/sites-available/000-default.conf

# Output file contents for debugging
RUN cat /etc/apache2/sites-available/000-default.conf

RUN echo PORT value: $PORT
CMD echo PORT value: $PORT

EXPOSE $PORT
FROM php:8.2-apache

ARG PORT
ENV PORT=$PORT

COPY ./src/ /var/www/html/

# Change the port numbers in apache's config files...

# Comment out all ports that are not in an IfModule block
RUN sed -i '/<IfModule/,/<\/IfModule>/! s/^\(Listen [0-9]*\)/# Disabled in image build step:\n# \1/' /etc/apache2/ports.conf
# Add correct port to end of file
RUN echo "Listen ${PORT}" >> /etc/apache2/ports.conf

# Output file contents for debugging
RUN cat /etc/apache2/ports.conf

# Duplicate the first occurrence of a VirtualHost opening tag and comment out the duplicate
RUN sed -i '/<VirtualHost/{p;h;s/^/# Disabled in image build step:\n# /g}' /etc/apache2/sites-available/000-default.conf
# Change the original with the correct port
RUN sed -E -i "0,/VirtualHost \*:[0-9]+/s//VirtualHost *:${PORT}/" /etc/apache2/sites-available/000-default.conf

# Output file contents for debugging
RUN cat /etc/apache2/sites-available/000-default.conf

RUN echo PORT value: $PORT
CMD echo PORT value: $PORT

EXPOSE $PORT
I checked both apache config files locally and on railway. They have the correct contents
Brody
Brody•13mo ago
right, as I said the auto generated PORT isn't available during build, but a manually set one would be make sure you are also listening on host 0.0.0.0 as well
joe
joe•13mo ago
For future readers, I had to specify 0.0.0.0 in the Listen and VirtualHost directives, like this... Listen 0.0.0.0${PORT} <VirtualHost 0.0.0.0${PORT}> @Brody did you mark this as solved yourself? It's only been one day I was going to mark my message as the solution for future reference
Brody
Brody•13mo ago
#🛂|readme #5 i was confident in what i said being the general solution, so yes i did give this thread a solved tag
joe
joe•13mo ago
huh, ok
Want results from more Discord servers?
Add your server