Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker Workflow #2

Merged
merged 2 commits into from
May 6, 2021
Merged

Docker Workflow #2

merged 2 commits into from
May 6, 2021

Conversation

github-learning-lab[bot]
Copy link
Contributor

Docker 🐳

What is Docker?

Docker is an engine that allows you to run containers. Containers have many advantages, including:

  • Code and dependancies are packaged together, so software runs more reliably in different environments
  • Containers are a standard unit of software
  • As standalone executable packages, containers include everything needed to run the application
  • Containers are lightweight in comparison to virtual machines

Docker vs Virtual Machines

visualization comparing containers to virtual machines

Container Virtual Machine
Application layer abstraction Physical layer abstraction
Consume less space than VMs Include a full copy of the operating system
Fast to startup Boot up slowly
Shared OS kernel allows many containers to run on a single host Multiple virtual machines run on one server at a greater resource cost per guest machine

Dockerfiles, Images, and Container

Before moving forward with the workflow file, let's spend some time on these concepts. There are important differences between Dockerfiles, Images, and Containers.

Dockerfile Docker Image Docker Container
Text document that contains all the commands and instructions necessary to build a Docker Image. Executable packages comprised of code, dependancies, libraries, a runtime, environment variables, and configuration files. Very similar to a virtual machine snapshot. A runtime instance of a Docker Image. This is what the image becomes when executed in memory.

What about our workflow?

Our repository contains a Dockerfile, source code, and tests for the Tic Tac Toe application.

Our CI Workflow allows us to make code changes. Those changes will trigger an automated build and automated test. But, the automation does not create a deployable artifact.

We will place our application into a Docker container. Then, we will have a deployable package. A deployable package enables CD.

Because a Dockerfile is a text file, we are able to version it as source code. This configuration as code allowing us a single point of truth for our application.

As you learned above, we need to turn that Dockerfile into a Docker image if we want to create a runtime instance. We are going to store that image in GitHub Packages.


📖Read More about Docker.

@github-learning-lab
Copy link
Contributor Author

Edit the workflow file

Step 2: Edit the workflow file

We are going to edit the current workflow file in our repository by adding a job that turns our Dockerfile into a Docker Image.

⌨️ Activity: Edit the workflow file and turn the Dockerfile into a Docker Image

  1. Edit the current workflow file in our repository
  2. Rename ci-workflow.yml to cd-workflow.yml:
  3. On line 1, change the name from Node CI to Docker CD
    yaml name: Docker CD
  4. Add the following job to your workflow file:
  Build-and-Push-Docker-Image:
    runs-on: ubuntu-latest
    needs: test
    name: Docker Build, Tag, Push

    steps:
    - name: Checkout
      uses: actions/checkout@v1
    - name: Download built artifact
      uses: actions/download-artifact@main
      with:
        name: webpack artifacts
        path: public
    - name: Build container image
      uses: docker/build-push-action@v1
      with:
        username: ${{github.actor}}
        password: ${{secrets.GITHUB_TOKEN}}
        registry: docker.pkg.github.com
        repository: klinki/github-actions-for-packages/tic-tac-toe
        tag_with_sha: true
  1. Commit the newly edited workflow file to your repository
Complete cd-workflow.yml file...
name: Docker CD

on:
  push:
    paths:
    - "**Dockerfile**"

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: npm install and build webpack
      run: |
        npm install
        npm run build
    - uses: actions/upload-artifact@main
      with:
        name: webpack artifacts
        path: public/

  test:
    runs-on: ubuntu-latest
    needs: build
    strategy:
      matrix:
        os: [ubuntu-lastest, windows-2016]
        node-version: [12.x, 14.x]

    steps:
    - uses: actions/checkout@v1
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - uses: actions/download-artifact@main
      with:
        name: webpack artifacts
        path: public
    - name: npm install, and test
      run: |
        npm install
        npm test
      env:
        CI: true

  Build-and-Push-Docker-Image:
    runs-on: ubuntu-latest
    needs: test
    name: Docker Build, Tag, Push

    steps:
    - name: Checkout
      uses: actions/checkout@v1
    - name: Download built artifact
      uses: actions/download-artifact@main
      with:
        name: webpack artifacts
        path: public
    - name: Build container image
      uses: docker/build-push-action@v1
      with:
        username: ${{github.actor}}
        password: ${{secrets.GITHUB_TOKEN}}
        registry: docker.pkg.github.com
        repository: klinki/github-actions-for-packages/tic-tac-toe
        tag_with_sha: true

@github-learning-lab github-learning-lab bot mentioned this pull request May 6, 2021
@github-learning-lab github-learning-lab bot merged commit 861a081 into main May 6, 2021
@github-learning-lab github-learning-lab bot deleted the docker-workflow branch May 6, 2021 12:41
@github-learning-lab
Copy link
Contributor Author

Great Job 👍

I have merged this pull request for you, and opened a new one for you to start working on the CD segment of our workflow.

Navigate to the next pull request to continue this course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant