Create a Dockerfile(SCA-cloud internship test).
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
- A GitHub account http://github.com/
- An account at Docker hub at docker.com
- Download and set up your Docker desktop.
- Stable version of Nodejs installed.
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
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.html
file
Project Structure
- In the App.js file, set up a Nodejs server using Express.
- Add to the the index.html file 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. Rundocker 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.
- 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 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!