Skip to content

Instantly share code, notes, and snippets.

@emilebosch
Last active April 10, 2020 10:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save emilebosch/f8fb129ef6028d980fc3b86c3fc1f85f to your computer and use it in GitHub Desktop.
Save emilebosch/f8fb129ef6028d980fc3b86c3fc1f85f to your computer and use it in GitHub Desktop.
Create docker containers on push

Docker build by push

This script makes a build script with a post receive hook. You can use this to build containers when doing a git push to a remote. Basically it will run docker build -t repo on a git push. This is especially handy if you're working in an environment with low internet speeds. It works likes this:

  • Makes a create script on the remote server
  • With the create script you can make a new repository with a buildhook that will run the file ./bin/build in your codebase
  • It passes $1 as branch name, you can use this for deploys or performance checks.

Install the create script on the server, this is responsible for making new repo's to which you can push.

ssh ipa@beers.xyz -- 'wget https://gist.githubusercontent.com/emilebosch/f8fb129ef6028d980fc3b86c3fc1f85f/raw/cff9f5626d2dea42c0d0347cb2022404269bb39e/create && chmod +x create'

This makes a new repo, and shows you the git command to add to you current repo:

 ssh ipa@beers.xyz -- ./create [YOUR REPO NAME]

After this, do a git push!

git push build

It will start a container build and you'll be happy and free.

## Add this file to your code repo under ./bin/build. This is executes on the server by the git push as
## post receive hook. The first argument is the branch name.
##
echo ----------------------------------------
echo Post receive hook $1
echo ----------------------------------------
# Build the app
sudo docker build -t myname/myapp .
# Push to repo
# sudo docker push myname/myapp
# Run the performance test
if [ "$1" == "test" ]
then
echo Running performance test
sudo docker run -it myname/myapp test
fi
# Run the performance test
if [ "$1" == "perf" ]
then
echo Running performance test
sudo docker run -it myname/myapp perf
fi
# Deploy
if [ "$1" == "deploy" ]
then
echo Running performance test
# Stop docker
# Remove docker
# Run docker
sudo docker run -it myname/myapp
fi
git init --bare $1
cat >> $1/hooks/post-receive<<OMG
#!/usr/bin/env bash
A=\`cat -\`
IFS=' ' read -a git <<< "\$A"
from=\${git[0]}
to=\${git[1]}
branch=\${git[2]}
rm -rf /tmp/deploy/$1 && mkdir -p /tmp/deploy/$1
GIT_WORK_TREE=/tmp/deploy/$1 git checkout -q -f \$branch
cd /tmp/deploy/$1 && echo \$to > VERSION && echo \$branch > BRANCH && ./bin/build \$branch
OMG
chmod +x $1/hooks/post-receive
echo "git remote add build `whoami`@`wget -qO- http://ipecho.net/plain`:`pwd`/$1"
## Rename to Dockerfile
FROM microsoft/dotnet:2.0-sdk
WORKDIR /app
COPY . ./
RUN dotnet publish -c Release
ENTRYPOINT ["dotnet", "MyApp.MyApp.App/bin/Release/netcoreapp2.0/MyApp.MyApp.App.dll"]
## Rename to Dockerfile
FROM ruby:2.3-alpine
WORKDIR /app
ENV RAILS_ENV production
#Install imagemagick & ruby
RUN apk update && apk --update add ruby ruby-irb ruby-json ruby-rake \
ruby-bigdecimal ruby-io-console libstdc++ tzdata nodejs imagemagick \
libffi-dev libxml2-dev libxslt-dev postgresql-dev
# Necessary for some gems which are compiled on bundle install
RUN apk add --virtual build-deps git build-base ruby-dev openssl-dev \
imagemagick-dev postgresql-dev libc-dev linux-headers && \
gem install bundler --no-ri --no-rdoc && \
bundle config build.nokogiri --use-system-libraries
COPY Gemfile* /app/
RUN bundle install --clean --without development test
COPY . /app
RUN DATABASE_URL=postgresql://user:pass@127.0.0.1/dbname bundle exec rake assets:precompile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment