docker deployment
Hi, sorry, I have a question. I want to upload a project to Railway that I have in a repository. Inside the repository, I have a container where I have the database, and that generates another one where I have the project. I'm new to Docker, so I'm not sure how it works exactly, but I know that I run 'sudo docker compose up -d' and it generates 2 containers. Then, inside the container, I have to run some Laravel-specific commands. My question is how to do this with Railway
223 Replies
Project ID:
N/A
N/A
please share the compose file and all the commands you need to run and where
i have these files
when i clone the commands are
sudo docker compose up -d
that generates 2 dockers, pgscomputacion and scomputacion-1
sudo docker exec -it scomputacion-1 bash
and then
compose install
well first off, your dockerfile should be doing compose install
mmm how?
composer install*
I do the composer install inside the container, after docker exec -it bash
do it in your dockerfile
via RUN
okey ill see how
after the composer install i do this
you'd do all the artisan stuff in the dockerfile too, and you might not need chown at all
like this?
you should not be doing multiple updates and installs, condense all apt stuff into a single command
like this?
Oh, one more thing I also do BEFORE the php artisan commands is cp .env.example .env and in the .env file I set db_host=pgscomputacion, db_port=5432, db_database=taller, db_user=postgres, db_password=postgres
yeah that's looking much better, but since you need the database variables in the dockerfile you need to reference them in the dockerfile with ARG
you'd set those in your service variables
how?
I do not know if it is ok
you had it right in the first screenshot, you dont need the ENV stuff
ok, only that, and now?
does your app need postgres 14? would 16 work?
I really don't know... It should work, I just use Laravel's eloquent for relationships and create/delete records, nothing complicated
okay then cant hurt to try 16
do you already have a railway project?
No, I had created it and deleted it because I didn't know how to do it
well then lets walk through the steps
so I started from 0
create an empty project and name it
and please send screenshots at every step of the way
from a github repo right?
nope, we hardly ever want to do that
an empty projectplease make sure to carefully read my messages
sorry,
add a postgres database
create a new empty service
name it and deploy it
can you copy and paste the ARGs you have in your dockerfile for me
ARG DB_HOST
ARG DB_PORT
ARG DB_DATABASE
ARG DB_USERNAME
ARG DB_PASSWORD
open up your service's raw editor and paste this in -
and then deploy it
and just this once no screenshot, click their eye icons to make sure they all show properly
yes
are the same of postgres
did you have any other environment variables that you need to set for it?
my .env file has more things but idk if are necesary
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:NP+/oB4Sc1mx4ISoyM6LECEJoLn2/gmQD9oe1zF8hwc=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=pgsql
DB_HOST=pgscomputacion
DB_PORT=5432
DB_DATABASE=taller
DB_USERNAME=postgres
DB_PASSWORD=postgres
BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1
VITE_APP_NAME="${APP_NAME}"
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
i didnt touch anything of these, only the db things
well add everything that is necessary to your service variables
I add all that
on the service?
but the syntax changes from
${PUSHER_HOST}
to ${{PUSHER_HOST}}
yesVITE_APP_NAME="${{APP_NAME}}"
VITE_PUSHER_APP_KEY="${{PUSHER_APP_KEY}}"
VITE_PUSHER_HOST="${{PUSHER_HOST}}"
VITE_PUSHER_PORT="${{PUSHER_PORT}}"
VITE_PUSHER_SCHEME="${{PUSHER_SCHEME}}"
VITE_PUSHER_APP_CLUSTER="${{PUSHER_APP_CLUSTER}}"
like that
yep, no quotes though
but
${{APP_NAME}}
isnt going to do anything unless you have also set APP_NAME
as welland always make sure you check them with the eye icon
I don't know, I didn't touch any of that, just the db stuff and nothing else, I left everything else as is
its ok
awsome
now set
PORT=80
go into its settings and generate a domain
done
and i assume you have the dockerfile and your app in a repo right?
yes
okay then go ahead and connect your repo
herE?
yep
deploy
building
let me know how that goes
it keeps
we shall wait
build logs please - https://bookmarklets.up.railway.app/log-downloader/
use the bookmarklet please
im trying
i dont understand, where do I have to drag the download logs?
to the bookmarks bar
you will want to enable the bookmarks bar
your dockerfile needs to also install the composer cli before running the composer command
?
i dont know php and the php eco system, so you tell me lol, is that how you install composer?
I really don't know hahaha I don't know about this myself, they gave me a container made and I only did everything in Laravel hahaha
chat gpt will help me
it would definitly be beneficial to learn the php ecosystem
It seems that if it is done that way
yes, its good. i´ll push
sounds good
done
try to build again?
yeah push your changes to github
fail again
Sure. The composer.json file is inside the src folder, but I run composer install from inside the container, after having run sudo docker exec -it scomputacion-1 bash
please make sure you have the composer.json file in the correct location
GitHub
GitHub - valeno12/SComputacion
Contribute to valeno12/SComputacion development by creating an account on GitHub.
thats the repository, its in src
yep i saw, please make sure you have the composer.json file in the correct location
It's okay, it has to be there
I mean, whenever I raised the project it was there and I had no problem. In my local project it is there
then you are in the wrong working directory in your dockerfile
https://docs.docker.com/reference/dockerfile/#workdir
but there is one thing I don't understand, railway creates the containers? or how does that work?
railway builds and runs the container, but your dockerfile defines how the container is built
I am raising the container in my premises to see if it works for me
building the docker container is easier than letting railway do it if you have the means to do that
:/
same thing
maybe your dockerfile runs that command in the wrong directory
yes but i dont understand
read the docs section I linked
i read it but i dont understand.
Look, the old dockerfile works correctly, but the new doesnt
i mean, the old dockerfile doesnt have the run composer install comand, but the route is supposed to be the same
you can't assume it's going to work the same when you aren't using docker compose, and you don't have ssh into the container so you need to set everything up beforehand in your dockerfile
FROM php:8.2-apache
Define las variables de entorno para la base de datos usando ARG
ARG DB_HOST
ARG DB_PORT
ARG DB_DATABASE
ARG DB_USERNAME
ARG DB_PASSWORD
Instala las dependencias necesarias para Laravel
RUN apt-get update && apt-get install -y \
libzip-dev \
zip \
unzip \
git \
zlib1g-dev \
libpng-dev \
libpq-dev \
libicu-dev \
libxslt1-dev \
g++ \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
libxml2-dev \
librabbitmq-dev \
libssh-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
Instala Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Copia el código de tu aplicación Laravel al directorio /var/www/html
COPY src /var/www/scomputacion
Configura el directorio de trabajo
WORKDIR /var/www/scomputacion
Copia el archivo composer.json
COPY src/composer.json /var/www/scomputacion/composer.json
Instala extensiones de PHP
RUN docker-php-ext-install zip pdo_mysql gd pdo_pgsql intl xsl opcache mysqli pdo pdo_mysql soap
Instala APCu
RUN pecl install apcu && docker-php-ext-enable apcu
Instala extensiones adicionales
RUN pecl install amqp && docker-php-ext-enable amqp
Configura Apache
RUN a2enmod rewrite
Ejecutar composer install
RUN composer install
Ejecutar comandos de Artisan de Laravel
RUN php artisan migrate:fresh --seed
RUN php artisan key:generate
RUN php artisan optimize:clear
Expone el puerto 80 para que puedas acceder a la aplicación desde tu navegador
EXPOSE 80
Inicia Apache cuando se inicia el contenedor
CMD ["apache2-foreground"]
i did that
but idk ill try
make sure you enclose code in triple back ticks
well its seem its working
now try on railway
ok
what even is pecl?
good question
RUN pecl install apcu installs the APCu extension using the pecl package manager.
RUN docker-php-ext-enable apcu enables the installed APCu extension within the PHP environment inside the Docker container.
it's going to be quite difficult to deploy your app if you aren't familiar with the technologies involved
I would recommend getting familiar with all the technologies involved, because I don't know php so I'm only able to really help with the railway side of things
At work, I was given this container to develop something in Laravel that is still in progress. Simultaneously, I started working on this project, and at work, they recommended that I use the same container since I was also going to use Laravel, but I don't know what it has. I just spun up the project and programmed
but I'm not in charge of uploading to production so I don't know what the procedure is like, I only program, I did this on my own that's why I don't know how to take it to production
just learning the language can only get you so far, i can not express how important it is to know the ecosystem too
but with that said, i think your last error might not be your doing, i just saw someone else get the exact same error and i know their deployment was working before
I know, it's My first work, i start like 3 months ago and im learning
But in this case im lost
try re-deploying, Google might have had some network troubles
ok, builiding
same error?
check out this link https://stackoverflow.com/a/59980635
ok, but that clean the db. It only runs once right?
it shouldn't wipe anything, but as always with uncertainty make sure to check that yourself
Hello, yesterday I had to go. Failed again
can you show me the code that connects to the database?
Laravel does that, it is not done manually, you just put the data in the .env
but you still define the variables laravel uses in code, dont you?
DB_CONNECTION=pgsql
I was missing that in the railway variables
i figured it was something like that
It's taking a long time, it's good, at least it doesn't fail xd
unoptimized dockerfile most likely
It didn't fail, but if I see the logs it says that
looks like you ran into the build time limit of the trial plan
try to do docker compose up -d locally and it throws that error. From what I saw, it is normal because the database is not created until the container that has Postgres is finished being created. But on railway there should not be this problem
The important thing is that it did not throw the pecl install amqp error
yeah because it didnt get to that stage yet
No, the php artisan is the last instruction
Look the blue text, the install amqp executed correctly
gotcha
but it didnt on railway, meaning there is an issue with your dockerfile
But how, it's the same dockerfile
^
But how can it be that this line operates locally and not on the railway? It is not a dockerfile problem because it works for me
im sorry but working locally does not mean its a platform issue
try again without changing anything
^
same error?
i think we should pick this back up when you are more familiar with the php ecosystem, im sorry i couldnt help you further
but here he throw another error
From what I see the problem is that you cannot run the php artisan from there
its due to a config issue, im sorry as i dont know php my usefulness has ran out
but there is no way to throw those commands after the build has finished? like a railway console
you can do it in your start command sure
i delete the php artisan commands, building again
well you can't just delete them
this is why i suggested coming back to this when you are more familiar with the php eco system
any variable you use during build needs to be referenced with ARG
https://docs.railway.app/guides/dockerfiles#using-variables-at-build-time
and now your database is likely not migrated
php artisan are exclusive Laravel commands, they are for executing internal commands. They can be done at any time. At any time I run the php artisan migrate and it generates the database, it does not have to be with the creation of the project
but railway does not provide ssh
That's what I meant with the railway console
there is no such thing
you can use
railway run
but that only runs the command locally with the service variables available
your best options are to run them in the dockerfile or in the start commandi found that in a forum
you arent using nixpacks
ill try to do that
it was not my suggestion to use nixpacks
But I'm seeing that many people solve it that way, how can I do it?
maybe you have forgotten to use an ARG
^
^
true
but failed in the next instruction
i think it would be beneficial if you where to also look at the build logs when something fails
I see them from the page but from that file I can't find where the error is reported
chown -R www-data:www-data /var/www/scomputacion/public
chown -R www-data:www-data /var/www/scomputacion/storage/ /var/www/scomputacion/bootstrap/
ln -s /var/www/scomputacion/public /var/www/html
I think I should add that to the dockerfile as well.
your code should not be trying to load a .env file
Now I had a problem, I tried to raise it again but it won't let me because the tables are trying to be created again because in the previous deployement that line worked, how can I delete the database?
you would need to have the migration command do nothing if the migrations have already been ran
Ok, but one question I have. Does the system have to stay on all the time? That is, it never stops? I thought you could choose to turn it off and on whenever you want, but if every time I turn it on it has to generate the dockerfile I think it will be a problem
you want app sleeping?
The app is probably used once a week, to upload orders. We want to do a one-month trial to check that the system works correctly but the free trial does not allow you to have the system running for the entire month, the monthly hours are limited, we thought it could be turned off and on so that it does not consume hours
theres no hour limit on the trial plan??
the only limit is the amount of resources and the trial credits you got
So I was misinformed. I had read that you could only keep it on a certain number of hours per month.
indeed, no time limit, only credit and resource limit
but you could run out of resources before the end of the month, i dont know how much resources your app and database will use
I have to put the permission commands. In theory it worked but that's what I get
put them in the start command then
i put this #Permisoschown -R www-data:www-data /var/www/scomputacion/public
RUN chown -R www-data:www-data /var/www/scomputacion/public
RUN chown -R www-data:www-data /var/www/scomputacion/storage/ /var/www/scomputacion/bootstrap/
RUN ln -s /var/www/scomputacion/public /var/www/html
but keeps :/
^
chown -R www-data:www-data /var/www/scomputacion/public && chown -R www-data:www-data /var/www/scomputacion/storage/ /var/www/scomputacion/bootstrap/ && ln -s /var/www/scomputacion/public /var/www/html
in your dockerfile
this is in the dockerfile
as the start command, not RUN
like this?
no
the start command in a dockerfile is equivalent to the last CMD
please try your best to read my messages so that i can avoid repeating myself
i did this but the error keeps
^