How to configure Rails & Docker app to use Angular? - ruby-on-rails

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?

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 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

How to execute docker compose from outside the directory?

I'm probably doing something very wrong, but I'll ask here just in case since I can't find it. Basically, I have no problem running my docker image when I'm on my pc and I just do "Docker-compose run..." from within the ruby app directory. However, when I push the image to the docker-hub, I want to pull that image on my ubuntu server to then build that image. The problem is that when I do so, I don't really have access to the ruby app, the gemfile or anything so it doens't work at all...
This was my error :
Step 10/23 : COPY Gemfile /myapp/Gemfile COPY failed: stat /var/lib/docker/tmp/docker-builder296802662/Gemfile: no such file or directory
My Dockerfile :
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
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 -qq && apt-get install -y nodejs yarn netcat libpq-dev nano tzdata apt-transport-https
RUN apt-get clean autoclean
RUN rm -rf /var/lib/apt /var/lib/dpkg /var/lib/cacbe /var/lib/log
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile Gemfile.lock /myapp/
RUN bundle install
COPY . .
RUN rm -Rf node_modules/
RUN rm yarn.lock
RUN spring stop
RUN rails webpacker:install
RUN yarn install
RUN yarn upgrade
RUN yarn install --check-files
EXPOSE 3000
# Running the startup script before starting the server
ENTRYPOINT ["sh", "./config/docker/startup.sh"]
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
My docker-compose :
services:
db:
image: mysql:latest
restart: always
command: --default-authentication-plugin=mysql_native_password
# volumes:
# - ./tmp/db:/var/lib/postgresql/data
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test
MYSQL_USERNAME: root
MYSQL_PASSWORD: root
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
links:
- db
environment:
DB_USER: root
DB_NAME: test
DB_PASSWORD: root
DB_HOST: db
DB_PORT: 3306
RAILS_ENV: development
How am I supposed to make it so that on my ubuntu server I can simply pull the image from my repo, build and run it?
Thank you very much.
P.S. I also always get "Your Yarn packages are out of date!"...
If you've pushed your application images to Docker Hub, you need to, all in the web service:
Add the image: name of your Docker Hub image
Remove the build: section
Delete the volumes: that overwrite the image's code
Delete the command: overriding the image's CMD (consider adding the rm -f server.pid command to your startup.sh entrypoint script)
Delete the archaic links: setting
This leaves you with:
version: '3.8'
services:
db: *as_in_the_question
web:
image: 'myname/web:20200622'
ports:
- "3000:3000"
depends_on:
- db
environment:
DB_USER: root
DB_NAME: test
DB_PASSWORD: root
DB_HOST: db
DB_PORT: 3306
RAILS_ENV: development
On the remote system you need to copy only the docker-compose.yml file, and you should be able to run docker-compose up to start it; it will pull the Docker Hub image and run it.
On the local system, if you have both a build: and an image: setting, docker-compose build will tag the image with the name you specify, and docker-compose push will push the built image.

Why to always run db:create when restarting containers?

Following https://docs.docker.com/compose/rails/ with a Dockerfile:
FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
and a docker-compose.yml:
version: '3'
services:
db:
image: postgres
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
it's said:
Stop the application = docker-compose down
Restart the application = docker-compose up AND docker-compose run web rake db:create
Why do we need to create the database every time we restart the containers? Normally we run db:create only once and from then on run db:migrate to run new migrations.
Basically, you have to map your docker local postgresql data files to your own machine.
version: '3'
services:
db:
image: postgres
volumes:
- /var/data/myapp/postgres:/var/lib/postgresql/data:rw
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
with this line:
- /var/data/myapp/postgres:/var/lib/postgresql/data:rw
you'll map /var/data/myapp/postgres to your docker /var/lib/postgresql/data and doesn't matter if you down or not your docker-compose, it'll still be available to the next docker-compose up

Docker-compose and Xvfb incompatibility?

Since I installed Capybara-webkit, I can't launch my specs with docker compose. The next command stays on hold:
$ docker-compose run web xvfb-run -a bundle exec rspec
I thought I have a problem with Capybara-webkit, so I created a SO question and an issue on the repo, but it seems it's more a pb of interaction between docker-compose and xvfb.
If I do first
$ docker-compose run web bash
then
$ xvfb-run -a bundle exec rspec spec
it works fine. I have no clue.
Edit 31/08/17
As requested, here is the docker-compose file:
version: '2'
services:
db:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=XXXXX
volumes:
- mysql-data:/var/lib/mysql
redis:
image: redis
ports:
- "6379:6379"
volumes:
- redis:/data
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/app_dir
- app-gems:/usr/local/bundle
ports:
- "3000:3000"
depends_on:
- db
- redis
volumes:
mysql-data:
driver: local
redis:
driver: local
app-gems:
driver: local
And the Dockerfile:
FROM ruby:2.4.1
RUN apt-get update -qq && apt-get install -y \
build-essential \
libpq-dev \
nodejs \
xvfb \
qt5-default \
libqt5webkit5-dev \
gstreamer1.0-plugins-base \
gstreamer1.0-tools \
gstreamer1.0-x
RUN mkdir /app_dir
WORKDIR /app_dir
ADD Gemfile* /app_dir/
RUN bundle install
COPY . .
In docker-compose.yml
command: ./start.sh
And in start.sh file
#!/bin/bash
xvfb-run "run whatever"
Posting comments as answer since I need formatting
Can you try changing below
command: bundle exec rails s -p 3000 -b '0.0.0.0'
to
entrypoint: xvfb-run -a bundle exec rspec
and try docker-compose up
Also if that doesn't work then try adding tty: true to the service

Resources