In the official Docker image, is there a way to sp...
# box-products
s
In the official Docker image, is there a way to specify a different
server.json
file when starting the server? Looking at the repo, it seems to me like
server.json
is hard-coded in
run.sh
(line 90). My repo has two separate server.json files to support launching in two different modes (front end vs. admin). I have two separate DockerFiles to create containers in the different modes. I've tried various things in the DockerFiles but none of them seem to work:
Copy code
COPY server-frontend.json /app/server.json

RUN box config set server.singleServerMode=false
RUN box server set serverConfigFile=server-frontend.json

ENV BOX_SERVER_SERVERCONFIGFILE="server-frontend.json"
p
Have you considered just building 2 different images of your own based on the official image?
s
Yes, I am actually doing that. I have two separate Dockerfiles - one for each mode. But I'm still not sure what to do to get them to use the correct
server-[mode].json
file.
p
If each Docker image is different why not just give it 2 different server.json files from 2 different code bases?
s
Having to maintain two separate code bases where 99.999% of the files are the same, except for server.json? That seems heavy handed. Also, when I run locally I am mounting my approot in into the containers, so both containers use the same root, which is what I want.
Bear in mind, I am Docker noob so please forgive me if I'm missing something obvious here
p
No you are right, I wasnt thinking of it that way.
Hmm your
server set serverconfigFile=blah.json
should do the trink unless you also have to configure the
start blah.json
command hmm
I havent really configured this way before; but here are the docs: https://commandbox.ortusbooks.com/embedded-server/server.json/using-multiple-server.json-files
s
Maybe in my Dockerfile I can copy in my own custom
<https://github.com/Ortus-Solutions/docker-commandbox/tree/development/build|build>/<https://github.com/Ortus-Solutions/docker-commandbox/tree/development/build/util|util>/start-server.sh
which adds the argument to
box server start
to specify the correct file?
Yeah, I've been referring heavily to that page in the docs. But even running
server set serverconfigFile=blah.json
doesn't seem to do anything.
box server start
just uses defaults and doesn't pick up anything from
blah.json
It does this in local CommandBox too - I do
server set serverconfigFile=blah.json
then run box server start and... doesn't use it. I have to say
box server start blah.json
explicitly.
p
We both are probably missing something here; @bdw429s is going to have the answer, unless @jclausen has the answer
s
Yup, appreciate the help anyways 🙂
I would think something like this in my Dockerfile would work too:
Copy code
COPY ./server-frontend.json ${APP_DIR}/server.json
But no dice. Maybe I have my path syntax wrong? If I
ls
in the container terminal, server.json is not there... so I'm not doing something right
j
COPY
is from the host machine to the image. The order in which you copy matters, because a subsequent copy of a directory can overwrite the previous one.
Try
RUN mv ./server-frontend.json ./server.json
I will take a look at the hard-coded usage of that file. I can probably use an environment variable for that instead.
With environment support, though, you could configure the entire server with the Dockerfile without any
server.json
file at all.
s
So, what I'm trying to achieve long term here is the ability to run the apps locally with commandbox (using the different server-[mode].json) files, and then deploy to Docker containers with a minimum of duplication of settings. i.e. putting all the environment stuff in both the Dockerfiles and the server.json files. Does that make sense?
s
I think ideally if I could just do
ENV BOX_SERVER_SERVERCONFIGFILE=server-frontend.json
in the Dockerfile that would be perfect
j
Totally, though if your front end application doesn’t need all of the dependencies of your back end application, a modular build ( e.g. including only what you need ) on two separate images, might be more efficient.
s
they both are quite intertwined - it's really one app with two modes. They share tons of the model and service layer
j
Got it. I will address the above issue on the next release. For now, I might just suggest modifying the
CMD
of the container to handle an environment variable:
Copy code
CMD mv $APP_DIR/server-${APPLICATION_MODE:-backend}.json $APP_DIR/server.json && $BUILD_DIR/run.sh
As long as your
profile
is set to
production
any
server-*.json
files would not be browsable.
s
So using that method I would just need an
ENV APPLICATION_MODE=frontend
earlier in the Dockerfile?
j
Exactly.
Or just provide it at runtime in the container.
👍 1
s
And yeah, I have Undertow predicates set up to block the server.json files...
j
If you really want to maintain portability, then there’s no need for handling the mode separation in the Dockerfile.
s
I think I get it. I could use the same Dockerfile for both modes.
☝️ 1
j
Exactly. Good call on the undertow settings. I just looked and
blockSensitivePaths
doesn’t handle custom
server-*.json
names: https://commandbox.ortusbooks.com/embedded-server/configuring-your-server/server-rules/baked-in-rules#block-sensitive-paths
p
I almost suggested the application_mode concept heh
👍 1
s
So, the CMD code you sent me above, that would be in addition to or would it replace this line:
Copy code
RUN ${BUILD_DIR}/util/warmup-server.sh
j
CMD
is the command that is run when the container starts.
It’s different than any
RUN
commands in your Dockerfile.
It doesn’t get executed until you start a container on that image.
The
RUN
commands are executions you would perform in the Dockerfile as you craft the image.
s
OK. You are exposing my noob-ness here. Ha. I'm learning though. I think.
j
No problem at all! All n00bs are welcome! 🙂
s
So that CMD line does go into the Dockerfile. But where? Before the RUN or after? Does it matter?
j
As the last line preferably.
s
OK. Giving that a shot! Thank you.
j
No problem. Any time. 🙂
s
This is working, for the most part 🙂 a complication is that I'm launching both containers locally via a
docker-compose.yml
file and mounting my app directory into both containers. So when they both launch simultaneously, stuff in the app root gets rewritten / moved / etc causing some race condition issues. If I launch each container separately everything works correctly. I just end up with an extra
server.json
in my root based on whichever the last launch was. And I had to change
mv
to
cp
I understand once I go to production this won't be an issue. That's another challenge altogether heh
j
Yes, that’s going to be a bit of an issue in development using compose and mounts, because you are going to have extra files created in your local.
I’ll work on getting a patch to the image pushed up this week to address this.
🙌 1
b
@seandaniels I know this is an old thread, but it was just pointed out to me. I'm not sure why I didn't see it back in January, but to address a couple things in it.
Copy code
RUN box server set serverConfigFile=server-frontend.json

ENV BOX_SERVER_SERVERCONFIGFILE="server-frontend.json"
☝️ Neither of those do anything. The first one sets a key in
server.json
that isn't valid or used. The second sets an env var which overrides the same non-existent key in your
server.json
. The valid things you can set in a
server.json
are listed here: https://commandbox.ortusbooks.com/embedded-server/server.json and you'll notice
serverConfigFile
isn't one of them. That is a param to the
server start
command, but it's not a value in your server.json and for obvious reasons. You can't have the server.json file tell CommandBox where to find itself! That's a catch-22!