How to access dockerized rails server using "localhost" - ruby-on-rails

I don't play often with Docker so I'm really confused here.
Here is my Dockerfile:
ARG RUBY_VERSION
FROM ruby:${RUBY_VERSION}-slim-buster
ARG PG_MAJOR
ARG NODE_MAJOR
ARG BUNDLER_VERSION
ARG YARN_VERSION
# common dependencies
RUN apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
gnupg2 \
curl \
less \
git \
&& apt-get clean \
&& rm -fr /var/cache/apt/archives/* \
&& rm -fr /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& truncate -s 0 /var/log/*log
# add postgresql to sources list
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& echo 'deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list
# add nodejs to sources list
RUN curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash -
# add yarn to sources list
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
# application dependencies
# we use an external aptfile for that
COPY Aptfile /tmp/Aptfile
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
libpq-dev \
postgresql-client-$PG_MAJOR \
nodejs \
yarn=$YARN_VERSION-1 \
$(cat /tmp/Aptfile | xargs) && \
apt-get clean && \
rm -fr /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
truncate -s 0 /var/log/*log
# configure bundler
ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
# upgrade rubygems and install required bundler version
RUN gem update --system && \
gem install bundler:${BUNDLER_VERSION}
# create a directory for the app code
RUN mkdir -p /app
WORKDIR /app
And here is my docker-compose.yml:
version: '3.8'
services:
app: &app
build:
context: .dockerdev
dockerfile: Dockerfile
args:
BUNDLER_VERSION: '2.1.4'
NODE_MAJOR: '11'
PG_MAJOR: '13'
RUBY_VERSION: '2.7.2'
YARN_VERSION: '1.22.5'
image: example-dev:1.0.0
tmpfs:
- /tmp
backend: &backend
<<: *app
stdin_open: true
tty: true
volumes:
- .:/app:cached
- rails_cache:/app/tmp/cache
- bundle:/usr/local/bundle
- node_modules:/app/node_modules
- packs:/app/public/packs
- .dockerdev/.psqlrc:/root/.psqlrc:ro
environment:
- NODE_ENV=development
- RAILS_ENV=${RAILS_ENV:-development}
- REDIS_URL=redis://redis:6379/
- DATABASE_URL=postgres://postgres:postgres#postgres:5432
- BOOTSNAP_CACHE_DIR=/usr/local/bundle/_bootsnap
- WEBPACKER_DEV_SERVER_HOST=webpacker
- HISTFILE=/app/log/.bash_history
- PSQL_HISTFILE=/app/log/.psql_history
- EDITOR=vi
- MALLOC_ARENA_MAX=2
- WEB_CONCURRENCY=${WEB_CONCURRENCY:-1}
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
runner:
<<: *backend
command: /bin/bash
ports:
- '3000:3000'
- '3002:3002'
rails:
<<: *backend
command: bundle exec rails server -b 0.0.0.0
ports:
- '3000:3000'
sidekiq:
<<: *backend
command: bundle exec sidekiq -C configs/sidekiq.yml
postgres:
image: postgres:13.1
volumes:
- .psqlrc:/root/.psqlrc:ro
- postgres:/var/lib/postgresql/data
- ./log:/root/log:cached
- ./.dockerdev/init.sql:/docker-entrypoint-initdb.d/init.sql
environment:
- PSQL_HISTFILE=/root/log/.psql_history
- POSTGRES_PASSWORD=postgres
ports:
- 5432
healthcheck:
test: pg_isready -U postgres -h 127.0.0.1
interval: 5s
redis:
image: redis:5.0-alpine
volumes:
- redis:/data
ports:
- 6379
healthcheck:
test: redis-cli ping
interval: 1s
timeout: 3s
retries: 30
webpacker:
<<: *app
command: ./bin/webpack-dev-server
ports:
- '3035:3035'
volumes:
- .:/app:cached
- bundle:/usr/local/bundle
- node_modules:/app/node_modules
- packs:/app/public/packs
environment:
- NODE_ENV=${NODE_ENV:-development}
- RAILS_ENV={RAILS_ENV:-development}
- WEBPACKER_DEV_SERVER_HOST=0.0.0.0
volumes:
postgres:
redis:
bundle:
node_modules:
rails_cache:
packs:
Everything works just fine, I can run my runner, install gems, play with db, run rails, etc.
Until I try to reach http://localhost:3000 which only shows:
This site can’t be reached
localhost refused to connect.
Here is the step I do to run rails server:
$ docker-compose run --rm rails
Creating okamii-saas_rails_run ... done
=> Booting Puma
=> Rails 6.0.3.4 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.6 (ruby 2.7.2-p137), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
And here is the result of docker-compose ps:
Name Command State Ports
------------------------------------------------------------------------------------------------------------
okamii-saas_postgres_1 docker-entrypoint.sh postgres Up (healthy) 0.0.0.0:32789->5432/tcp
okamii-saas_rails_run_6fff67202995 bundle exec rails server - ... Up
okamii-saas_redis_1 docker-entrypoint.sh redis ... Up (healthy) 0.0.0.0:32788->6379/tcp
I have the feeling that the fact that nothing shows in the Ports column for okamii-saas_rails_run_6fff67202995 is a sign something is wrong but I don't know why it is empty and what I am supposed to do here. (cf. EDIT 1)
As a note, I know the title say how to using localhost but I really can't access it at all AFAIK :)
===
EDIT 1:
That's not entirely true. I figured that by adding EXPOSE 3000 in my Dockerfile, docker-compose ps will show something in the column Ports for my container but that did not change things a bit.
Here is an updated view of docker-composer ps when using EXPOSE 3000:
Name Command State Ports
------------------------------------------------------------------------------------------------------------
okamii-paas_postgres_1 docker-entrypoint.sh postgres Up (healthy) 0.0.0.0:32771->5432/tcp
okamii-paas_rails_run_d812907346b4 bundle exec rails server - ... Up 3000/tcp
okamii-paas_redis_1 docker-entrypoint.sh redis ... Up (healthy) 0.0.0.0:32770->6379/tcp
EDIT 2:
From what I can read from the doc about EXPOSE, it is only acting as a documentation. It does not do anything else which explains why using it does not change anything.
EDIT 3:
I just tried running docker-compose up -d rails instead of docker-compose run rails and this message started spawning:
$ dc up -d rails
Creating network "okamii-paas_default" with the default driver
Creating okamii-paas_postgres_1 ... done
Creating okamii-paas_redis_1 ... done
Creating okamii-paas_rails_1 ...
Creating okamii-paas_rails_1 ... error
ERROR: for okamii-paas_rails_1 Cannot start service rails: driver failed programming external connectivity on endpoint okamii-paas_rails_1 (5d07dfedfc5c979133ce61a237327edb149a0a6793a85f61f6ad8218a60a510b): Bind for 0.0.0.0:3000 failed: port is already allocated
ERROR: for rails Cannot start service rails: driver failed programming external connectivity on endpoint okamii-paas_rails_1 (5d07dfedfc5c979133ce61a237327edb149a0a6793a85f61f6ad8218a60a510b): Bind for 0.0.0.0:3000 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
I don't understand where the conflict comes from.
After #dbugger's suggestion, I tweaked the ports publication from "3000:3000" to "3000". And now, stuff shows in the Ports column but of course, the mapping is wrong.
Name Command State Ports
------------------------------------------------------------------------------------------------
okamii-paas_postgres_1 docker-entrypoint.sh postgres Up (healthy) 0.0.0.0:32784->5432/tcp
okamii-paas_rails_1 bundle exec rails server - ... Up 0.0.0.0:32786->3000/tcp
okamii-paas_redis_1 docker-entrypoint.sh redis ... Up (healthy) 0.0.0.0:32785->6379/tcp

Related

Rails in docker error when try to start server in Windows WSL2 Docker Desktop Engine(docker compose), but working correctly in a Debian system

This error message appears to 3 images in composed docker container.
exec /usr/bin/entrypoint.sh: no such file or directory
All images related to Ruby execution of services
Sidekiq, Webpack runned by Ruby executable and Web(rails) services
I have tried change every execution to run loading de Gemfile environment using bundle exec, but nothing worked.
Dockerfile
FROM ruby:2.6.6
RUN apt-get update -qq \
&& apt-get install -y curl build-essential libpq-dev postgresql \
nodejs postgresql-client &&\
curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt-get update && apt-get install -y nodejs yarn
ADD . /app
WORKDIR /app
RUN gem install bundler:2.3.22
RUN bundle install
RUN yarn install --check-files
RUN gem install foreman
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 80
CMD ["bash"]
docker-compose.yml
version: '3.3'
services:
db:
image: postgres
ports:
- 5423:5432
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: *****
redis:
image: redis
ports:
- "6379:6379"
volumes:
- 'redis:/data'
depends_on:
- db
webpack:
build: .
command: sh -c 'rm -rf public/packs/* || true && bin/webpack-dev-server --host 0.0.0.0 --port 3035 -w'
volumes:
- .:/app
- /app/node_modules
ports:
- "3035:3035"
depends_on:
- db
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && rails s -b 0.0.0.0 -p 80"
volumes:
- .:/app
ports:
- "80:80"
depends_on:
- db
- redis
- webpack
- chrome
env_file: .env_docker
environment:
RAILS_ENV: development
RAILS_MAX_THREADS: 5
sidekiq:
build: .
command: bundle exec sidekiq -C config/sidekiq.yml
volumes:
- .:/app
depends_on:
- db
- redis
env_file: .env_docker
environment:
RAILS_MAX_THREADS: 5
chrome:
image: selenium/standalone-chrome
ports:
- "4444:4444"
volumes:
- /dev/shm:/dev/shm
depends_on:
- db
- redis
- webpack
- sidekiq
volumes:
redis:
postgres:
Equal to entrypoint.sh exec: #: not found but not resolved
I really want to change my Debian development OS to Windows and work only with containers, not looking to Linux or WSL alternatives

How to configure Rails & Docker app to use Angular?

I have a very basic rails & docker app that I want to add Angular to, to handle all my frontend javascript. However, I can't seem to get Angular to work. I installed Angular via webpacker. As of now I only have the hello-angular files that come standard with rails 6 and webpack.
Dockerfile
FROM ruby:2.6
# Prerequisites
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && apt-get update -q && apt-get install -y nodejs yarn cron
# Cache Gems
WORKDIR /tmp
ADD Gemfile .
ADD Gemfile.lock .
RUN bundle install
# Copy App
WORKDIR /usr/bcb/app
ADD . /usr/bcb/app
# Precompile assets
RUN bin/yarn install
RUN bin/rails assets:precompile
# Expose port 3000 to other containers (Note: not external devices such as our workstation)
ENV PORT 3000
EXPOSE $PORT
# Run the built in Rails server (puma)
CMD ./docker-entrypoint.sh
# clean up APT
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
docker-compose.yml
version: '3.3'
volumes:
dbdata:
driver: local
services:
nginx:
image: nginx
ports:
- '8080:80'
volumes:
- ./nginx/vhost.development.conf:/etc/nginx/conf.d/default.conf
restart: always
depends_on:
- web
db:
image: postgres:11
environment:
- PGDATA=/var/lib/postgresql/data/pgdata
- POSTGRES_USER=appuser
- POSTGRES_PASSWORD=devdb
volumes:
- dbdata:/var/lib/postgresql/data/pgdata
web:
build: . # Builds the image from Dockerfile
environment:
WEBPACK_DEV_SERVER_HOST: webpack_dev_server
links:
- webpack_dev_server
environment:
- RAILS_ENV=development
- RACK_ENV=development
- POSTGRES_USER=appuser
- POSTGRES_PASSWORD=devdb
volumes:
- .:/usr/bcb/app
depends_on:
- db
webpack_dev_server:
image: bcbapp_web
command: ./bin/webpack-dev-server
environment:
NODE_ENV: development
RAILS_ENV: development
WEBPACK_DEV_SERVER_HOST: 0.0.0.0
volumes:
- .:/usr/bcb/app
ports:
- "3035:3035"
docker-entrypoint.sh
rm -f tmp/pids/server*.pid
bin/rails server -b 0.0.0.0 -p $PORT --pid tmp/pids/server.`hostname`.pid
bundle exec rake db:migrate RAILS_ENV=$environment 2>/dev/null || bundle exec rake db:create db:migrate
Here is my github repo:
[1]: https://github.com/zacwillis/bcb
What am I missing?

Rails view rendering is slow in Docker

I'm dockerizing a Rails application, and I'm not sure why the views render so slow. It takes nearly 2 seconds to navigate between any two pages, with some pages taking longer. This problem does not exist when I run the application outside of Docker.
I am using Docker Desktop on a 2015 MBP with 16GB of memory.
Here is my Dockerfile:
FROM ruby:2.5.3
# replace shell with bash so we can source files
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN apt-get update -qq && apt-get install -y build-essential checkinstall \
libpq-dev libvips-dev libvips-tools
# python dependencies
RUN apt-get install -y libreadline-gplv2-dev libncursesw5-dev libssl-dev \
libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
WORKDIR /tmp
RUN wget https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs921/ghostscript-9.21.tar.gz && \
tar xzf ghostscript-9.21.tar.gz && cd ghostscript-9.21 && \
./configure && make && make install
RUN wget https://nodejs.org/download/release/v10.8.0/node-v10.8.0.tar.gz && \
tar xzf node-v10.8.0.tar.gz && cd node-v10.8.0 && \
./configure && make -j4 && make install
WORKDIR /app
RUN gem install bundler -v 1.17.3
RUN gem install foreman -v 0.85.0
RUN npm install -g yarn
And here is my docker-compose.yml:
version: "3"
services:
web:
build:
context: .
dockerfile: Dockerfile
command: bash -c "rm -f /app/tmp/pids/server.pid && foreman start -f Procfile.dev"
volumes:
- .:/app
- npm_packages:/app/node_modules
- bundler_gems:/usr/local/bundle/
ports:
- 3000:3000
- 8888:8888
depends_on:
- postgres
- redis
- mailcatcher
environment:
PGHOST: postgres
PGUSER: postgres
PGPASSWORD: "password"
RAILS_ENV: development
RACK_ENV: development
NODE_ENV: development
mailcatcher:
build:
context: .
dockerfile: Dockerfile
command: bash -c "gem install mailcatcher && mailcatcher --ip 0.0.0.0 --foreground"
volumes:
- .:/app
- npm_packages:/app/node_modules
- bundler_gems:/usr/local/bundle/
ports:
- 1080:1080
- 1025:1025
redis:
image: redis
ports:
- 6379
volumes:
- redis:/data
postgres:
image: postgres:9.6
ports:
- 54320:5432
restart: always
volumes:
- postgres:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: "password"
volumes:
postgres:
redis:
npm_packages:
bundler_gems:
Everything works, it is just very slow.
duration=1623.74 view=1622.89
It could be a known problem with docker-compose running on Mac OS.
Try adding the line 127.0.0.1 localunixsocket.local to the file /etc/hosts
Found on: https://github.com/docker/compose/issues/3419#issuecomment-221793401

Docker on MacOs Errno::ENOENT: No such file or directory - getcwd

I am trying to set up rails 6 with webpacker on docker and I get a weird error once docker up finishes:
Errno::ENOENT: No such file or directory - getcwd
Once I ssh in the container I get this:
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
This is my DockerFile:
ARG RUBY_VERSION
# See explanation below
FROM ruby:$RUBY_VERSION
ARG PG_MAJOR
ARG NODE_MAJOR
ARG BUNDLER_VERSION
ARG YARN_VERSION
# Add PostgreSQL to sources list
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& echo 'deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list
# Add NodeJS to sources list
RUN curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash -
# Add Yarn to the sources list
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
# Install dependencies
# We use an external Aptfile for that, stay tuned
COPY ./dockerDev/Aptfile /tmp/Aptfile
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
postgresql-client-$PG_MAJOR \
nodejs \
yarn=$YARN_VERSION-1 \
$(cat /tmp/Aptfile | xargs) && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
truncate -s 0 /var/log/*log
# Configure bundler and PATH
ENV LANG=C.UTF-8 \
GEM_HOME=/bundle \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
ENV BUNDLE_PATH $GEM_HOME
ENV BUNDLE_APP_CONFIG=$BUNDLE_PATH \
BUNDLE_BIN=$BUNDLE_PATH/bin
ENV PATH /app/bin:$BUNDLE_BIN:$PATH
# Upgrade RubyGems and install required Bundler version
RUN gem update --system && \
gem install bundler:$BUNDLER_VERSION
# Create a directory for the app code
RUN mkdir -p /app
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
COPY package.json /app/package.json
COPY yarn.lock /app/yarn.lock
COPY . /app
# Add a script to be executed every time the container starts. Fixes a Rails-specific issue that prevents the server from restarting when a certain server.pid file pre-exists
# COPY entrypoint.sh /usr/bin/
# RUN chmod +x /usr/bin/entrypoint.sh
# ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
This is my docker-compose:
version: '3.5'
services:
app: &app
build:
context: .
dockerfile: ./Dockerfile
args:
RUBY_VERSION: '2.6.3'
PG_MAJOR: '10'
NODE_MAJOR: '11'
YARN_VERSION: '1.13.0'
BUNDLER_VERSION: '2.0.2'
image: treasure-dev:1.0.0
tmpfs:
- /tmp
backend: &backend
<<: *app
stdin_open: true
tty: true
volumes:
- .:/app
# !!!! WARNING !!! For MacOs add this line. It does have a cost though
# https://docs.docker.com/docker-for-mac/osxfs-caching/#cached
# - .:/app:cached
- rails_cache:/app/tmp/cache
- bundle:/bundle
- node_modules:/app/node_modules
- packs:/app/public/packs
- ./dockerDev/.psqlrc:/root/.psqlrc:ro
environment:
- NODE_ENV=development
- RAILS_ENV=${RAILS_ENV:-development}
# - REDIS_URL=redis://redis:6379/
- DATABASE_URL=postgres://postgres#postgres
- BOOTSNAP_CACHE_DIR=/bundle/bootsnap
- WEBPACKER_DEV_SERVER_HOST=webpacker
- WEB_CONCURRENCY=1
- HISTFILE=/app/log/.bash_history
- PSQL_HISTFILE=/app/log/.psql_history
- EDITOR=vi
depends_on:
- postgres
# - redis
# runner:
# <<: *backend
# command: /bin/bash
# ports:
# - '3000:3000'
# - '3002:3002'
rails:
<<: *backend
command: ["/bin/bash","-c", "script/start-rails"]
ports:
- '3054:3054'
# sidekiq:
# <<: *backend
# command: bundle exec sidekiq -C config/sidekiq.yml
postgres:
image: postgres:10.10
volumes:
- .psqlrc:/root/.psqlrc:ro
- postgres-data:/var/lib/postgresql/data
- ./log:/root/log:cached
environment:
- PSQL_HISTFILE=/root/log/.psql_history
ports:
- "5432:5432"
# redis:
# image: redis:3.2-alpine
# volumes:
# - redis:/data
# ports:
# - 6379
webpacker:
<<: *backend
command: ["script/start-webpack-dev-server"]
ports:
- '3035:3035'
volumes:
- .:/app:cached
- bundle:/bundle
- node_modules:/app/node_modules
- packs:/app/public/packs
environment:
- NODE_ENV=${NODE_ENV:-development}
- RAILS_ENV=${RAILS_ENV:-development}
- WEBPACKER_DEV_SERVER_HOST=0.0.0.0
volumes:
postgres-data:
# redis:
bundle:
node_modules:
rails_cache:
packs:
This is my start-rails script
#!/bin/bash
echo "Preparing container. This may take a while..."
wait_service ${DATABASE_URL:-db} 5432
wait_service webpacker 3035
bundle check || bundle install
yarn install --frozen-lockfile
bundle exec rake db:create
bundle exec rake db:migrate
bundle exec rake db:seed
echo "Done."
bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3054 -b '0.0.0.0'"
These are my versions of docker-compose and docker:
treasure rails-6-new-postgresql % docker -v
Docker version 19.03.4, build 9013bf5
treasure rails-6-new-postgresql % docker-compose -v
docker-compose version 1.24.1, build 4667896b
treasure rails-6-new-postgresql %
I've read a bit on google and it is a docker bug, but I did not find how I can get passed it, my rails server won't start because of it. Any ideas?
Update:
This would be the relevant issue on github:
https://github.com/docker/for-mac/issues/1509
A guy said they do this:
#ryfow might be on to something. In my case there was no subdirectory mount, but two different containers based on the same image were mounting a volume from the same host directory at the same path inside the container. We have two different services in the same repository with virtually identical dependencies, so docker-compose.yml just launches the same image twice with different commands and configurations. We now have a workaround in place which just retries in a loop until it works, so we haven't noticed the problem in a while, but I suspect it's still happening.
But I need a translation for what he is doing, or a piece of code.
Update2 for David:
Dockerfile stays as is
docker-compose can be reduced to:
version: '3.5'
services:
app: &app
build:
context: .
dockerfile: ./Dockerfile
args:
RUBY_VERSION: '2.6.3'
PG_MAJOR: '10'
NODE_MAJOR: '11'
YARN_VERSION: '1.13.0'
BUNDLER_VERSION: '2.0.2'
image: treasure-dev:1.0.0
tmpfs:
- /tmp
backend: &backend
<<: *app
stdin_open: true
tty: true
volumes:
- .:/app
# !!!! WARNING !!! For MacOs add this line. It does have a cost though
# https://docs.docker.com/docker-for-mac/osxfs-caching/#cached
# - .:/app:cached
- rails_cache:/app/tmp/cache
- bundle:/bundle
- node_modules:/app/node_modules
- packs:/app/public/packs
- ./dockerDev/.psqlrc:/root/.psqlrc:ro
environment:
- NODE_ENV=development
- RAILS_ENV=${RAILS_ENV:-development}
# - REDIS_URL=redis://redis:6379/
- DATABASE_URL=postgres://postgres#postgres
- BOOTSNAP_CACHE_DIR=/bundle/bootsnap
- WEBPACKER_DEV_SERVER_HOST=webpacker
- WEB_CONCURRENCY=1
- HISTFILE=/app/log/.bash_history
- PSQL_HISTFILE=/app/log/.psql_history
- EDITOR=vi
depends_on:
- postgres
# - redis
rails:
<<: *backend
command: ["/bin/bash","-c", "script/start-rails"]
ports:
- '3054:3054'
postgres:
image: postgres:10.10
volumes:
- .psqlrc:/root/.psqlrc:ro
- postgres-data:/var/lib/postgresql/data
- ./log:/root/log:cached
environment:
- PSQL_HISTFILE=/root/log/.psql_history
ports:
- "5432:5432"
volumes:
postgres-data:
bundle:
node_modules:
rails_cache:
packs:
ARG RUBY_VERSION
# See explanation below
FROM ruby:$RUBY_VERSION
ARG PG_MAJOR
ARG NODE_MAJOR
ARG BUNDLER_VERSION
ARG YARN_VERSION
# Add PostgreSQL to sources list
RUN curl -sSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
&& echo 'deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main' $PG_MAJOR > /etc/apt/sources.list.d/pgdg.list
# Add NodeJS to sources list
RUN curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash -
# Add Yarn to the sources list
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
# Install dependencies
# We use an external Aptfile for that, stay tuned
COPY ./dockerDev/Aptfile /tmp/Aptfile
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
postgresql-client-$PG_MAJOR \
nodejs \
yarn=$YARN_VERSION-1 \
$(cat /tmp/Aptfile | xargs) && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
truncate -s 0 /var/log/*log
# Configure bundler and PATH
ENV LANG=C.UTF-8 \
GEM_HOME=/bundle \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3
ENV BUNDLE_PATH $GEM_HOME
ENV BUNDLE_APP_CONFIG=$BUNDLE_PATH \
BUNDLE_BIN=$BUNDLE_PATH/bin
ENV PATH /app/bin:$BUNDLE_BIN:$PATH
# Upgrade RubyGems and install required Bundler version
RUN gem update --system && \
gem install bundler:$BUNDLER_VERSION
# Create a directory for the app code
RUN mkdir -p /app
ENV APP_PATH=/app
WORKDIR $APP_PATH
ONBUILD COPY Gemfile* /app/
ONBUILD COPY package.json /app/
ONBUILD COPY yarn.lock /app/
ONBUILD COPY . /app
# Add a script to be executed every time the container starts. Fixes a Rails-specific issue that prevents the server from restarting when a certain server.pid file pre-exists
# COPY entrypoint.sh /usr/bin/
# RUN chmod +x /usr/bin/entrypoint.sh
# ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
Ok it seems that the answer was to use the ONBUILD command, this seems to have fixed it along with that APP_PATH variable..... I've remade the container 5 times, no more error.
I can't explain why, maybe someone with more knowledge can, but this is the answer, at least for me

Docker compose connection issue

I try to run a rails application in docker but I have an issue with docker-compose network, I think...
My Dockerfile looks like this:
FROM ruby:2.3-slim
RUN apt-get update \
&& apt-get install -qq -y --no-install-recommends \
build-essential \
nodejs \
libpq-dev \
git \
tzdata \
libxml2-dev \
libxslt-dev \
ssh \
&& rm -rf /var/lib/apt/lists/*
ENV APP_HOME /var/apps/books-store
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
ENV GEM_HOME /var/apps/books-store/vendor/bundle
ENV PATH $GEM_HOME/bin:$PATH
ENV BUNDLE_PATH $GEM_HOME
ENV BUNDLE_BIN $BUNDLE_PATH/bin
EXPOSE 3000
my docker-compose.yml looks like this:
version: '2'
services:
database:
image: postgres
volumes:
- ./data/pgdata:/pgdata
ports:
- '5555:5432'
env_file:
- '.env'
web:
links:
- database
build: .
volumes:
- .:/var/apps/books-store
ports:
- '3000:3000'
command: [bundle, exec, puma]
env_file:
- '.env'
stdin_open: true
tty: true
When I try docker-compose up, from logs I see rails server starts successfully but when I try to access localhost:3000 from host browser it does not work and I could not understand why. What am I doing wrong?
docker ps:
407b59a2fa99 bookstore_web "bundle exec puma" About a minute ago Up 41 seconds 0.0.0.0:3000->3000/tcp bookstore_web
1837fc3e3f387 postgres "docker-entrypoint..." About a minute ago Up 49 seconds 0.0.0.0:5555->5432/tcp bookstore_database_1
docker-compose logs web:
Attaching to bookstore_web_1
web_1 | Puma starting in single mode...
web_1 | * Version 3.6.2 (ruby 2.3.3-p222), codename: Sleepy Sunday Serenity
web_1 | * Min threads: 0, max threads: 16
web_1 | * Environment: development
web_1 | * Listening on tcp://0.0.0.0:9292
web_1 | Use Ctrl-C to stop

Resources