Skip to content

Commit

Permalink
Deploy using Dockerfile instead of buildpack
Browse files Browse the repository at this point in the history
  • Loading branch information
dentarg committed Mar 1, 2024
1 parent 89edae1 commit d7233a4
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ jobs:
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- run: bin/test
- run: |
docker run --rm -it --env PORT=8080 $(docker build . -q) bin/test
57 changes: 57 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.2.3
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# The app lives here
WORKDIR /app

# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y \
curl

# Set production environment
ENV BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"

# Throw-away build stage to reduce size of final image
FROM base as build

# Install build packages
RUN apt-get install --no-install-recommends -y \
build-essential \
git \
pkg-config

# Install application gems
COPY Gemfile Gemfile.lock .ruby-version ./
RUN bundle install

# Copy application code
COPY . .

# Final stage for app image
FROM base

ARG APPUSER=app
ARG APPDIR=/app

# Clean up installation packages to reduce image size
RUN rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build $APPDIR $APPDIR

# Run and own only the runtime files as a non-root user for security
RUN mkdir -p log tmp
RUN groupadd --system --gid 1000 $APPUSER
RUN useradd $APPUSER --uid 1000 --gid 1000 --create-home --shell /bin/bash
RUN chown -R $APPUSER:$APPUSER log tmp
USER 1000:1000

RUN gem install overman

# Start the server by default, this can be overwritten at runtime
CMD ["overman", "start"]
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@ Returns a list of URLs.

Start

foreman start
```bash
overman start

# in docker
bin/docker-run
```

Test

bin/test
```bash
bin/test

# in docker
docker run --rm -it -e PORT=8080 $(docker build . -q) bin/test
```
## Deploy

git push # to github
```bash
git push # to github
```

## Example usage

Expand Down
10 changes: 10 additions & 0 deletions bin/docker-run
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

TAG=${TAG:-urls}
PORT=${PORT:-8080}

# echo commands
set -x

docker build . -t $TAG
docker run --rm -it --publish ${PORT}:${PORT} --env PORT=${PORT} $TAG
9 changes: 5 additions & 4 deletions bin/test
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# echo commands
set -x

PORT=${PORT:-8080}
RUBYOPT=-W:deprecated

# info
Expand All @@ -12,22 +13,22 @@ gem -v

# install app dependencies
bundle install
which foreman || gem install --no-document foreman
which overman || gem install --no-document overman

# start app and wait for it
foreman start &
overman start &
sleep 3

# run tests and save the result

# tests Urls.by_url
# can't use "localhost" because that's a NullURL in twingly/url
curl --silent --fail "http://localhost:5000/?url=localhost.v4.dentarg.net:5000" | grep -q "3 URLs for"
curl --silent --fail "http://localhost:${PORT}/?url=localhost.v4.dentarg.net:${PORT}" | grep -q "3 URLs for"
test1=$?

# tests Urls.by_text
curl --silent --fail --data \
'text=http://www.example.org https://google.com blog.trello.com' "http://localhost:5000/text" | grep -q "3 URLs"
'text=http://www.example.org https://google.com blog.trello.com' "http://localhost:${PORT}/text" | grep -q "3 URLs"
test2=$?

# stop app
Expand Down
3 changes: 0 additions & 3 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
app = "urls"

[build]
builder = "heroku/buildpacks:20"

[env]
PORT = "8080"
RACK_ENV = "production"
Expand Down

0 comments on commit d7233a4

Please sign in to comment.