Create a Dockerfile(SCA-cloud internship test).

Sandy Goodnews
4 min readJan 30, 2021

--

A few weeks ago, The She Code Africa organization called for applications for its cloud internship, and one of the technical assessments was on Docker containers. You can look up the assessment here. Was the test too challenging for you? well, it’s not too late to learn now.

Basically, the whole idea is to run an application from a Docker container. Just like running an app on localhost, but this time it’s on the Docker environment. Our preferred language is JavaScript and we will be running a Nodejs application from a docker container.

Okay, let’s start.

Prerequisites

Step 1: Create a GitHub repository

  • Create a GitHub repository named SCA Cloud School Application on your GitHub account. Remember to add a README.md file.
  • Clone the repository using your terminal. You are currently on the master or main branch.

Step 2: Set up Nodejs Application.

Switch to a branch named stable git checkout -b stable and create a folder named docker in your project folder.

  • Run npm init and follow the prompts ( name: docker-sample, Entry point: app.js)
  • Run npm install express
This is what your package.json file should look like.

Note: Replace script object

“scripts”: { 
“test”: “echo \”Error: no test specified\” && exit 1"
},

with

“scripts”: {
“start”: “node app.js”
}
  • Create a views folder at the root folder, and add an index.htmlfile

Project Structure

Project Structure
  • In the App.js file, set up a Nodejs server using Express.
App.js file
  • Add to the the index.html file in the views folder.
index.html
  • On the terminal , runnpm start you should have your server running on port 8080. Go the http://localhost:8080 on your browser to view the message Welcome to SCA Cloud School Application

Step 3: Set up the Dockerfile.

  • Add these codes to your Dockerfile
  • Add
    node_modules
    .dockerignore
    .gitignore
    npm-debug.log
    Dockerfile
    to the .dockerignore file, to prevent the files from being copied to the docker image during the build. The .dockerignore is similar to .gitignore

Step 4: Build a Docker image

  • Run docker build -t dockerdemo .where dockerdemo is the name of your image. Run docker run -p 49160:8080 -d dockerdemo (4990 is a random new port number, you could use any number)
  • Go to your docker desktop to view and run the docker image.
  • Your docker image should be running on http://localhost:49160
  • To view all images in your container, Run docker images on your terminal.

Step 5: Push to GitHub

  • Remember, you are on the stable branch.
  • Rungit add .and git commit -m "commit-message" and git push to push the docker folder to the SCA-GitHub repository.

Step 6: Push to DockerHub

  • Create a repository in Docker Hub named sca-cloud-docker.
  • Run docker tag dockerdemo <<dockerhub-username>>/sca-cloud-docker:stable
  • Run docker push <<dockerhub-username>>/sca-cloud-docker:stable

Step 7: Feature Branch (similar to stable branch)

  • Check out to feature branch git checkout -b feature
  • Change the content of the index.html file to “Welcome to SCA Cloud School Application, this is my first assessment”
  • Build and run a docker image docker build -t docker-feature ,docker run -p 4990:8080 -d docker-feature
    N/b: we use a different port number
  • Run your generated image on the docker desktop and confirm your application is working on http://localhost:4990
  • Add, commit and push the feature branch to GitHub.
  • Push to the same docker repository
    docker tag docker-feature <<dockerhub-username>>/sca-cloud-docker:feature
    docker push <<dockerhub-username>>/sca-cloud-docker:feature
    N/b: we use the feature as a tag.

Other things based on instruction:

  • Merge the feature branch to stable and do not delete the feature branch.
  • The master branch should have only a README.md file with documentation.

And that is it…✌ You can view the Repository here

I hope this really helped someone. I appreciate your comments and questions if you get stuck. BYE!

--

--

Sandy Goodnews
Sandy Goodnews

Written by Sandy Goodnews

Software developer. Imaginative. I love to try, I write to relearn and reinforce while sharing the knowledge.

No responses yet