Giter Club home page Giter Club logo

docker-image-with-github-actions's Introduction

Docker-Image-with-GitHub-Actions

This repo provides a guide ๐Ÿ“— to creating a simple pipeline to build โš™ and upload docker ๐Ÿณ image artifacts to the docker hub ๐Ÿณ repo with the help of GitHub actions as changes take place in GitHub main branch.

Prerequisites

  1. Docker ๐Ÿณ basics.

  2. Yaml ๐Ÿ“„ basics

Note

If you are viewing this page from GitHub pages then some code for GitHub secrets will not be shown, you will only see a $ sign. Check out GitHub repo from ๐Ÿ‘‰Here for the correct code.

Scanario

For this demo, the application is to be dockerized ๐Ÿณ in a static site. It is a simple game of guessing ๐Ÿค” numbers between 0 0๏ธโƒฃ to 9 9๏ธโƒฃ. You access this page Here

Screenshot

Repository structure

/src
  index.html
  index.css
/Dockerfile
/README.md

Let's take a look ๐Ÿ‘€ at source files ๐Ÿ—ƒ

  1. /src - This folder contains source files ๐Ÿ“ for our static site.
  2. Dockerfile - This is a basic Dockerfile ๐Ÿณ which specifies how the image should be built from the source. We will take a look ๐Ÿ‘€ at this in a moment.
  3. README.md - This is the file ๐Ÿ“„ you currently reading.

Dockerfile

Dockerfile ๐Ÿณ provides a set of instuctions to build โš™ the image. For this repo it looks ๐Ÿ‘€ like the following.

 FROM nginx
 ADD src/* /usr/share/nginx/html/
 EXPOSE 80
 CMD ["nginx","-g","daemon off;"]

Let's take a look ๐Ÿ‘€ at each instruction to understand what these instructions mean

  1. FROM nginx - This line tells docker ๐Ÿณ to use Nginx ๐Ÿ†– as the official image as a base image for our new image.
  2. ADD src/* /usr/share/nginx/html/ - This line copies files ๐Ÿ—ƒ of src folder inside the /usr/share/nginx/html/ folder of image. /usr/share/nginx/html/ folder is used by the nginx ๐Ÿ†– server as entry point of website.
  3. EXPOSE 80 - This line tells docker ๐Ÿณ to open port 80 inside the docker ๐Ÿณ container so that we can communicate with the nginx ๐Ÿ†– server inside.
  4. CMD ["nginx","-g","daemon off;"] - This last cline starts Nginx ๐Ÿ†– server and runs โ–ถ every time the docker ๐Ÿณ container start.

It's all about what we need to get started. Let's take a look ๐Ÿ‘€ how to setup GitHub actions to create a pipeline.

Steps to create ๐Ÿ›  a pipeline

Create github workflow

  1. Open github repo and click on actions option.
  2. Search ๐Ÿ” for Simple workflow and select the most suitable from the option that appeared.
  3. Rename the file ๐Ÿ“„ with the name you want for your workflow.

Writing Code ๐Ÿ‘จโ€๐Ÿ’ป for workflow

  1. Replace ๐Ÿ” the code ๐Ÿ‘ฉโ€๐Ÿ’ป of workflow with the following.

    name:
    on:
    jobs:
    

    Let's understand ๐Ÿค” what this code ๐Ÿ‘ฉโ€๐Ÿ’ป means

    • The lines starting with # represents comments in YAML.
    • name: - Defines the name for your workflow.
    • on: - When we create a GitHub action, we want the action to run โ–ถ on certain events i.e push to a branch. This on parameter describes the events in which the action is going to run โ–ถ.
    • jobs: - jobs work section is the place where we define what going to happen when events specified in the on: section occurs.
  2. Give a name to your workflow

    name: Docker image CI
  3. Specify the event of our interest in this case push event

    • Add event

      name: Docker image CI
      on:
        push:
    • Add branches for which we going to perform some action

      name: Docker image CI
      on:
        push:
          branches: [ "main" ]
  4. Now the remaining part of the workflow is jobs section. Jobs define the set of actions (Tasks) to be performed. These action can run โ–ถ sequentially or in parallel. for this demo we are just going to create one job named docker-build.

    name: Docker image CI
    on:
       push:
         branches: [ "main" ]
    jobs:
       docker-build:
  5. Each job section has following things

    • runs-on: - Tells ๐Ÿ“ข the platform on which this job(Task) is going to run โ–ถ on. For this demo, we are taking 'ubuntu-latest'.

    • steps: - This defines a set of steps to be performed to get the job done.

    • Now our code ๐Ÿ‘ฉโ€๐Ÿ’ป will look ๐Ÿ‘€ like this.

       name: Docker image CI
       on:
          push:
            branches: [ "main" ]
       jobs:
          docker-build:
               runs-on: ubuntu-latest
               steps: 

    Each step in steps walk section has the following parts

    • name: - name of the step
    • run: - set of command ๐Ÿ’ป to run โ–ถ
    • uses: - rerefence of any other action which is used by the current steps. The referenced action will run โ–ถ first.optional
    • Apart from these, there are many other optional parts.
  6. Add the following code ๐Ÿ‘จโ€๐Ÿ’ป in the steps section

    - uses: actions/checkout@v3
    - name: Build the Docker image
      run: |
    • notice | after run: this allows us to write multiple commands ๐Ÿ’ป.
  7. To build โš™ image add the following command ๐Ÿ’ป to the Build the Docker image step and replace <image_name> With the name of your choice.

     docker build . --file Dockerfile --tag <image_name>
    • Now our file ๐Ÿ“„ look ๐Ÿ‘€ something like this

      name: Docker image CI
      on:
        push:
          branches: [ "main" ]
      jobs:
          docker-build:
             runs-on: ubuntu-latest
               steps:
                 - uses: actions/checkout@v3
                 - name: Build the Docker image
                   run: |
                      docker build . --file Dockerfile --tag hackthenumber
    • Notice ๐Ÿ“ the uses part specifies actions/checkout@v3. This action checks if our repository is present and if we have access to it.

    • Command ๐Ÿ’ป docker build . --file Dockerfile --tag hackthenumber builds โš™ an image named hackthenumber it is currently stored at the local system at which the job is running.

  8. Login ๐Ÿ” to dockerhub ๐Ÿณ

    To make push the image to the docker hub ๐Ÿณ we need to login to the docker hub ๐Ÿณ. Docker recommends creating access tokens for logins ๐Ÿ” instead of using password ๐Ÿ”‘.

    • Creating dockerhub ๐Ÿณ Access token ๐Ÿ—

      • To create an access token login ๐Ÿ” to docker hub ๐Ÿณ. Goto access Account Setting > Security > New Access token.

        acc

        security

      • Enter description for the token and click generate โš™.

      • Select Copy and Close.

    • Adding passwords ๐Ÿ”‘ directly to workflow files ๐Ÿ“„ can be a potential threat. To make this secure GitHub provides secrets to store passwords ๐Ÿ”‘ etc. To add the secrets to GitHub repo do the following.

      • Goto your repo.
      • Open Settings ๐Ÿ”ง.
      • Select Secret > Actions > New Repository Secret
      • Add DOCKERHUB as name and your docker hub username as secret.
      • Add another secret for TOKEN as the name and paste the access token you generated โš™ in the previous step.
    • Login ๐Ÿ” to Dockerhub ๐Ÿณ

      • Add the following lines to run part to login ๐Ÿ”

        docker login -u ${{secrets.USERNAME}} -p ${{secrets.TOKEN}}
  9. Tag image to refer to Dockerhub ๐Ÿณ repo

    • To tag image add the following command ๐Ÿ’ป by replacing <image_name> with the image name you have choosen for your image

      docker tag <image_name> ${{secrets.USERNAME}}/<image_name> 
    • With our choosen name it will look ๐Ÿ‘€ like something this

      docker tag hackthenumber ${{secrets.USERNAME}}/hackthenumber
  10. Add the following line to push image to dockerhub ๐Ÿณ

    docker push ${{secrets.USERNAME}}/hackthenumber

Now our file ๐Ÿ“„ will look ๐Ÿ‘€ like something this

name: Docker image CI
on:
  push:
    branches: [ "main" ]
jobs:
    docker-build:
        runs-on: ubuntu-latest
           steps:
              - uses: actions/checkout@v3
              - name: Build the Docker image
                run: |
                   docker build . --file Dockerfile --tag hackthenumber
                   docker login -u ${{secrets.USERNAME}} -p ${{secrets.TOKEN}}
                   docker tag hackthenumber ${{secrets.USERNAME}}/hackthenumber
                   docker push ${{secrets.USERNAME}}/hackthenumber

Similarly, you can add code to test if everything is working fine in the same script. After adding the testing code, our final code will look like this.

name: Docker image CI
on:
  push:
    branches: [ "main" ]
jobs:
  docker-build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - name: Build the Docker image
            run: |
                  docker build . --file Dockerfile --tag hackthenumber
                  docker login -u ${{secrets.USERNAME}} -p ${{secrets.TOKEN}}
                  docker tag hackthenumber ${{secrets.USERNAME}}/hackthenumber
                  
  docker-test:
        runs-on: ubuntu-latest
        needs: docker-build
        steps:
          - uses: actions/checkout@v3
          - name: Test image presence
            run: |
              docker image inspect ${{secrets.USERNAME}}/hackthenumber
              if [[ $? -eq 1 ]]; then
                  echo "โŒ Image not found!"
                  exit 1  # This will cause the job to fail
              else
                  echo "โœ… Image Build Successfully!"
              fi
          - name: Test image response
            run: |
                  docker run -itdp 8000:80 --rm ${{secrets.USERNAME}}/hackthenumber
                  response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8000)
                  if [[ $response -ne 200 ]]; then
                    echo "โŒ Image response is not 200!"
                    exit 1
                  else
                    echo "โœ… Image response is 200!"
                  fi
  docker-deploy:
        runs-on: ubuntu-latest
        needs: docker-test
        steps:
            - uses: actions/checkout@v3
            - name: Deploy to docker hub
              run: |
                docker push ${{secrets.USERNAME}}/hackthenumber && echo "โœ… Deployed to Docker Hub" || echo "โŒ Deployment to Docker Hub failed"

Quick Test deployed image

  • Go to https://labs.play-with-docker.com and login ๐Ÿ” with dockerhub ๐Ÿณ credentials.

  • Click start and then Add New Instance.

    image

    Add instance

  • Run โš™ the command ๐Ÿ’ป docker run -dp 8080:80 <username>/<image_name> by replacing <username> with the dockerhub username and <image_name> with the image name used in the GitHub action. For example

    docker run -dp 8080:80 sandeepsource/hackthenumber
  • Click the port number 8080 on the right side of the open port button. If the button for port 8080 not appeared then click the open port button and enter 8080 and click ok.

    open port

  • Now if everything is gone well ๐Ÿ™Œ you will able to see following output.

    output

docker-image-with-github-actions's People

Contributors

sandeep-source avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.