This repository contains the examples and exercises for the Edument Docker Workshop.
Ensure that Docker Desktop is installed on your system.
The examples include:
-
demo-dockerfile
A simple NodeJS application and associated Dockerfile for building a Docker image.
In the demo folder, run:
// build the image. docker build -t demo-dockerfile . // run a container (based on the built image). docker run -d -p 8080:8080 demo-dockerfile // test the server application running within the container. curl http://localhost:8080
-
demo-multistage
An example of how to introduce multiple build stages in a Dockerfile.
See the
Dockerfile
for how to run a specific stage. -
demo-multistage-java
An example of a multistage build for a Java application.
In order to create a production image that excludes intermediate build files (class files in the case of Java), the
Dockerfile
in this demo uses theCOPY --from=<stage>
flag to build the project and then select only the final JAR file for the production image.To create the test image (which also runs tests):
docker build -t javamultistage-test --target test .
To create the production image and run the application:
docker build -t javamultistage-prod --target production . docker run javamultistage-prod
-
demo-docker-compose
An example of how to "compose and run multiple application services" using
docker-compose
.In the demo folder, run:
// start up the multiservice stack. docker-compose up -d --build // stop the multiservice stack. docker-compose down
See each exercise's description for what it entails; see the solutions
folder for a step-by-step walkthrough on completing an exercise.
If you want to push an image to Docker Hub for public access, you'll first have to create a repository (with public visibility) that will store the pushed image.
This guide provides instructions for creating a repository; assuming Docker is installed on your machine, and you've built a Docker image named <My Docker ID>/<Image_Name>
and created a Docker Hub repository with the same name, run:
docker push <My Docker ID>/<Image_Name>
To automate the building and pushing of Docker images, you may set up a pipeline that executes these steps upon pushing changes to a source code repository, e.g. on Github.
Follow these instructions to set up such a pipeline using Github Actions and Docker Hub:
-
In Docker Hub, create a repository for your image (see the previous section "Docker Hub Repositories").
-
Go to your account and navigate to Account Settings -> Security and click New Access Token; enter a name for your access token (e.g. github-pipeline) and click "Generate". Save the generated token for later use.
-
Create an (empty) Github repository to host your application source code.
You may use the
demo-dockerfile
example as a simple application for this purpose,Copy that folder's contents to a separare folder on your machine, and in that folder run:
git init . git commit -a -m "Initial commit" // make sure you've added the corresponding Github repository as a remote. git push -u origin master
-
Once you've pushed the application source code to the Github repository, create the following secrets by navigating to Settings -> Secrets and clicking "New repository secret":
Name: DOCKER_HUB_USERNAME Value: <Your Docker ID> Name: DOCKER_HUB_ACCESS_TOKEN Value: <Your Generated Access Token>
-
Set up a workflow via Actions -> New workflow and selecting the suggested "Docker image" workflow.
-
Replace the default workflow content with the following:
name: Docker Image CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- uses: actions/checkout@v2
- name: Build the Docker image
run: docker build . --file Dockerfile --tag ${{ secrets.DOCKER_HUB_USERNAME }}/myimage:latest
- name: Publish the Docker image
run: docker push ${{ secrets.DOCKER_HUB_USERNAME }}/myimage
__Note__: Replace `myimage` in the workflow above with the name you've chosen for your particular image.
Finally, click Start commit -> Commit new file to create the `.github/workflow/docker-image.yml` file in the Github repository.
- Make a change to the application code and push it to the Github repository; this will trigger a build of the Docker image. After a moment, your Docker Hub repository should have been updated with the new image.
See the demo-gitlab
folder for an example of a multistage Gitlab pipeline (for a NodeJS application).
Implementing container based applications typically involves the complex task of managing multiple containers; Kubernetes is a container orchestration platform that you can utilize for deploying and running your application.
The demo-docker-k8
folder contains K8 YAML files for deploying a simple application and attaching a so-called LoadBalancer service to allow for incoming traffic.
If you e.g. have dockerized the demo-dockerfile
and pushed the image (named myapp
by default in the K8 files, change it to match the name of your image) to Docker Hub, you can launch containers based on that image in a Kubernetes cluster.
Setting up a cluster is beyond the scope of the Docker workshop, but see the Links section below for a tutorial on Kubernetes and how to work with K8 YAML files.
Deploy a docker-compose application to AWS ECS
Introduction to Kubernetes (by Marc Klefter)
Marc Klefter | [email protected]