π³ Introduction to DockerFile π¦:
A DockerFile is like a recipe for creating Docker container magic. It's a simple text file used in the Docker world.
It tells Docker what steps to follow to build a container image ποΈ. Think of it as packaging your app and all it needs into a single, lightweight, and isolated box π¦ that works the same everywhere.
A Dockerfile has instructions like choosing a starting point (base image), adding stuff (copying files), making things happen (running commands), setting the mood (environment variables), opening doors (exposing ports), and telling the container what to do (running processes) π οΈ
π After crafting a Dockerfile, ποΈ you can employ the 'docker build' command to fashion an image from it.
πΌοΈ This image can subsequently be deployed to spin up containers housing your app and its necessary bits, nicely tucked away from the host system and other containers.
π³ Dockerfiles play a crucial role in Docker's strategy for organized and repeatable software deployments.
Project Task :
Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)
Application File
Here, app.js is the file where the developer has written code & the file is located in the same directory as Dockerfile.
vim app.js
const http = require('http');
const hostname = '0.0.0.0';
const port = 8000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello NodeJS from Docker\n');
});
server.listen(port, hostname, () => {
console.log('Server running at http://%s:%s/', hostname, port);
});
process.on('SIGINT', function() {
console.log('Caught interrupt signal and will exit');
process.exit();
});
Dockerfile
Build the image using the Dockerfile and run the container
To build an image from this Dockerfile, save it as "Dockerfile" in your application's root directory, and then run the following command in the same directory:
vim dockerfile
#Base image / Parent image for running node js application FROM node:6 #working directory for container when launched WORKDIR /app #copy content from our current directory (app.js & Dockerfile) & place inside app/ COPY . /app #Exposing container port to 8000 EXPOSE 8000 #To run node application CMD ["node","app.js"]
docker build -t my-node-app .
If you are facing above error then try below command
sudo chown $USER /var/run/docker.sock
Here, my-node-app is the name you're giving to the image, and . indicates that the build context is the current directory.
Once the image is built, you can run a container from it:
docker run -d -p 8000:8000 my-node-app
Verify that the application is working as expected by accessing it in a web browser
Running the Application
Open a web browser and navigate to http://<public_ip>:8000. If your Node.js application is working as expected, you should see the output of your application in the browser.
Push The Image From local Repository to Dockerhub
Push the image to a public or private repository (e.g. Docker Hub )
To push the Docker image to a public or private repository, you can follow these steps, to know how to push the image to Docker Hub:
Create an Account on Docker Hub:
If you don't have a Docker Hub account, go to Docker Hub and create one.
Tag the Image:
Before pushing the image, you need to tag it with the repository's URL and a version tag.
Replace **<docker-hub-username>**with your Docker Hub username and my-node-app with the name you used for the image:
docker tag my-node-app <docker-hub-username>/my-node-app:v1
Log in to Docker Hub:
In your terminal, log in to your Docker Hub account using the docker login command:
docker login
Before Push Image :
You will be prompted to enter your Docker Hub username and password.
Push the Image:
Once logged in, you can push the tagged image to Docker Hub:
docker push <docker-hub-username>/my-node-app:v1
Docker will upload the image to your Docker Hub repository. Replace <docker-hub-username> with your Docker Hub username.
Access the Image:
-
Your pushed image will now be available on Docker Hub. You can see it by visiting your Docker Hub profile in a web browser and navigating to the "Repositories" tab.
I hope you enjoy the blog post!
If you do, please show your support by giving it a like β€, leaving a comment π¬, and spreading the word π’ to your friends and colleagues π