I am following this tutorial from docker Docker Rails and I have created a folder and added this code below in my docker file.
FROM ruby:2.5
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 my docker compose file code is:
version: '3'
services:
db:
image: postgres
volumes:
- .data:/var/lib/postgresql/data
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
I am following tutorial when I am running docker compose up I can just see this error:
Could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
What is wrong here I don't know how to inspect and detect error how to fix this.
You need environment variables within your web container so that it knows how to connect to the db container.
version: '3'
services:
db:
image: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=
volumes:
- ./data:/var/lib/postgresql/data
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
environment:
- PGHOST=db
- PGUSER=postgres
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
Please go to your database.yml and add host set to db , then username and password and run the command again.
Related
I'm trying to run a rails server in a docker image along with a mysql and vue frontend image. I'm using ruby 3 and rails 6. The mysql and frontend image both start without problems. However the rails images doesn't start.
I'm on a Macbook Pro with MacOS Monterey and Docker Desktop 4.5.0
this is my docker-compose.yml:
version: "3"
services:
mysql:
image: mysql:8.0.21
command:
- --default-authentication-plugin=mysql_native_password
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=nauza_backend_development
ports:
- "3307:3306"
volumes:
- mysql:/var/lib/mysql
backend:
build:
context: nauza-backend
args:
UID: ${UID:-1001}
tty: true
stdin_open: true
command:
bundle exec rails s -p 8080 -b '0.0.0.0'
volumes:
- ./nauza-backend:/usr/src/app
# attach a volume at /bundle to cache gems
- bundle:/bundle
# attach a volume at ./node_modules to cache node modules
- node-modules:/usr/src/app/node_modules
# attach a volume at ./tmp to cache asset compilation files
- tmp:/usr/src/app/tmp
environment:
- RAILS_ENV=development
ports:
- "8080:8080"
depends_on:
- mysql
user: rails
environment:
- RAILS_ENV=development
- MYSQL_HOST=mysql
- MYSQL_USER=root
- MYSQL_PASSWORD=root
frontend:
build:
context: nauza-frontend
args:
UID: ${UID:-1001}
volumes:
- ./nauza-frontend:/usr/src/app
ports:
- "3000:3000"
user: frontend
volumes:
bundle:
driver: local
mysql:
driver: local
tmp:
driver: local
node-modules:
driver: local
and this is my Dockerfile:
FROM ruby:3.0.2
ARG UID
RUN adduser rails --uid $UID --disabled-password --gecos ""
ENV APP /usr/src/app
RUN mkdir $APP
WORKDIR $APP
ENV EDITOR=vim
RUN apt-get update \
&& apt-get install -y \
nmap \
vim
COPY Gemfile* $APP/
RUN bundle install -j3 --path vendor/bundle
COPY . $APP/
CMD ["rails", "server", "-p", "8080", "-b", "0.0.0.0"]
when I try to start this with docker-compose up on my Mac I get the following error:
/usr/local/lib/ruby/3.0.0/fileutils.rb:253:in `mkdir': Permission denied # dir_s_mkdir - /usr/src/app/tmp/cache (Errno::EACCES)
Any ideas on how to fix this?
Remove the line - tmp:/usr/src/app/tmp on your Dockerfile.
You don't need to access temp files of your container I would say. 🙂
I am setting up docker-compose for an existing Ruby on Rails project. I am using docker-compose version 1.23.1, build b02f1306 and Docker version 18.09.0, build 4d60db4
When I am trying to start my containers for development using docker-compose up --build my web and worker containers are exiting with code 10. When I /bin/bash into them the /web_gen folder only contains a /tmp/db inside of that and postgres files inside of that.
I can get the containers working by changing the volumes to - /web_gen but then the volumes will not hot reload.
My docker-compose.yml
version: '3'
services:
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/web_gen
ports:
- "3000:3000"
depends_on:
- db
- redis
db:
image: 'postgres:9.4.5'
volumes:
- ./tmp/db:/var/lib/postgresql/data
redis:
image: 'bitnami/redis:latest'
environment:
- ALLOW_EMPTY_PASSWORD=yes
worker:
build: .
command: bundle exec sidekiq -c 1
volumes:
- .:/web_gen
depends_on:
- redis
Dockerfile
FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /web_gen
WORKDIR /web_gen
COPY Gemfile /web_gen/Gemfile
COPY Gemfile.lock /web_gen/Gemfile.lock
RUN bundle install
COPY . /web_gen
I got this error suddenly.
Before getting this error I could connect.
$ curl localhost:3000
curl: (7) Failed to connect to localhost port 3000: Connection refused
My environments are below.
OS:macOS Sierra 10.12.6
Docker version 18.06.1-ce, build e68fc7a
docker-compose.yml
version: "3"
services:
web:
build: web
ports:
- "3000:3000"
environment:
- "DATABASE_HOST=db"
- "DATABASE_PORT=5432"
- "DATABASE_USER=********"
- "DATABASE_PASSWORD=********"
links:
- db
volumes:
- "./app:/app" #共有フォルダの設定
stdin_open: true
db:
image: postgres:10.1
ports:
- "5432:5432"
environment:
- "POSTGRES_USER=********"
- "POSTGRES_PASSWORD=********"
Dockerfile
FROM ruby:2.5.0
RUN apt-get update && apt-get install -y build-essential libpq-dev postgresql-client
RUN gem install rails
RUN mkdir /app
WORKDIR /app
If you have some suggestion to solve this please tell me.
Thanks.
you should specify your command for running application like: bundle exec rails s -p 3000 -b '0.0.0.0' which run your server on port 3000 and bind it to local network 0.0.0.0.
so you should write it in your Dockerfile in the last line:
RUN bundle exec rails s -p 3000 -b '0.0.0.0'
It ran just before I rebooted my machine, and suddenly I get Could not locate Gemfile or .bundle/ directory when starting the container.
During build, which completes without issue, I can see the contents of /app are correct, but on startup, /app only contains a .bundle directory and nothing else.
UPDATE: Turns out the volume ./documents_api:/app is what isn't working. Environment is docker for windows 17.09.1 running as administrator
Here is my folder structure:
./
.env
docker-compose.yml
documents_api/
<typical rails directory contents>
Dockerfile
.env just contains RAILS_ENV=development
the dockerfile contains:
FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /app
WORKDIR /app
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install
ADD . /app
docker-compose.yml contains:
version: '3'
services:
database:
image: mongo
volumes:
- mongo:/var/lib/mongo
env_file:
- .env
ports:
- "27017:27017"
documents:
build: ./documents_api
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- ./documents_api:/app
env_file:
- .env
expose:
- "3000"
depends_on:
- database
frontend:
image: nginx
build: ./web
depends_on:
- documents
ports:
- "80:80"
- "144:144"
# Persistence
volumes:
mongo:
Turns out I needed to remap my shared drives in the settings. It had lost the credentials after the reboot and was silently failing to map the volume.
I've created the docker-compose.yml file below to create a container based on Ruby image and a container based on MySQL image. When I execute docker-compose up, the MySQL container seems to be created correctly, however it is not run in the background. How can I configure it to do so using the docker-compose.yml file?
version: '2'
services:
web:
build:
context: .
dockerfile: .docker/rails.dockerfile
volumes:
- .:/var/www
ports:
- "3000:3000"
depends_on:
- 'mysql'
networks:
- ddoc-network
mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: 'SOMETHING'
networks:
- ddoc-network
networks:
ddoc-network:
driver: bridge
rails.dockerfile
FROM ruby:2.3.1
MAINTAINER Juliano Nunes
RUN apt-get update -qq && apt-get install -y build-essential mysql-client libmysqlclient-dev nodejs
RUN mkdir /var/www
WORKDIR /var/www
ADD Gemfile /var/www/Gemfile
ADD Gemfile.lock /var/www/Gemfile.lock
RUN bundle install
ADD . /var/www
CMD ['bundle', 'exec', 'rails', 'server', '-b', '0.0.0.0']
You can always use docker-compose up -d to run your containers in detached mode.
Check docker-compose up --help for more info.