Docker compose connection issue - ruby-on-rails

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

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

Files which generated through docker-compose run web rails g doesn't create

I have project which used docker-compose to provide environment to developers. The application is running fine on docker-compose build command and running on 0.0.0.0:3000 on docker-compose up command. When I am trying to run the command docker-compose run web rails g uploader or docker-compose run web rails g migration it's show in console thats they successfuly create but when I check project there are no files.
This is my Dockerfile:
# Base image
FROM ruby:2.7.0
# Set enviroment variables in docker
ENV INSTALL_PATH=/app \
RAILS_ENV=production \
RACK_ENV=$RAILS_ENV \
RAILS_LOG_TO_STDOUT=true \
RAILS_SERVE_STATIC_FILES=true \
SECRET_KEY_BASE=ad187ccccdf25beb51568211a26b0bff237385d79df37e08151acda85266f9a469f37926450ba18d9362ec5e83d1b612c09368bc59dc895cb5ce2798a3ab456b
RUN env
# Ensure gems are cached and only get updated when they change. This will
# drastically increase build times when your gems do not change.
ADD Gemfile* $INSTALL_PATH/
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 \
&& apt-get install -qq -y build-essential nodejs libpq-dev cron htop vim sqlite3 yarn imagemagick netcat --fix-missing --no-install-recommends \
&& cd $INSTALL_PATH; bundle install --jobs 20 --retry 5
WORKDIR $INSTALL_PATH
ADD . .
RUN mv config/database.docker.yml config/database.yml \
# Fix windows line ending from windows runners
&& find ./ -type f -exec sed -i 's/\r$//' {} + \
&& chmod +x docker/* \
&& yarn install --check-files \
&& RAILS_ENV=$RAILS_ENV bundle exec rails assets:precompile \
&& chown -R nobody:nogroup $INSTALL_PATH
USER nobody
# Expose a volume so that nginx will be able to read in assets in production.
VOLUME ["$INSTALL_PATH/public"]
EXPOSE 3000
CMD ["docker/startup.sh"]
This one is my docker-compose.yml:
version: '2'
volumes:
database_data:
driver: local
web_rails_public: {}
services:
web:
restart: always
image: eu.gcr.io/academic-ivy-225422/joystree_web
container_name: joystree_web_app_container
build: .
volumes:
- web_rails_public:/app/public
env_file:
- '.env.web'
ports:
- "3000:3000"
links:
- "database:database"
depends_on:
- database
database:
restart: always
container_name: joystree_postgres_container
image: postgres:11
env_file:
- '.env.db'
ports:
- "5432:5432"
volumes:
- database_data:/var/lib/postgresql/data
I had this same problem and solved it by following these steps:
1 - in Dockerfile add the fallowing lines, for be able to create new files:
RUN mkdir /home/web \
&& chown $(id -un):$(id -gn) /home/web
WORKDIR /home/web
2 - in docker-compose.yml web the volume should be .:/home/web or just the same as you call in mkdir
web:
volumes:
- .:/home/web
I hope that solve your problem too

How to access dockerized rails server using "localhost"

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

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

Pry Rails on docker not working

I tried the approach with tty: true stdin_open: true inside docker-compose.yml and attaching to the container id (following http://www.chris-kelly.net/2016/07/25/debugging-rails-with-pry-within-a-docker-container/) but it just hangs.
I also tried docker-compose run --service-ports web following this article https://blog.carbonfive.com/2015/03/17/docker-rails-docker-compose-together-in-your-development-workflow/ but it also hangs the request when binding.pry
Could supervisord affect this?
Here's the Dockerfile:
FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev supervisor
RUN curl -sL https://deb.nodesource.com/setup_9.x | bash - && apt-get install -yq nodejs
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install yarn
RUN mkdir /example
WORKDIR /example
COPY Gemfile /example/Gemfile
COPY Gemfile.lock /example/Gemfile.lock
RUN bundle install
COPY . /example
COPY docker/supervisor.conf /etc/supervisor/conf.d/example.conf
RUN cd client-app && npm install
CMD supervisord -n
And the docker-compose.yml:
version: '3'
services:
db:
image: postgres
web:
build: .
environment:
API_HOST: http://localhost:3000/api
volumes:
- .:/example
ports:
- "3000:3000"
- "4200:4200"
depends_on:
- db
And the supervisor.conf:
[program:rails]
directory=/example
command=rails server -b 0.0.0.0 -p 3000
autostart=true
autorestart=true
[program:npm]
directory=/example
command=/bin/bash -c "yarn && cd client-app && npm run docker-start"
autostart=true
autorestart=true
(assuming you're running postgres something like docker run -d --name=postgres ...)
I would ditch compose and try
docker build -t web .
docker run --link postgres:db -it -p 3000:3000 -p 4200:4200 web
If that fails, I'd punt and
docker run --link postgres:db -it -p 3000:3000 -p 4200:4200 web bash
Then try running rails s, rails c, etc manually within the container.

Resources