Episode 4. Create first Dockerized Application
In the article, we will create our first dockerized application. We will understand commands and configurations in detail in the upcoming articles.
Here we will understand the steps to dockerize our application and run the server.
STEP 1: Pick any codebase of your choice (say Node.js or Python or PHP etc.)
STEP 2: Create a file named Dockerfile (without any extension) at the root level of your application
→ Paste below command codes in Dockerfile:
FROM nodeCOPY package.json /appRUN npm installCOPY . .EXPOSE 3000CMD ["node", "server"]
STEP3: Run docker build command.
docker build .
→ This will execute our Dockerfile image and create an environment.
→ Build command captures image of code plus the environment in which it is running
→ We should make sure Docker is running in the application, we get image ID at the end.
STEP 4: Run the container using the above image:
docker run -p 3000:3000 <Image ID>eg. docker run -p 3000:3000 6a047ebcc8590252c13c3c5ec377e4f0749e8f4ac8d801099abb46c89743334e
We see our terminal pointer is in running mode
→ Let us go to the browser and run
http://localhost:3000
Our web application will run 😀
Check all running containers:
→ To check all the containers running, we use the below command:
docker ps
→ Docker assigns a random name. Copy the name.
Stop the container:
The below command is used to stop the container
docker stop <image name>eg. docker stop naughty_torvald
http://localhost:3000
→ So without installing node.js packages. We started our node.js server successfully.
npm install // started server without running this command
Video:
Closing Thoughts:
Happy to create our first dockerized application. If you are working in other technology. It is fine. Docker supports almost all technologies.
The key point is no software installation is required in every machine. We can share the images with the team and directly use them.
Thank you for reading till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter and subscribe Happy Learnings !! to see some other tips, articles, and things I learn about and share there.