Table of contents
๐ Dockerfile: -
Docker is a tool that makes it easy to run applications in containers. Containers are like small packages that hold everything an application needs to run. To create these containers, developers use something called a Dockerfile.
A Dockerfile is like a set of instructions for making a container. It tells Docker what base image to use, what commands to run, and what files to include. For example, if you were making a container for a website, the Dockerfile might tell Docker to use an official web server image, copy the files for your website into the container, and start the web server when the container starts.
Here are some of the most commonly used commands in a Dockerfile:
FROM
: Specifies the base image that the new image will be built on top of. For example, you might use an official Node.js image as the base for an application that runs on Node.js.RUN
: Executes a command in the image. This command is run during the image build process. For example, you might use theRUN
command to install necessary packages or dependencies for your application.COPY
: Copies files from the host machine to the image. For example, you might use theCOPY
command to copy the files for your application into the image.ENV
: Sets an environment variable in the image. For example, you might use theENV
command to set a variable that holds the version of your application.EXPOSE
: Specifies the ports that should be exposed on the container. For example, you might use theEXPOSE
command to specify that port 8000 should be exposed on the container.CMD
: Specifies the command that should be run when a container is created from the image. For example, you might use theCMD
command to specify that your application should be started when the container is created.
Example: -
๐ Run a DevOps Project using Docker: -
Step 1: - Create a Dockerfile for Nodejs application.
FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
RUN npm run test
EXPOSE 8000
CMD ["node","app.js"]
here, we are using an official Node.js image (node:12.2.0-alpine) as the base for an application that runs on Node.js
WorkDir which is currently using is app
COPY is used to copy files from the host to the Image
The RUN command is used to install npm, which will install packages
Expose is used to expose port 8000, on which we are trying to run the application on the Web
CMD commands specify which commands to run while creating a container from the image.
Step 2: - Build the image
docker build . -t node-app
Step 3:- Run the container
docker run -d -p 8000:8000 node-app:latest
Step 4: - Check for port 8000 on the web for running the application
Step 5: - Log in to Docker Hub
Step 6: - Tag an Image that we want to push to Docker Hub
docker image tag node-app:latest pavanpawar57/node-app:latest
Step 7: - Once the Image is tagged then, need to push it into Docker Hub
docker push pavanpawar57/node-app:latest
So in this way, we can run a complete project using Docker.
Thanks for reading โฃ๏ธ
Pavan Pawar