TinyTds::Error: Server name not found in configuration files - ruby-on-rails

I am using Docker + Rails + Tiny TDS on Windows.
This configuration worked great until I started dockerizing the environment.
Any help is much appreciated.
Gemfile
gem 'tiny_tds'
gem 'rails', '4.2.8'
DockerFile
FROM ruby:2.4.0
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN apt-get update && apt-get install -y dos2unix
RUN apt-get update && apt-get install -yq freetds-bin
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
# Start the main process.
RUN dos2unix entrypoint.sh && apt-get --purge remove -y dos2unix && rm -rf /var/lib/apt/lists/*
EXPOSE 3000
Docker-compose.yml
version: '3'
services:
db:
image: postgres:12-alpine
redis:
image: redis:alpine
server:
tty: true
stdin_open: true
build: .
command: bash -c "rm -f tmp/pids/server.pid && rake db:create && rake db:migrate && bundle exec rails s -p 3000 -b '0.0.0.0'"
links:
- db
- redis
volumes:
# - ./tmp/db:/var/lib/postgresql/data
- .:/usr/src/app
# - .:/myapp
ports:
- "3000:3000"
- "1433:1433"
- "1434:1434"
environment:
DATABASE_URL: postgres://postgres#db:5432/app?pool=25&encoding=unicode&schema_search_path=public
# DEVISE_JWT_SECRET_KEY: RANDOM_SECRET
REDIS_URL: redis://redis:6379
When I test the connection
client = TinyTds::Client.new username: 'sa',
password: 'password',
port: 1433,
host: 'server_name', # works if you are not using docker
# dataserver: 'server_name', #This did not work as well
timeout: 10000
Error message:
TinyTds::Error: Server name not found in configuration files from
/usr/local/bundle/gems/tiny_tds-1.0.4/lib/tiny_tds/client.rb:43:in
`connect'
Any help is much appreciated.

As per #lyzard-kyng comment on the question the IP address helped diagnose the issue.
The DNS suffix properties were managed by windows. I had to add them to the server_name.

Related

I want to do a cron-job with the help of docker (Aipine) and the whenever gem

Aasumption
I m using docker-compose and wanted to use the whenever gem to do a cron process that deletes at a certain time in Rails, but upon research I found that I have to install and run cron in docker. So I looked into it, but I can't find any information about Alpine regarding cron processing in Rails. Can an
yone tell me how to do this?
What we have achieved
I want to execute a specific process once a day.
Code
Here is my Dockerfile:
FROM ruby:2.7.1-alpine
ARG WORKDIR
ENV RUNTIME_PACKAGES="linux-headers libxml2-dev make gcc libc-dev nodejs tzdata postgresql-dev postgresql git" \
DEV_PACKAGES="build-base curl-dev" \
HOME=/${WORKDIR} \
LANG=C.UTF-8 \
TZ=Asia/Tokyo
RUN echo ${HOME}
WORKDIR ${HOME}
COPY Gemfile* ./
RUN apk update && \
apk upgrade && \
apk add --no-cache ${RUNTIME_PACKAGES} && \
apk add --virtual build-dependencies --no-cache ${DEV_PACKAGES} && \
bundle install -j4 && \
apk del build-dependencies
COPY . .
CMD ["rails", "server", "-b", "0.0.0.0"]
Here is my Docker Compose file:
version: '3.8'
services:
db:
image: postgres:12.3-alpine
environment:
TZ: UTC
PGTZ: UTC
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
volumes:
- ./api/tmp/db:/var/lib/postgresql/data
api:
build:
context: ./api
args:
WORKDIR: $WORKDIR
command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
environment:
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
API_DOMAIN: "localhost:$FRONT_PORT"
APP_URL: "http://localhost:$API_PORT"
volumes:
- ./api:/$WORKDIR
depends_on:
- db
ports:
- "$API_PORT:$CONTAINER_PORT"
mailcatcher:
image: schickling/mailcatcher
ports:
- "1080:1080"
- "1025:1025"
front:
build:
context: ./front
args:
WORKDIR: $WORKDIR
CONTAINER_PORT: $CONTAINER_PORT
API_URL: "http://localhost:$API_PORT"
command: yarn run dev
volumes:
- ./front:/$WORKDIR
ports:
- "$FRONT_PORT:$CONTAINER_PORT"
depends_on:
- api
Actual processing
/config/schedule.rb
require File.expand_path(File.dirname(__FILE__) + "/environment")
ENV.each { |k, v| env(k, v) }
set :output, "#{Rails.root}/log/cron.log"
set :environment, :development
every 1.days do
runner "User.guest_reset"
end
What we tried
I did a lot of research and found a lot of information on using cron with apt, but could not find any information on using apk.
Separate cron into another service in your docker-compose.yml by using the same image as your rails app image (which built by Dockerfile). Then run cron and whenever --update-crontab in the command.
docker-compose.yml
version: '3'
services:
app:
image: myapp
depends_on:
- 'db'
build:
context: .
command: bash -c "rm -f tmp/pids/server.pid &&
bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- ".:/myapp"
cron:
image: myapp
command: bash -c "touch log/cron.log && cron && whenever --update-crontab &&
crontab -l && tail -f log/cron.log"
volumes:
- '.:/myapp'
db:
image: postgres:13
ports: # 127.0.0.1 to only expose the port to loopback
- '127.0.0.1:5432:5432'
volumes:
- 'postgres_dev:/var/lib/postgresql/data'
Dockerfile
FROM ruby:3.0.1
RUN apt-get update -qq && apt-get install -y postgresql-client cron vim \
&& mkdir /myapp
WORKDIR /myapp
ENV BUNDLE_WITHOUT=development:test
COPY Gemfile Gemfile.lock ./
RUN bundle install --jobs 20 --retry 5
COPY package.json ./
RUN npm install --check-files --production

How to share Docker Volume between two docker containers?

I have the following Problem: I have two Docker containers, one for my App and one for NGINX. Now I want to share uploaded images from my app with the NGINX container. I tried to do that using a volume. But when I restart my app container, the images are lost. What can I do to save the images, even after I restarted or recreated the container?
My configuration:
docker-compose.yml
version: '3'
services:
# the application
app:
build:
context: .
dockerfile: ./docker/app/Dockerfile
environment:
- DB_USERNAME=postgres
- DB_PASSWORD=postgres
- DB_PORT=5432
volumes:
- .:/app
- gallery:/app/public/gallery
ports:
- 3000:3000
depends_on:
- db
# the database
db:
image: postgres:11.5
volumes:
- postgres_data:/var/lib/postgresql/data
# the nginx server
web:
build:
context: .
dockerfile: ./docker/web/Dockerfile
volumes:
- gallery:/app/public/gallery
depends_on:
- app
ports:
- 80:80
networks:
default:
external:
name: app-network
volumes:
gallery:
postgres_data:
app/Dockerfile:
FROM ruby:2.7.3
RUN apt-get update -qq
RUN apt-get install -y make autoconf libtool make gcc perl gettext gperf && git clone https://github.com/FreeTDS/freetds.git && cd freetds && sh ./autogen.sh && make && make install
# for imagemagick
RUN apt-get install imagemagick
# for postgres
RUN apt-get install -y libpq-dev
# for nokogiri
RUN apt-get install -y libxml2-dev libxslt1-dev
# for a JS runtime
RUN apt-get install -y nodejs
# Setting an Envioronment-Variable for the Rails App
ENV RAILS_ROOT /var/www/app
RUN mkdir -p $RAILS_ROOT
# Setting the working directory
WORKDIR $RAILS_ROOT
# Setting up the Environment
ENV RAILS_ENV='production'
ENV RACK_ENV='production'
# Adding the Gems
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install --jobs 20 --retry 5 --without development test
# Adding all Project files
COPY . .
RUN bundle exec rake assets:clobber
RUN bundle exec rake assets:precompile
EXPOSE 3000
CMD ["bundle", "exec", "puma", "-p", "3000"]
web/Dockerfile:
# Base Image
FROM nginx
# Dependiencies
RUN apt-get update -qq && apt-get -y install apache2-utils
# Establish where Nginx should look for files
ENV RAILS_ROOT /var/www/app
# Working Directory
WORKDIR $RAILS_ROOT
# Creating the Log-Directory
RUN mkdir log
# Copy static assets
COPY public public/
# Copy the NGINX Config-Template
COPY docker/web/nginx.conf /tmp/docker.nginx
# substitute variable references in the Nginx config template for real values from the environment
# put the final config in its place
RUN envsubst '$RAILS_ROOT' < /tmp/docker.nginx > /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Rather than a volume, you can mount the same directory on the host to multiple Docker containers simultaneously. So long as the containers are not writing to the same file simultaneously, which is not in your described use case, you shouldn’t have a problem.
For example:
docker run -d --name Web1 -v /home/ubuntu/images:/var/www/images httpd
docker run -d --name Other1 -v /home/ubuntu/images:/etc/app/images my-docker-image:latest
If you would rather a Docker volume, this article will give you everything you need to know.

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

Ruby on rails on docker-compose

I'm having problems with a project, using docker-compose, I always use the same Dockerfile and docker-compose.yml in all projects, just changing the version of ruby. However, in just ONE of these projects, I no longer update what I modify in the code, every change I make always reflected, but now it stopped suddenly, and only in one project. I have already refitted build, I have removed all the containers, all the images, downloaded the project again ... and nothing! Just refresh if I stop and upload the container again!
docker-compose.yml :
version: '2'
services:
postgres:
image: 'postgres:9.5'
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:
- postgres
Dockerfile
FROM ruby:2.3.1
RUN apt-get update -qq && apt-get install -y build-essential libpq- dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp
Resolved, in config/environments/development.rb it has to be: config.cache_classes = false

web_1 | /docker-entrypoint.sh: line 99: exec: bundle: not found app_web_1 exited with code 127

I'm trying to use docker to push my existing rails project to a docker container.
I'm using postgres database.
When I did $> docker-compose up
I get following error in the logs.
web_1 | /docker-entrypoint.sh: line 99: exec: bundle: not found
app_web_1 exited with code 127
-
# Dockerfile
FROM ruby:2.2.0
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN gem install bundler
RUN bundle install
ADD . /myapp
FROM postgres:9.4
#FROM library/postgres
ENV POSTGRES_USER my-user-name
ENV POSTGRES_PASSWORD ''
ENV POSTGRES_DB app-database-name
-
# docker-compose.yml
version: '2'
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
You don't need two instruction in your Dockerfile, just use docker-compose for overwrite postgres enviroments images.
You can try this:
# Dockerfile
FROM ruby:2.2.3
# Update ubuntu and deps
RUN apt-get update -qq && apt-get install -y build-essential
# Install postgres dep
RUN apt-get install -y libpq-dev
# Install nokogiri dep
RUN apt-get install -y libxml2-dev libxslt1-dev
# Install JS runtime
RUN apt-get install -y nodejs
ENV APP_HOME /app
RUN mkdir $APP_HOME
WORKDIR $APP_HOME
ADD Gemfile* $APP_HOME/
# Install api deps
RUN bundle install --jobs 4
ADD . $APP_HOME
Now in your docker-compose.yml (I use v1 version) you can try that:
postgres:
image: postgres
environment:
POSTGRES_USER: my-user-name
POSTGRES_PASSWORD: ''
POSTGRES_DB: app-database-name
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -b '0.0.0.0'"
environment:
PORT: 3000
DATABASE_URL: 'postgres://postgres:#postgres:5432/postgres'
ports:
- '3000:3000'
link:
- db
volumes:
- .:/app

Resources