DEV Community

Cover image for Dockerize an application in less than 3 minutes
Vladimir Mukhin
Vladimir Mukhin

Posted on • Originally published at yourdevopsmentor.com

Dockerize an application in less than 3 minutes

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.

Image description

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.js
Enter fullscreen mode Exit fullscreen mode

Windows:

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

Step 4. Launch the application locally

node app.js
Enter fullscreen mode Exit fullscreen mode

Step 5. Make sure the application responds on port 3000

curl http://127.0.0.1:3000
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

copy app.js into your container:

COPY ./app.js .
Enter fullscreen mode Exit fullscreen mode

On a container startup run the application:

CMD node app.js
Enter fullscreen mode Exit fullscreen mode

To summarize, your Dockerfile should look like this:
Image description

Step 7. Build your image.

docker build -t nodejs-hello:latest .
Enter fullscreen mode Exit fullscreen mode

Step 8. Run your container.

docker run -d -p 3000:3000 nodejs-hello:latest
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

or open this address in your browser.

Image description

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/


Originally published on https://yourdevopsmnetor.com

Top comments (0)