Skip to content

Instantly share code, notes, and snippets.

@almokhtarbr
Last active March 2, 2023 10:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almokhtarbr/73cb0857cf00f8d4c675d3467039d4c1 to your computer and use it in GitHub Desktop.
Save almokhtarbr/73cb0857cf00f8d4c675d3467039d4c1 to your computer and use it in GitHub Desktop.
Dockerfile
```ruby
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY 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/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]
```
Gemfile
```
source 'https://rubygems.org'
gem 'rails', '~>5
```
Gemfile.lock
touch Gemfile.lock
Next, provide an entrypoint script to fix a
Rails-specific issue that prevents the server
from restarting when a certain server.pid file pre-exists.
This script will be executed every time the container
gets started. entrypoint.sh consists of:
```
#!/bin/bash
set -e
# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid
# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
```
docker-compose.yml
```
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_DB: "db"
POSTGRES_HOST_AUTH_METHOD: "trust"
volumes:
- ./tmp/db:/var/lib/postgresql/data
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
```
docker-compose run web rails new . --force --no-deps --database=postgresql
docker-compose build
Replace the contents of config/database.yml with the following:
```
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password:
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
```
docker-compose up
docker-compose run web rake db:create
docker-compose down
```
Rebuild the application
If you make changes to the Gemfile or the Compose file to try
out some different configurations, you need to rebuild. Some
changes require only docker-compose up --build, but a full
rebuild requires a re-run of docker-compose run web bundle
install to sync changes in the Gemfile.lock to the host,
followed by docker-compose up --build.
Here is an example of the first case, where a full rebuild is
not necessary. Suppose you simply want to change the exposed
port on the local host from 3000 in our first example to 3001.
Make the change to the Compose file to expose port 3000 on the
container through a new port, 3001, on the host, and save the changes:
ports: - "3001:3000"
Now, rebuild and restart the app with docker-compose up --build.
Inside the container, your app is running on the same port as before 3000,
but the Rails Welcome is now available on http://localhost:3001 on your local host.
````
more https://docs.docker.com/compose/rails/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment