A template GitHub Repo is provided for this workshop using Snyk's Goof application.
Click "Use this Template" to copy the Repo into your GitHub account.
If you want to copy-paste commands, set an environment variable for your GitHub ID.
# Set an environment variable for your GitHub IDGithubId=<<your_github_id>>
Now, clone the Repo to your local environment, then
cd into it.
# Clone the Repo and cdgit clone https://www.github.com/$GithubId/docker-academy && cd docker-academy
To run our application locally, we need to install its dependencies first.
# Install npm dependencies and generate lockfilenpm install --package-lock
The app requires MongoDB to work. Instead of installing Mongo, we'll run it in a Docker container.
# Start detached mongo with container port 27017 mapped to host port 27017docker run -d -p 27017:27017 mongo
You can verify that mongo is running by running
Now you can run the application.
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.
If you want to copy-paste commands, set an environment variable for your Docker ID.
# Set an environment variable for your GitHub IDDockerId=<<your_docker_id>>
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:
DockerfileFROM node:10.4 ## Use Node 10.4 as our base imageRUN mkdir /usr/src/goof ## Create the directory for our applicationRUN mkdir /tmp/extracted_filesCOPY . /usr/src/goof ## Copy the current directory files into the imageWORKDIR /usr/src/goof ## Set the working directoryRUN npm update ## Ensure npm is up to dateRUN npm install ## Install dependenciesEXPOSE 3001 ## Expose the portEXPOSE 9229ENTRYPOINT ["npm", "start"] ## The command
When ready, build and tag the container image to get it ready to push into Docker Hub.
# Build the container and tag it devdocker build -t $DockerId/goof:dev .
When the build finishes, push the container to Docker Hub.
# Push to Docker Hubdocker push $DockerId/goof:dev
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.
spec:containers:- image: <<DOCKERID>>/goof:dev #Edit with your Docker Hub IDname: goofimagePullPolicy: Always
Save your changes. Now you're ready to deploy the application. Run the following commands:
# Create a namespacekubectl create ns snyk-docker# Set the current context to use the new namespacekubectl config set-context --current --namespace snyk-docker# Spin up the goof deployment and servicekubectl create -f goof-deployment.yaml,goof-mongo-deployment.yaml
To check the status of the pods as the application comes up, use the following command:
kubectl get pods
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.