Your First Dockerfile(SCA-cloud test)

Few weeks ago, She Code Africa called for applications for it’s cloud internship. One of the technical assessment 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 Docker environment. Our preferred language is JavaScript and we will be running a Nodejs application from a docker container.

Okay let’s start.

  • A GitHub account http://github.com/
  • An account with Docker hub at docker.com
  • Download and set up your the Docker desktop.
  • Nodejs Command line tool installed.
  • Create a repository named SCA Cloud School Application on your GitHub account . Remember to add a readme file.
  • Clone the repository to your terminal. You are currently on the master(main) branch.

Switch to a branch stable git checkout -b stable and create a folder called docker.

  • Run npm init (App 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”
}

Your file structure

docker
| — — node_modules
| — — views
| | — — index.html
| — — .dockerignore
|— — .gitignore
| — App.js
| — — Dockerfile
|— — package-lock.json
|— — package.json

App.js

index.html (inside views folder.)

  • On the terminal Runnpm start. you should have your server running on port 8080. Go the http://localhost:8080 to view the message Welcome to SCA Cloud School Application

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 build. .dockerignore is similar to .gitignore.

Run docker build -t dockerdemo where dockerdemo is the name of your image. Run docker run -p 4990:8080 -d dockerdemo

  • Go to your docker desktop to view and to run the docker image .
  • Your docker image should be running on http://localhost:49160 . To view images in your container Run docker images
  • Remember, you are on the stable branch.
  • Rungit add .and git commit -m "message" and git push to push the docker folder to the SCA-GitHub repository created.
  • Create a repo in docker hub named sca-cloud-docker.
  • Run docker tag image-id yourhubusername/reponame:tag — example: docker tag 9c9f09c6e82e yourhubusername/sca-cloud-docker:stable
  • Run docker push yourhubusername/reponame:tag — example: docker push yourhubusername/sca-cloud-docker:stable
  • 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, confirm on http://localhost:4990
  • Add ,commit and push feature branch to GitHub.
  • Push to the same docker repository docker tag image-id:yourhubusername/reponame:tag , docker push yourhubusername/reponame:tag n/b: we use feature as tag.

Other things to note based on instruction:

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

That is it…✌ You can view the repo here

I hope this really helped someone. I appreciate comments and questions where you don’t understand. BYE!

Software developer. Imaginative. I love daring and exploring. The confusion in startups is thrilling and i love it.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store