Skip to content

Instantly share code, notes, and snippets.

@manojkumararyan
Last active July 14, 2021 07:08
Show Gist options
  • Save manojkumararyan/6f911e2d780214a08f576e05cd0bd155 to your computer and use it in GitHub Desktop.
Save manojkumararyan/6f911e2d780214a08f576e05cd0bd155 to your computer and use it in GitHub Desktop.

Dockerize your application

Sample rails application Dockerfile

FROM ruby:2.6.4

RUN apt-get update -o Acquire::CompressionTypes::Order::=gz -qq && apt-get install -y build-essential libpq-dev nodejs

RUN apt-get update --fix-missing

RUN gem install bundler -v 1.17.3

ENV APPLICATION_NAME={{your application name}}

RUN mkdir -p /$APPLICATION_NAME

WORKDIR /$APPLICATION_NAME

ADD Gemfile /$APPLICATION_NAME/Gemfile
ADD Gemfile.lock /$APPLICATION_NAME/Gemfile.lock

ADD . /$APPLICATION_NAME

RUN bundle install

ARG TAG
ENV APP_VERSION=${TAG}
ARG CI_COMMIT_BRANCH
ENV BUILD_BRANCH=${CI_COMMIT_BRANCH}
ARG RAILS_ENV=production
ENV RAILS_ENV="${RAILS_ENV}"
ENV RAILS_SERVE_STATIC_FILES true

RUN RAILS_ENV=${RAILS_ENV} bundle exec rails assets:precompile

EXPOSE 3000
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "3000"]
  • Once your Dockerfile is ready, run this command to build your application image.
docker image build -t {application_name}:{tag} {Dockerfile path}
  • On successful image creation, run this command to check your image.
docker image ls #list docker images
  • After that, run your image to check everything is working as expected.
docker container run -it {REPOSITORY}:{TAG} 
#REPOSITORY & TAG, you will get this from list docker images command

Kubernetes deployment

  • First, you will get an app 'dev-cluster' from develops team for your kubernetes cluster.
  • You will need to update this file dev-cluster > cloud > builder-environment.yml as per your need & push the code.
cloudProvider: azure
region: centralindia
environment: dev # dev|stage|prod
deleted: false
machineType: Standard_D2a_v4
nodeCountMin: 2
nodeCountMax: 3
clusterName: studio  # keep it as short as possible, do not inlude domain
logging: loki  # loki|filebeat
gpgKey: B93913AB8235F03C1C60A5368ED7AF1D24303834 # a fingerprint of key used to encrypt variables in platform-automation-tools repo
contactEmail: [email protected]
# Switch between letsencrypt stage (self-signed) or prod SSL certificates
# sslCertificate: stage | prod
dev_tldomain: builder.cafe
masterHighAvailability: false
  • When ever you need to update your cluster configuration, simply update the above file & push the code.

Next steps

  • After dev-cluster setup, develops team will provide you some set of files for your application.
  • You have to add these files in .deploy directory (or create .deploy directory) on your application.
  • If you have some environment specfic variables, add them in values-{env_name}.yml.
appExtConfig:
  SMTP_ADDRESS: "smtp.develmail.com"
  SMTP_PORT: "587"
  SMTP_USERNAME: "WE4CX23WXDSU33WDOAAEWAK6JU"
  • And if you have environment specific secret variables, then in that case your have to generate a file named as 'values-{env_name}-secrets.yaml'.
Generation of secrets.yaml file
cd app-deploy
gpg --import template-app-key-pub.gpg
  • Create the secrets yaml file in the app-deploy directory, using this command.
sops --pgp 4C80959A5A0DC7D784CEDE1E1B7F34C12F7B86BE values-{env_name}-secrets.yaml
  • If you don't have the fingerprint id, you can get it with
gpg --list-secret-keys
  • Edit the contents of the file to look like this
appExtConfig:
    secretStage: hello-world-stage-new

They key name must be appExtConfig.

  • Save the file and exit.
  • Verify the contents, it should look like this :
appExtConfig:
    secretStage: ENC[AES256_GCM,data:kfpvQqaezIWs/eb2Q4NAVb5HRW40,iv:x65hdmItrErv6EuAmsWlKdbRVGcYJ1quPF3ECCr7ctU=,tag:0UNrmM9X2WA75DGWenSFbw==,type:str]
sops:
    kms: []
    gcp_kms: []
    azure_kv: []
    lastmodified: '2020-08-25T22:05:10Z'
    mac: ENC[AES256_GCM,data:5db6CLC37zNIzv35OhTkyAr68bIil7+PCNUAyQAh16OqZoVeAxKZLdpe5LHvjmGRImMXPspjs5zyiwWSJhEaCBNSYxrAIJgJrYqpqWyr7jvD2aMxuvKI8VC2t1csnMqKI6Qrp33/5M3EeKnIqJ3vIReXYr8OAzhzmKMDWXmxdkQ=,iv:yX1pMA9XKRjgChckD6fUgkKByOATvpV5Pfy0v66gFpA=,tag:T0mCiZCjc4AqB9RAGDqEAw==,type:str]
    pgp:
    -   created_at: '2020-08-12T17:29:21Z'
        enc: |
            -----BEGIN PGP MESSAGE-----

            hQEMA1gSLZ947okQAQf/bYtv3ObIez55ET8rigjQLoKAdQ8qH4iRnEGs/7pliJ9r
            APuQHq1ggNQthVFkZSJqQmyRjW1r7CwkEOHd2aPiKM3dOGK2vdVBHES4LqWrR4cq
            Oa7VRkUNq1WE/4DZh6NDOSn3vBJnZ8OozDK5hrsl95X1aSNhWH9lKlbFDR8VqxWb
            +zz8h6RK7nEmzduc+l00ONbFGPx6y517QpkPJ0urb/N7XfVDtUN/er/2GGywR9a9
            Wu8KR0lymseyemxf1v9Qw2igbJFfZuQgEp2+83amD4Zc02WGNTGRz8kFJcn9MELV
            Gol91lPJNSzvlglVVwJP47kXPeot9I32FFR3qjwkPdJeAfElVY6zasr75yiOSDgK
            oR787IVYPZMMAr6Z+q7xqMpDrhRX3NEHDOCEATHPxLXVH/HkwRHtEtIfxth87MLg
            ye+HrTuHNSADKGKJRWh3fHuzAY1vfFeGGJFNxQIv7w==
            =B5sa
            -----END PGP MESSAGE-----
        fp: 4C80959A5A0DC7D784CEDE1E1B7F34C12F7B86BE
    unencrypted_suffix: _unencrypted
    version: 3.5.0
  • Add the file to your git repo, commit & push.
Modify existing secrets

If you have the private key, you can simply edit an existing encrypted file with sops:

sops values-{env_name}-secrets.yaml

If you don't have the private key you must delete the existing file and replace it with the new one you encrypted.

Last step

  • include .deploy > ci > {file_name}.yml these files in your .gitlab-ci.yml file.
include:
  - /.deploy/ci/.gitlab-ci-unit-tests.yml
  - /.deploy/ci/.gitlab-ci-deploy.yml
  - /.deploy/ci/.gitlab-ci-build.yml
  #include all files which you needed for your pipelines
  • Be sure your application logger logs to STDERR.
  • Now commit & push your changes, observe your gitlab pipelines for completion or failure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment