Skip to content

Commit

Permalink
fixes #26741 - adds containers support for developers
Browse files Browse the repository at this point in the history
this patch introduce a docker file and docker compose example
so that developers can easily get a production docker based env
working quickly.

this by no means is a replacement to the installer or should
effect users besides the porpose of getting up an environment quickly

this does not try to solve docker problems for plugins (but it
can be used to build customized images with a custom plugin installed).

to test this pr you would need docker and docker-compose (I'm running docker-compose-1.22.0-2.)

to use this PR:
(install docker-compose)

docker-compose run app bundle exec rake db:create db:migrate db:seed
docker-compose up

in order to build your own docker image you can either use a service
(such as quay.io) tiggered by a git push or manually OR
to install a recent version of docker that supports multi-stage builds

in theory podman also supports it, but i had stability issues with it
while trying to build images.
  • Loading branch information
ohadlevy authored and timogoebel committed May 21, 2019
1 parent aa73237 commit 89465aa
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
**/node_modules
public/webpack
.git
.bundle
~
db/*.sqlite3*
log/*.log
log/*.log.age
tmp/*
jenkins/reports/*
test/reports/*
db/schema.rb
db/openid-store
config/settings.yaml
config/ignored_environments.yml
config/settings.plugins.d
config/database.yml
config/initializers/local_secret_token.rb
config/initializers/encryption_key.rb
coverage/*
*.sw?
.idea
*.pyc
public/apipie-cache
public/assets
.DS_Store
foreman_client
doc/
.rbenv*
.rvmrc*
.ruby-version*
.ruby-gemset*
locale/*.mo
locale/*/*.edit.po
locale/*/*.po.time_stamp
locale/*/*.pox
locale/*/LC_MESSAGES
coverage/
extras/jumpstart
tags
_build
zeus.json
custom_plan.rb
pkg/
config/hooks
*.csv
vendor/ruby
.env*
package-lock.json
npm-debug.log
.vscode
.yo-rc.json
out*
.byebug_history
.gitignore
.dockerignore
.travis.yml
docker-compose*.yml
Gemfile.lock
85 changes: 85 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Base container that is used for both building and running the app
FROM fedora:30 as base
ARG RUBY_MODULE="ruby:2.6"
ARG NODEJS_MODULE="nodejs:11"

RUN \
echo "tsflags=nodocs" >> /etc/dnf/dnf.conf && \
# dnf -y upgrade && \
dnf -y module install ${RUBY_MODULE} ${NODEJS_MODULE} && \
dnf -y install mysql-libs mariadb-connector-c postgresql-libs ruby{,gems} rubygem-{rake,bundler} nc hostname \
# needed for VNC/SPICE websockets
python python2-numpy && \
dnf clean all && \
rm -rf /var/cache/dnf/

ARG HOME=/home/foreman
WORKDIR $HOME
RUN groupadd -r foreman -f -g 1001 && \
useradd -u 1001 -r -g foreman -d $HOME -s /sbin/nologin \
-c "Foreman Application User" foreman && \
chown -R 1001:1001 $HOME

# 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"]

# Temp container that download gems/npms and compile assets etc
FROM base as builder
ENV RAILS_ENV=production
ENV FOREMAN_APIPIE_LANGS=en
ENV BUNDLER_SKIPPED_GROUPS="test development openid libvirt journald"

RUN \
dnf -y install redhat-rpm-config git \
gcc-c++ make bzip2 gettext \
libxml2-devel libcurl-devel ruby-devel \
mysql-devel postgresql-devel libsq3-devel && \
dnf clean all && \
rm -rf /var/cache/dnf/

ENV DATABASE_URL=sqlite3:tmp/bootstrap-db.sql

ARG HOME=/home/foreman
USER 1001
WORKDIR $HOME
COPY --chown=1001:1001 . ${HOME}/
RUN echo gem '"rdoc"' > bundler.d/container.rb
RUN bundle install --without "${BUNDLER_SKIPPED_GROUPS}" \
--binstubs --clean --path vendor --jobs=5 --retry=3 && \
rm -rf vendor/ruby/*/cache/*.gem && \
find vendor/ruby/*/gems -name "*.c" -delete && \
find vendor/ruby/*/gems -name "*.o" -delete
RUN npm install --no-optional
RUN \
make -C locale all-mo && \
bundle exec rake assets:clean assets:precompile db:migrate && \
bundle exec rake db:seed apipie:cache:index && rm tmp/bootstrap-db.sql
RUN ./node_modules/webpack/bin/webpack.js --config config/webpack.config.js && npm run analyze && rm -rf public/webpack/stats.json
RUN rm -rf vendor/ruby/*/cache vendor/ruby/*/gems/*/node_modules

FROM base

ARG HOME=/home/foreman
ARG RAILS_ENV=production
ENV RAILS_SERVE_STATIC_FILES=true
ENV RAILS_LOG_TO_STDOUT true

USER 1001
WORKDIR ${HOME}
COPY --chown=1001:1001 . ${HOME}/
COPY --from=builder /usr/bin/entrypoint.sh /usr/bin/entrypoint.sh
COPY --from=builder --chown=1001:1001 ${HOME}/.bundle/config ${HOME}/.bundle/config
COPY --from=builder --chown=1001:1001 ${HOME}/Gemfile.lock ${HOME}/Gemfile.lock
COPY --from=builder --chown=1001:1001 ${HOME}/vendor/ruby ${HOME}/vendor/ruby
COPY --from=builder --chown=1001:1001 ${HOME}/public ${HOME}/public
RUN echo gem '"rdoc"' > bundler.d/container.rb

RUN date -u > BUILD_TIME

# Start the main process.
CMD "bundle exec bin/rails server"

EXPOSE 3000/tcp
EXPOSE 5910-5930/tcp
55 changes: 55 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
version: '3.4'
services:
db:
environment:
- MYSQL_USER=foreman
- MYSQL_PASSWORD=foreman
- MYSQL_DATABASE=foreman
- MYSQL_DATADIR_ACTION=upgrade-auto
hostname: db.example.com
image: centos/mariadb-102-centos7
ports:
- 3306
restart: always
healthcheck:
test: ["CMD-SHELL", "nc -z 127.0.0.1 3306 || exit 1"]
interval: 30s
timeout: 30s
retries: 3
volumes:
- db:/var/lib/mysql/data

app: &app_base
image: quay.io/ohadlevy/foreman:containers
command: bundle exec bin/rails server -b 0.0.0.0
build:
context: .
environment:
- DATABASE_URL=mysql2://foreman:foreman@db:3306/foreman
- RAILS_MAX_THREADS=5
- RAILS_ENV=production
hostname: foreman.example.com
links:
- db
ports:
- "${MY_DOCKER_IP:-127.0.0.1}:3000:3000"
- "${MY_DOCKER_IP:-127.0.0.1}:5910-5930:5910-5930"
restart: always
healthcheck:
test: ["CMD-SHELL", "nc -z 127.0.0.1 3000 || exit 1"]
interval: 5m
start_period: 1m

worker:
<<: *app_base
command: bundle exec rake dynflow:executor
hostname: worker.example.com
ports:
- 9999
healthcheck:
test: "ps ax | grep -v grep | grep dynflow_executor"
interval: 1m
start_period: 1m

volumes:
db:
10 changes: 10 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e

export PATH=~/bin:${GEM_HOME}/bin:${PATH}

# Remove a potentially pre-existing server.pid for Rails.
rm -f ~/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

0 comments on commit 89465aa

Please sign in to comment.