Docker Compose

1. Overview

This section describes the use of docker deployment

The docker deploys us to use the harbor mirror repository. Once the local code is written, build the mirror to be uploaded to harbor.

2. Project code

This section code is consistent with a section code deployed by the physical machine, but needs to use goctl to generate a dockerfile step

Execute the following command to generate Dockerfile at the root of the apicode project

  1. $ goctl docker -go apicode.go

2.1 Upload code

Push the code to the gitlab

2.2 Public key for configuring jenkins server

jenkins needs a gitlab pull code to build, so we want to configure the public key of the jenkins physical machine to gitlab and unencrypt login

3. Mirror Repository

Go to harbor to create this project’s mirror repository

View the push command

  1. $ docker push 192.168.1.180:8077/apicode/REPOSITORY[:TAG]

4. Jenkins deployment

We have already deployed jenkins with gitlab and then we use jenkins to publish the code as long as you write pipline

4.1 Create pipline

Click “New Item” on the left side of the front page, enter “apicode-docker”, select “Waterlines”, then determine

Then click on “General”, select “This project is parameterized”, “Add parameter”, “Choice Parameter”, like the beacon

Then write the following

Save directly.

4.2 Edit pipline

‘Note’ We have a public key to configure before writing pipline to configure jenkins public key to the server running the service because we build using jenkins and then upload the built tar package to the running server using scp.

View jenkins host public key:

  1. $ cat /root/.ssh/id_rsa.pub

Configure to the /root/.ssh/authorized_keys running the service physics.

Swipe down to find Pipeline scripts, fill in script content

  1. pipeline {
  2. agent any
  3. parameters {
  4. gitParameter name: 'branch',
  5. type: 'PT_BRANCH',
  6. branchFilter: 'origin/(.*)',
  7. defaultValue: 'master',
  8. selectedValue: 'DEFAULT',
  9. sortMode: 'ASCENDING_SMART',
  10. description: '选择需要构建的分支'
  11. }
  12. stages {
  13. stage('服务信息') {
  14. steps {
  15. sh 'echo 分支:$branch'
  16. }
  17. }
  18. stage('拉取代码') {
  19. steps {
  20. checkout([$class: 'GitSCM',
  21. branches: [[name: '$branch']],
  22. doGenerateSubmoduleConfigurations: false,
  23. extensions: [],
  24. submoduleCfg: [],
  25. userRemoteConfigs: [[credentialsId: 'gitlab-cert', url: 'ssh://git@192.168.1.182:2222/root/apicode.git']]])
  26. }
  27. }
  28. stage('获取commit_id') {
  29. steps {
  30. echo '获取commit_id'
  31. git credentialsId: 'gitlab-cert', url: 'ssh://git@192.168.1.182:2222/root/apicode.git'
  32. script {
  33. env.commit_id = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
  34. }
  35. }
  36. }
  37. stage('Dockerfile Build') {
  38. steps{
  39. script{
  40. env.image = sh(returnStdout: true, script: 'echo ${JOB_NAME}-${type}:${commit_id}').trim()
  41. env.jobname = sh(returnStdout: true, script: 'echo ${JOB_NAME}-${type}').trim()
  42. }
  43. sh 'echo 镜像名称:${image} && docker build -t ${image} .'
  44. }
  45. }
  46. stage('上传到镜像仓库') {
  47. steps{
  48. //docker login 这里要注意,会把账号密码输出到jenkins页面,可以通过port.sh类似方式处理,官网文档有这里我就不详细写了
  49. sh 'docker login --username=${docker_username} --password=${docker_pwd} http://${docker_repo}'
  50. sh 'docker tag ${image} ${docker_repo}/apicode/${image}'
  51. sh 'docker push ${docker_repo}/apicode/${image}'
  52. }
  53. }
  54. stage('Deploy') {
  55. steps{
  56. sh 'ssh root@192.168.1.183 docker login --username=${docker_username} --password=${docker_pwd} http://${docker_repo}'
  57. sh 'ssh root@192.168.1.183 docker pull ${docker_repo}/apicode/${image}'
  58. // 当然这里端口号可以选择放到配置哪里都可以
  59. sh 'ssh root@192.168.1.183 docker run -d -p 8889:8889 ${docker_repo}/apicode/${image}'
  60. }
  61. }
  62. }
  63. }

5. Build Publication

Click on the home page to find the apicode service to click on it

Tap Build with Parameters, select the corresponding “branch” to “Service” to start building

Build finished, we go to http://192.168.1.183:8889/hello?msg=mikael to see output on page

  1. {
  2. "msg": "hello->mikael"
  3. }

As of then, deployment has been completed.Of course you can forward your favorite gateway to this service like nginx, kong…