Thanks to @squirly's most helpful suggestions, I now have my work-in-progress Docker image up, running tests and serving HTTP requests. Thanks, Tyler! 👏
Ploughing down the list of previously-unknown unknowns is a highly reliable means of instilling a respectful wariness of the extent of that list for the properly-attentive n00b, in any endeavour. This is little different; questions remain, such as:
When Bundler bundles my app in the container, it gives me a warning that I should not run bundler
as root
which, to a guy who did a couple decades of Unix back in the day, seems blindingly obvious. I see the USER
option on the run
command (and for Dockerfiles); at least from a Dockerfile perspective, it seems a bit chicken-and-egg-like, yes? When I build the image, I should just RUN useradd
(and passwd
?) with appropriate environment variables for the username/password, and then use -u the_created_user
on subsequent docker-compose run app
commands?
Back to exercising my search-engine-fu, but again, thanks!
Original content follows:
I'm trying to get a Rails app's dev/test stack up in a Docker image (and accessible from the host Mac's browser/command line, obviously). I'm forgetting something that'll be bloody obvious in hindsight, but I'm much too close to the problem to see it through the Docker waterfall from behind my ears.
I've taken an existing Rails app under development (that yields success on CI),
and modified/created three files while attempting to generally follow two tutorials
(here and
here, both by @mlocher of CodeShip).
Dockerfile
and docker-compose.yml
are obviously new, while config/database.yml
has been changed
to (at least initially) use hard-coded Docker environment variables for Postgres host and port-number settings. (The original,
"pre-Docker" database.yml
file is here for those
who care.)
The obvious problem is that I can't access the app (in the container) from a browser (running in the host Mac); Docker logs seem to suggest that both the Postgres container and the Rails app container were successfully started and are running.
After running docker-compose build
and docker-compose up -d
, I have an apparently running pair of containers, judging by the Docker
app and database logs (reproduced below). That assumption doesn't last long. Shelling in via docker-compose run app bash
and trying to
set up the database from there yields more confusion:
$ docker-compose run app bash
root@f00dc52c4dbe:/app# bin/bundle exec rake db:create
sh: 1: createdb: not found
Could not create database for development.
root@f00dc52c4dbe:/app# which psql
root@f00dc52c4dbe:/app# which createdb
root@f00dc52c4dbe:/app# exit
Am I not running the containers properly? docker-compose ps
yields
$ docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------
newfortstrap_app_1 rails server --port 3000 - ... Up 0.0.0.0:3000->3000/tcp
newfortstrap_db_1 /docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
If you're coming here from Reddit or StackOverflow, kindly comment on this Gist rather than on the post/link that brought you here. Thanks.
First:
POSTGRES_PORT
should beDB_PORT
in config_database.yml.To solve your issue I believe you need to either install the postgres client (
RUN apt-get install postgres-client
) in your rails docker image or calldocker-compose run db created app_development
.