Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 -- 'wget && chmod +x create'

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

 ssh -- ./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" ]
echo Running performance test
sudo docker run -it myname/myapp test
# Run the performance test
if [ "$1" == "perf" ]
echo Running performance test
sudo docker run -it myname/myapp perf
# Deploy
if [ "$1" == "deploy" ]
echo Running performance test
# Stop docker
# Remove docker
# Run docker
sudo docker run -it myname/myapp
git init --bare $1
cat >> $1/hooks/post-receive<<OMG
#!/usr/bin/env bash
A=\`cat -\`
IFS=' ' read -a git <<< "\$A"
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
chmod +x $1/hooks/post-receive
echo "git remote add build `whoami`@`wget -qO-`:`pwd`/$1"
## Rename to Dockerfile
FROM microsoft/dotnet:2.0-sdk
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
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@ bundle exec rake assets:precompile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment