Skip to content

Instantly share code, notes, and snippets.

@jdickey
Last active November 16, 2015 08:15
Show Gist options
  • Save jdickey/59cbbac9490f5ba655bf to your computer and use it in GitHub Desktop.
Save jdickey/59cbbac9490f5ba655bf to your computer and use it in GitHub Desktop.

SOLVED! (for an initial value of 'solved', anyway)

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 

Help?!?

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.

FROM ruby:2.2.3
# HACK: httpredir breaks often from Singapore, so....
RUN echo deb http://mirror.0x.sg/debian jessie main > /etc/apt/sources.list && echo deb http://mirror.0x.sg/debian jessie-updates main >> /etc/apt/sources.list && echo deb http://security.debian.org jessie/updates main >> /etc/apt/sources.list
RUN apt-get update -qq && apt-get install -y build-essential
# for postgres
RUN apt-get install -y libpq-dev
# for nokogiri
RUN apt-get install -y libxml2-dev libxslt1-dev
# for capybara-webkit
RUN apt-get install -y libqt4-webkit libqt4-dev xvfb
# for a JS runtime
RUN apt-get install -y nodejs
ENV APP_HOME /app
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
ADD Gemfile* $APP_HOME/
ADD . $APP_HOME
RUN gem install bundler brakeman
RUN bundle install
# RUN bundle exec rake db:create:all db:migrate db:reset
# The main command to run when the container starts. Also
# tell the Rails dev server to bind to all interfaces by
# default.
# CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
db:
image: postgres:9.4.5
ports:
- "5432:5432"
app:
build: .
command: rails server --port 3000 --binding 0.0.0.0
ports:
- "3000:3000"
links:
- db
volumes:
- .:/app
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
username: postgres
host: <%= ENV['DB_PORT_5432_TCP_ADDR'] %>
port: <%= ENV['POSTGRES_PORT_5432_TCP_PORT'] %>
development:
<<: *default
database: app_development
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run
# "rake". Do not set this db to the same as development or
# production.
test:
<<: *default
database: app_test
2015-11-15T17:31:35.822098284Z => Booting Thin
2015-11-15T17:31:35.822237595Z => Rails 4.2.4 application starting in development on http://0.0.0.0:3000
2015-11-15T17:31:35.822275875Z => Run `rails server -h` for more startup options
2015-11-15T17:31:35.822901871Z => Ctrl-C to shutdown server
2015-11-15T17:31:37.076030168Z Thin web server (v1.6.4 codename Gob Bluth)
2015-11-15T17:31:37.076724960Z Maximum connections set to 1024
2015-11-15T17:31:37.077296773Z Listening on 0.0.0.0:3000, CTRL+C to stop
2015-11-15T17:31:30.445313247Z The files belonging to this database system will be owned by user "postgres".
2015-11-15T17:31:30.445438398Z This user must also own the server process.
2015-11-15T17:31:30.445447895Z
2015-11-15T17:31:30.445652775Z The database cluster will be initialized with locale "en_US.utf8".
2015-11-15T17:31:30.445682817Z The default database encoding has accordingly been set to "UTF8".
2015-11-15T17:31:30.445703196Z The default text search configuration will be set to "english".
2015-11-15T17:31:30.445738621Z
2015-11-15T17:31:30.445757978Z Data page checksums are disabled.
2015-11-15T17:31:30.445827272Z
2015-11-15T17:31:30.445870656Z fixing permissions on existing directory /var/lib/postgresql/data ... ok
2015-11-15T17:31:30.446680167Z creating subdirectories ... ok
2015-11-15T17:31:30.464350234Z selecting default max_connections ... 100
2015-11-15T17:31:30.485849996Z selecting default shared_buffers ... 128MB
2015-11-15T17:31:30.485962501Z selecting dynamic shared memory implementation ... posix
2015-11-15T17:31:30.774745321Z creating configuration files ... ok
2015-11-15T17:31:31.852074605Z creating template1 database in /var/lib/postgresql/data/base/1 ... ok
2015-11-15T17:31:31.896373610Z initializing pg_authid ... ok
2015-11-15T17:31:31.992355632Z initializing dependencies ... ok
2015-11-15T17:31:32.154258719Z creating system views ... ok
2015-11-15T17:31:32.268140694Z loading system objects' descriptions ... ok
2015-11-15T17:31:32.310639063Z creating collations ... ok
2015-11-15T17:31:32.436275664Z creating conversions ... ok
2015-11-15T17:31:32.491981233Z creating dictionaries ... ok
2015-11-15T17:31:32.548331550Z setting privileges on built-in objects ... ok
2015-11-15T17:31:32.967110634Z creating information schema ... ok
2015-11-15T17:31:33.026303316Z loading PL/pgSQL server-side language ... ok
2015-11-15T17:31:33.536368191Z vacuuming database template1 ... ok
2015-11-15T17:31:33.626636969Z copying template1 to template0 ... ok
2015-11-15T17:31:33.723984532Z copying template1 to postgres ... ok
2015-11-15T17:31:33.874606793Z syncing data to disk ... ok
2015-11-15T17:31:33.886426379Z
2015-11-15T17:31:33.886435054Z Success. You can now start the database server using:
2015-11-15T17:31:33.886441987Z
2015-11-15T17:31:33.886457770Z postgres -D /var/lib/postgresql/data
2015-11-15T17:31:33.886474941Z or
2015-11-15T17:31:33.886598358Z pg_ctl -D /var/lib/postgresql/data -l logfile start
2015-11-15T17:31:33.886609295Z
2015-11-15T17:31:33.996705305Z waiting for server to start....LOG: database system was shut down at 2015-11-15 17:31:33 UTC
2015-11-15T17:31:34.006469977Z LOG: MultiXact member wraparound protections are now enabled
2015-11-15T17:31:34.008136436Z LOG: database system is ready to accept connections
2015-11-15T17:31:34.008490988Z LOG: autovacuum launcher started
2015-11-15T17:31:34.916372700Z done
2015-11-15T17:31:34.926373612Z server started
2015-11-15T17:31:34.935994619Z ALTER ROLE
2015-11-15T17:31:34.936610729Z
2015-11-15T17:31:34.936759903Z
2015-11-15T17:31:34.936880023Z /docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
2015-11-15T17:31:34.937054634Z
2015-11-15T17:31:34.956217925Z LOG: received fast shutdown request
2015-11-15T17:31:34.956360628Z LOG: aborting any active transactions
2015-11-15T17:31:34.956524527Z LOG: autovacuum launcher shutting down
2015-11-15T17:31:34.966798930Z LOG: shutting down
2015-11-15T17:31:35.038862594Z waiting for server to shut down....LOG: database system is shut down
2015-11-15T17:31:35.968432883Z done
2015-11-15T17:31:35.968591229Z server stopped
2015-11-15T17:31:35.982277540Z
2015-11-15T17:31:35.982348653Z PostgreSQL init process complete; ready for start up.
2015-11-15T17:31:35.982513098Z
@squirly
Copy link

squirly commented Nov 15, 2015

First: POSTGRES_PORT should be DB_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 call docker-compose run db created app_development.

@squirly
Copy link

squirly commented Nov 15, 2015

Are you accessing with localhost:3000? You need to use the docker machine ip. Either boot2docker ip or docker-machine ls. Then navigate to ip_address:3000.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment