Create a Dockerfile(SCA-cloud internship test).
A few weeks ago, She Code Africa called for applications for its cloud internship. 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
- A GitHub account http://github.com/
- An account at Docker hub at docker.com
- Download and set up your Docker desktop.
- Nodejs Command line tool installed.
Step 1: Create a GitHub repository
- Create a 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
( name: docker-sample, Entry point: app.js) - Run
npm install express
Note: Replace script object
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1"
},
with
“scripts”: {
“start”: “node app.js”
}
Project Structure
App.js
In the app.js file, we set up a Nodejs server using Express.
index.html
The index.html file is in the views folder.
- On the terminal , run
npm 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 4990:8080 -d dockerdemo
- Go to your docker desktop to view and run the docker image.
- Your docker image should be running on http://localhost:49160
To view images in your container, Rundocker images
on your terminal.
Step 5: Push to GitHub
- Remember, you are on the stable branch.
- Run
git add .
andgit commit -m "commit-message"
andgit 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 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
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, confirm at http://localhost:4990
- Add, commit and push the 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 the feature as a tag.
Other things to note 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.
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!