Pry Rails on docker not working - ruby-on-rails

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.

Related

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

Rails cron job not running using whenever on Docker

I'm currently using whenever gem for running my Sidekiq worker. It's perfectly fine when I'm running the Sidekiq worker but when I run it in background using cron it's not working.
Dockerfile
FROM ruby:2.6.0-slim
RUN apt-get update -qq \
&& apt-get install -y \
# Needed for certain gems
build-essential \
# Needed for postgres gem
libpq-dev \
# install cron for cronjobs
cron \
# Others
nodejs \
vim-tiny \
# The following are used to trim down the size of the image by removing unneeded data
&& apt-get clean autoclean \
&& apt-get autoremove -y \
&& rm -rf \
/var/lib/apt \
/var/lib/dpkg \
/var/lib/cache \
/var/lib/log
RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
ADD . /app
RUN bundle exec whenever --update-crontab --set environment='development' && /etc/init.d/cron restart
CMD bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"
docker-compose.yml
version: '3'
services:
db:
image: postgres:alpine
volumes:
- ./tmp/db:/var/lib/postgresql/data
ports:
- "5432:5432"
redis:
image: redis:5.0.7
command: redis-server
ports:
- "6379:6379"
sidekiq:
build: .
command: bundle exec sidekiq
depends_on:
- redis
volumes:
- .:/app
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
- redis
- sidekiq
schedule.rb
set :environment, "development"
every 1.minute do
runner "User.create"
end
When I run docker-compose build then docker-compose up and I access the web image using docker exec -it <container-id> sh
crontab -e returns
# Begin Whenever generated tasks for: /app/config/schedule.rb at: 2019-12-16 03:00:58 +0000
* * * * * /bin/bash -l -c 'cd /app && bundle exec bin/rails runner -e development '\''User.create'\'''
# End Whenever generated tasks for: /app/config/schedule.rb at: 2019-12-16 03:00:58 +0000
It should create a user every minute. I tried running this manually and it managed to create a user. I wonder why it's not running from cron

How to define a docker cli service in docker-compose

I have a docker-compose file that runs a few services.
services:
cli:
build:
context: .
dockerfile: docker/cli/Dockerfile
volumes:
- ./drupal8site:/var/www/html/drupal8site
drupal:
container_name: drupal
build:
context: .
dockerfile: docker/DockerFile.drupal
args:
DOC_ROOT: /var/www/html/drupal8site
ports:
- 80:80
volumes:
- ./drupal8site:/var/www/html/drupal8site
restart: always
environment:
APACHE_DOCUMENT_ROOT: /var/www/html/drupal8site/web
mysql:
image: mysql:5.7
ports:
- 3306:3306
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
I would like to add another service which will be a container in which I could run CLI commands (composer, drush for drupal, php, etc).
The following Dockerfile was how I initially defined the cli service but it stops right after it is run. How do I define it so it is part of my docker-compose, shares my mounted volume, and I can interactively connect to it and run CLI commands on it ?
FROM php:7.2-cli
#various programs
RUN apt-get update \
&& apt-get install vim --assume-yes \
&& apt-get install git --assume-yes \
&& apt-get install mysql-client --assume-yes
CMD ["bash"]
Thanks,
Yaron
If you want to run automated scripts on docker images this is obviously a job for a ci-pipeline. You can use CloudFoundry or OpenStack to do this.
But there are many other questions in this post:
1.) How can i share my mounted volume:
You can pass a volume with the -v option to a container. e.g.:
docker run -it -d -v $(pwd)/localFolder:/exposedFolderFromDocker mydockerhub/myawesomeimage
2.) Can I interactively connect to it and run CLI commands on it
docker exec -it docker_cli_1 bash
I recommend to implement features of an docker-image to the individual docker-images Dockerfile. For example copying and running a prepared shell-script:
# your Dockerfile
FROM php:7.2-cli
#various programs
RUN apt-get update \
&& apt-get install vim --assume-yes \
&& apt-get install git --assume-yes \
&& apt-get install mysql-client --assume-yes
# individual changes
COPY your_script.sh /
RUN chown root:root /your_script.sh && \
chmod 0755 /your_script.sh
CMD ["/your_script.sh"]
# a folder to expose
VOLUME /exposedFolderFromDocker
CMD ["bash"]

Cannot see my server in the browser

I'm using Docker Toolbox on Windows 10.
I'm starting a server using python manage.py runserver 0.0.0.0:8000
I'm getting the response Starting development server at http://0.0.0.0:8000/
However when I'm going to http://127.0.0.1:8000/ in my browser, it's not working.
Any idea? What should I check?
I've tried docker run -p 8000:8000 my_image but I get Bind for 0.0.0.0:8000 failed: port is already allocated.
Here is my docker-compose.yml file:
version: '2'
services:
postgres:
image: postgres
restart: on-failure
redis:
image: redis:alpine
restart: on-failure
elasticsearch:
image: "elasticsearch:2.4-alpine"
restart: on-failure
ports:
- "9200:9200"
politikon:
image: *****
links:
- postgres
- redis
- elasticsearch
ports:
- "2233:22"
- "8000:8000"
dns:
- 8.8.8.8
- 8.8.4.4
environment:
- "POSTGRES_PORT_5432_TCP_PORT=5432"
- "POSTGRES_PORT_5432_TCP_ADDR=postgres"
- "DJANGO_SETTINGS_MODULE=*****.settings.dev"
- "BONSAI_URL=http://elasticsearch:9200/"
- "ELASTIC_USERNAME=*****"
- "ELASTIC_PASSWORD=*****"
volumes:
- $PWD:/app
restart: on-failure
And here is my Docker file (without some irrelevant lines):
FROM ubuntu:trusty
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y -qq --fix-missing
RUN apt-get install -y wget
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" >> /etc/apt/sources.list.d/pgdg.list
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
RUN apt-get update -y -qq --fix-missing
RUN apt-get upgrade -y -qq
RUN apt-get install -y python-dev python-pip postgresql-client-common postgresql postgresql-contrib postgresql-9.5 libpq-dev git libmemcached-dev curl openssh-server mercurial gettext vim libjpeg-dev libjpeg8-dev
RUN echo "locale-gen en_US.UTF-8" >> /etc/profile
RUN echo "dpkg-reconfigure locales" >> /etc/profile
RUN echo "export VISIBLE=now" >> /etc/profile
RUN echo "KexAlgorithms=diffie-hellman-group1-sha1" >> /etc/ssh/sshd_config
RUN echo "PermitUserEnvironment=yes" >> /etc/ssh/sshd_config
ENV PORT 8000
EXPOSE 8000
EXPOSE 22
RUN touch /root/.bash_profile
RUN echo "cd /app" >> /root/.bash_profile
RUN mkdir /root/.ssh/
RUN touch /root/.ssh/environment
CMD env >> /root/.ssh/environment; export -p | grep _ >> /etc/profile; /usr/sbin/sshd -D;
ADD /requirements.txt /app/
WORKDIR /app
RUN apt-get install zlib1g-dev
RUN pip install -r requirements.txt
CMD tail -f LICENSE

Docker varnish start with command but not with docker-compose

I'm new to docker and try to convert my actual web stack in it.
Currently I use this configuration:
varnish -> nginx -> php-fpm -> mysql
I have already convert php-fpm and nginx and now tries varnish.
When I run my image with a command, all is fine but when I put it in docker-compose my container restart indefinitely.
Command:
name="varnish"
cd $installDirectory/$name
docker build -t $name .
docker rm -f $(docker ps -a | grep $name | cut -d' ' -f1)
docker run -d -P --name $name \
-p 80:80 \
--link nginx:nginx \
-v /home/webstack/varnish/:/etc/varnish/ \
-t $name
My docker-compose.yml:
php-fpm:
restart: always
build: ./php-fpm
volumes:
- "/home/webstack/www/:/var/www/"
nginx:
restart: always
build: ./nginx
ports:
- "8080:8080"
volumes:
- "/home/webstack/nginx/:/etc/nginx/"
- "/home/webstack/log/:/var/log/nginx/"
- "/home/webstack/www/:/var/www/"
links:
- "php-fpm:php-fpm"
varnish:
restart: always
build: ./varnish
ports:
- "80:80"
volumes:
- "/home/webstack/varnish/:/etc/varnish/"
links:
- "nginx:nginx"
I have no result with docker logs webstack_varnish_1 and docker ps -a result show:
688c5aace1b3 webstack_varnish "/bin/bash" 16 seconds ago Restarting (0) 5 seconds ago 0.0.0.0:80->80/tcp
Here you can see my Dockerfile:
FROM debian:jessie
# Update apt sources
RUN apt-get -qq update
RUN apt-get install -y curl apt-transport-https
RUN sh -c "curl https://repo.varnish-cache.org/GPG-key.txt | apt-key add -"
RUN echo "deb https://repo.varnish-cache.org/debian/ jessie varnish-4.1" > /etc/apt/sources.list.d/varnish-cache.list
# Update the package repository
RUN apt-get -qq update
# Install varnish
RUN apt-get install -y varnish
# Expose port 80
EXPOSE 80
What I am doing wrong please?
Regards.
Your varnish Dockerfile seems to be missing ENTRYPOINT and/or CMD directives that would actually launch Varnish.
We have found the solution here :
https://github.com/docker/compose/issues/2563
I have to add tty: true to my varnish config.
Regards.

Resources