Best Practice of Dockerfile

 

  • write .dockerignore file
  • one container for one application
  • combine multi RUN commands into one
  • avoid using default latest tag
  • remove temp files after each RUN command
  • set WORKDIR and CMD
  • (optional) use ENTERPOINT
  • use COPY instead of ADD
  • adjust the order of COPY and RUN (e.g. copy package.json first and then run npm install then copy the other source code)
  • use health check (optional)

Auto Deployment with Docker Image (2)

Setup QA Deployment and Service

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: XXX
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app:XXX
    spec:
      containers:
      - image: mongo
        name: XXX
        ports:
        - name: mongo
          containerPort: 27017
          hostPort: 27017
        volumeMounts:
            - name: mongo-persistent-storage
              mountPath: /data/db
      volumes:
        - name: mongo-persistent-storage
          gcePersistentDisk:
            pdName: XXX
            fsType: ext4

#db-service.yml
apiVersion: v1
kind: Service
metadata:
  labels:
    name: XXX
  name: XXX
spec:
  ports:
    - port: 27017
      targetPort: 27017
  selector:
    name: XXX

# web-deployment.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: XXX-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: XXX
    spec:
      containers:
      - image: gcr.io/XXX/XXX
        name: XXX
        ports:
        - containerPort: 8080
          name: http-server
# web-service.yml
apiVersion: v1
kind: Service
metadata:
  name: XXX
  labels:
    name: XXXX
spec:
  type: LoadBalancer
  loadBalancerIP: XXX
  ports:
    - port: 80
      targetPort: 8080
      protocol: TCP
  selector:
    app: XXX

Setup the persistent storage

Tips: if the cluster only have one node and the performace is not good enough, there may be a issue about “failed to fit in any node fit failure on node “

Auto Deployment with Docker Image (1)

Branch-Oriented Deployment

Each new feature should a separate branch. Each branch should have its own image which can be deployed to the QA server for testing.

After fully testing, the branch should be merged with development and deployed to Dev Server

If everyone is satisfied with Dev Server version, then we deploy it to Staging Server.

Docker Image Repository

During the QA and Deployment process, docker image should be passed between different server in order to control the env difference

DB backup and replication

There should be an easy method to replicate the real data from Staging server and deploy it into the Dev and QA server.

Entire Workflow

Create a new branch

  • Create a new feature branch from development within bitbucket
  • The branch name should explain the current version, Jira task number, and date

Init Gcloud and Docker

  • Install the gcloud on your local
  • Update gcloud command line tools to the latest version
  • Install Docker on your local
  • Make sure the Docker is running

Check the Deployment Tool package for QA Deployment

/deploymentToolBox
 /QA
  /createAndPushImagesToContainerRepository.sh
  /Dockerfile
   

Execute the command

A Cluster has been created for QA and Staging

gcloud container clusters get-credentials sl-qa-staging-api-cluster --zone asia-east1-a --project stream-lending

An issue about ruby

In ruby symbol is different compared with string. You cannot directly use the string to call the attribute within on hash array.

The data type of hash array keys is symbol. Therefore, we need to convert string to the symbol to check if it’s existing in the array.

to_sym() public
Returns the Symbol corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name.

"Koala".intern         #=> :Koala
s = 'cat'.to_sym       #=> :cat
s == :cat              #=> true
s = '@cat'.to_sym      #=> :@cat
s == :@cat             #=> true
This can also be used to create symbols that cannot be represented using the :xxx notation.

'cat and dog'.to_sym   #=> :"cat and dog"