Nerd For Tech
Published in

Nerd For Tech

Docker — “But it works on my machine…” Then we’ll ship your machine!

I can enable you to take your computer to all your customers houses and run your application for them on it.

Github link to the application code: https://github.com/adisingh007/works-on-my-machine

Docker meme

Won’t eat much of your time. Just 5 mins.

  1. A quick nodejs application with 1 rest endpoint.
  2. 1 e2e test to verify the response.

Performing the above 2 steps is further expanded into 15 steps below. This would involve configuring dependencies, running tests, and then starting the application.

We do not want our customers to go through those painful steps. We just want them to download the application and straight away run it. So we will dockerize the application later and enable our customers to run it on their machines as if it was you running it on your machine.

Painful stuff:

0. Let’s quickly create a project directory named works-on-my-machine

Created the project directory

Lets take some pain for our customers and build an application, download dependencies, write a test, run the test, start the app and play with the app:

  1. npm init
Just fill in the details… Regular government stuff…

2. npm i express

3. npm i --save-dev supertest jest

Downloading express, supertest and jest

4. mkdir -p app/src app/test recursively creates srcand test in app directory.

Directories have been created and listed

5. Make sure you create 2 scripts in package.json

"scripts": {
"start": "node app/src/index.js",
"test": "jest"
}

6. Add the following in package.json file that was generated after step 1.

"jest": {
"testMatch": [
"**/*.test.js"
]
}

The above one is just a regex that says jest will look into any directory for test files that end with a suffix:.test.js

This is how package.json looks like after step 5 and 6

7. Create fileapp/src/app.js:

Create an express app

8. Create another file app/src/index.js:

Starts the app on port 3000

9. npm start

Run the app

10. Stop the above app by pressing control+C.

11. Quickly create a fileapp/test/e2e/works-on-my-machine.test.js and write the following e2e test:

Creating an e2e test displaying how we want our expected endpoint to work

12. npm test

Run the above test and you see it fails:

Test case failed

This happened because we did not create an endpoint / yet.

13. In app/src/app.js, lets add an endpoint /.

Creates an express app with 1 endpoint “/”. When making GET request on “http://localhost:3000/”, it will return “It works on your machine too 😃!”

14. Upon running npm test again, the test case succeeds.

The test case succeeded this time

15. Run npm start again and hit the endpoint http://localhost:3000/ from your browser.

Run the app again
It works on your machine too 😃!

Great! The app is well tested, and is up and running. Time to ship it. 😃

But every time our customers try to run the app, they need to perform all the above 15 steps. They might just mess something up. What is they download dependencies that are not supported? Plus, it eats up too much of our customers valuable time. Not very easy to use. Is it? 😏

What if you could bring your machine to your customers house and run it for them? Easy, right? You could do that for 1 customer. But what if you had thousands? Or millions? How many houses would you have to go to?

Lets enable our customers to use the application as if you were running it on your machine without having them go through the pain of installing dependencies and doing other setup stuff…

Well, I would take the liberty to assume that you already have Docker setup.

So I will straight away jump to the necessary steps:

Docker — You could literally take you computer to a million of your customers houses and run the app for them:

  1. Quickly create a simple file named Dockerfile in your project root and add the following code. Added comments for explanation:
Dockerfile

2. docker build -t works-on-my-machine-image .

Build was successful

This step takes some time usually when run for the first time. Would require some patience.

The above build was successful. If any step would have gone wrong, let’s assume some test case failed, the build would fail. Try messing with the e2e test we wrote above and re-run the build. And notice how the above build fails.

3. docker images

An image named “works-on-my-machine-image” is created

4. docker run -p 3000:3000 --name works-on-my-machine-too works-on-my-machine-image

Runs a docker container from the image we built

5. Hit http://localhost:3000/ from browser again.

It works!

Running 6 steps can also be too much. No? 😛

First things first, lets stop and remove the container and images we created above:

  1. docker stop works-on-my-machine-too to stop the container.
  2. docker rm works-on-my-machine-too to remove the container.
  3. docker rmi works-on-my-machine-image to remove the image.

Now let’s enable our customers to run the application in even lesser steps.

Docker-Compose to the make carrying your computer to houses of your customers even easier:

  1. Create a file named docker-compose.yml in the project root:

2. docker-compose up

Builds the image if it doesn’t exist and starts the application.

The application started and can be stopped by just hitting control+C.

3. Hit the api again:

It still works!

Our customers can now use the application in just one step by performing step 2.

They can just download your application, and straight away run it. And mess around, and simply delete the container.

Happy customers… Satisfied customers… 😉.

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

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
Aditya Singh

Aditya Singh

I write code. Engineer at CoinDCX.