Clone and run the sample application

Step 1: Get the sample app and clone the Repo

A template GitHub Repo is provided for this workshop using Snyk's Goof application.
GitHub - snyk-partners/docker-academy
GitHub
Click "Use this Template" to copy the Repo into your GitHub account.
To properly copy-paste the commands in these instructions name your repo docker-academy. Otherwise, ensure the command uses the correct repo name.
If you want to copy-paste commands, set an environment variable for your GitHub ID.
1
# Set an environment variable for your GitHub ID
2
GithubId=<<your_github_id>>
Copied!
Now, clone the Repo to your local environment, then cd into it.
1
# Clone the Repo and cd
2
git clone https://www.github.com/$GithubId/docker-academy && cd docker-academy
Copied!

Step 2: Test the Application locally

Install Dependencies

To run our application locally, we need to install its dependencies first.
1
# Install npm dependencies and generate lockfile
2
npm install --package-lock
Copied!

Start the Mongo database

The app requires MongoDB to work. Instead of installing Mongo, we'll run it in a Docker container.
1
# Start detached mongo with container port 27017 mapped to host port 27017
2
docker run -d -p 27017:27017 mongo
Copied!
You can verify that mongo is running by running docker ps.

Start the Application

Now you can run the application.
1
node app.js
Copied!
Once it starts, it will be available at http://localhost:3001. Verify it works by adding a few items into the todo list.
Success! Now that we know it works, let's package it in a container for distribution.

Build the Docker Image

If you want to copy-paste commands, set an environment variable for your Docker ID.
1
# Set an environment variable for your GitHub ID
2
DockerId=<<your_docker_id>>
Copied!
The Dockerfile in the repo tells docker build how to build the container. To learn more about Dockerfile commands, visit Docker's documentation. A summary is provided below:
Dockerfile
1
FROM node:10.4 ## Use Node 10.4 as our base image
2
3
RUN mkdir /usr/src/goof ## Create the directory for our application
4
RUN mkdir /tmp/extracted_files
5
COPY . /usr/src/goof ## Copy the current directory files into the image
6
WORKDIR /usr/src/goof ## Set the working directory
7
8
RUN npm update ## Ensure npm is up to date
9
RUN npm install ## Install dependencies
10
EXPOSE 3001 ## Expose the port
11
EXPOSE 9229
12
ENTRYPOINT ["npm", "start"] ## The command
Copied!
When ready, build and tag the container image to get it ready to push into Docker Hub.
1
# Build the container and tag it dev
2
docker build -t $DockerId/goof:dev .
Copied!
When the build finishes, push the container to Docker Hub.
1
# Push to Docker Hub
2
docker push $DockerId/goof:dev
Copied!

Deploy the application to Kubernetes

To ensure the container deploys correctly into Kubernetes without incurring cloud costs, we use the Kubernetes cluster shipped with Docker Desktop. Before deploying the app, you'll need to make a change to the app's deployment manifest. Open the file goof-deployment.yaml in a text editor.
Find these lines, and insert your Docker ID where indicated.
1
spec:
2
containers:
3
- image: <<DOCKERID>>/goof:dev #Edit with your Docker Hub ID
4
name: goof
5
imagePullPolicy: Always
Copied!
Save your changes. Now you're ready to deploy the application. Run the following commands:
1
# Create a namespace
2
kubectl create ns snyk-docker
3
4
# Set the current context to use the new namespace
5
kubectl config set-context --current --namespace snyk-docker
6
7
# Spin up the goof deployment and service
8
kubectl create -f goof-deployment.yaml,goof-mongo-deployment.yaml
Copied!
To check the status of the pods as the application comes up, use the following command:
1
kubectl get pods
Copied!
Once both are running, the application should now be accessible in http://localhost:3001. Success!
Now that we know our application works when deployed into Kubernetes, we'll set up a continuous integration and continuous delivery pipeline to re-build our container as code changes are made.
Last modified 2d ago