Dockerize an application in less than 3 minutes

Vladimir Mukhin
2 min readFeb 18, 2022

--

Everyone knows Docker. Docker is a set of tools that use OS-level virtualization to deliver software in packages called containers. If you have never tried Docker before, this guide is a perfect place to start. Follow these instructions and it will take less than 3 minutes to Dockerize your first application.

Step 1. Install Docker. Follow the instructions for your platform:
Windows: https://docs.docker.com/desktop/windows/install/
Linux: https://docs.docker.com/engine/install/ubuntu/

Step 2. Install Node.js. The application we will containerize is written on Node.js. Therefore, we need tooling that will allow us to test the application locally without a container first. However, of course, we can use an application that is written in any other language.
Linux: https://www.geeksforgeeks.org/installation-of-node-js-on-linux
Windows: https://www.geeksforgeeks.org/installation-of-node-js-on-windows

Step 3. Download the sample app. Open your terminal and run these commands:
Linux:
wget https://raw.githubusercontent.com/vladimirmukhin/nodejs-hello/main/app.j

Windows:
powershell "Invoke-WebRequest -Outfile app.js -Uri https://raw.githubusercontent.com/vladimirmukhin/nodejs-hello/main/app.js"

Step 4. Launch the application locally
node app.js

Step 5. Make sure the application responds on port 3000
curl http://127.0.0.1:3000

or open this address in your browser

Step 6. Create a Docker file. Create a file that is called Dockerfile(without extension) and add the following lines inside

Use node as your base image:
FROM node:latest

copy app.js into your container:
COPY ./app.js .

On a container startup run the application:
CMD node app.js

To summarize, your Dockerfile should look like this:

Step 7. Build your image.
docker build -t nodejs-hello:latest .

Step 8. Run your container.
docker run -d -p 3000:3000 nodejs-hello:latest

Step 9. Validate your container. Make sure the application responds on port 3000. But this time, it is actually being served from the container:
curl http://127.0.0.1:3000/

or open this address in your browser.

Congratulations! You have created your first docker image and launched your first container. Let me know in the comments how much time did it take for you.

Apply for individual mentorship here: https://yourdevopsmentor.com/apply/
Connect with me on LinkedIn: https://www.linkedin.com/in/vladimir-mukhin-devops/
The success story of my client: https://yourdevopsmentor.com/blog/from-biologist-to-devops-engineer/
Follow me on Twitter: https://twitter.com/vmukhindevops

Originally published Feb 18, 2022 4:44:03 AM (updated February 18 2022 )

Originally published at https://yourdevopsmentor.com.

--

--