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.
Let's talk about what we insert in 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.
Task
Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)
Clone the repository in your system using the
git clone
command, view the content of the repository & create a Dockerfile if not present or modify the present Dockerfile.FROM
creates a layer from thenode:12.2.0-alpine
Docker imageWORKDIR
defines the working directory of a Docker container.COPY
adds files from the host machine to the image.RUN
installs necessary packages or dependencies likenpm
for the applicationEXPOSE
specifies the port 8000 that should be exposed on the containerCMD
specifies what command to run within the containerBuild the image using the Dockerfile and run the container
Now build the image by using the command
docker build
,-t
option for tag &'.'
represents the current path.Then run the container by using the command
docker run
,-d
option to run this container in the background,-p
for port exposure.Verify that the application is working as expected by accessing it in a web browser.
Go to your ec2 instance security group and check whether the port
8000
is opened or not. If not, then add it to the inbound rules of the security group.Then open
<public_IPv4_of_instance>:8000
at any web browser.Push the image to a public or private repository (e.g. Docker Hub )
Here use command
docker login
to login Docker Hub with credentials.Then use the command
docker tag <image_name> <username/image_name:tag>
Finally, push the image to a public or private repository by the command
docker push <username/image_name:tag>
Thus we could see the pushed image from the instance to DockerHub
Reference: Video
Thanks for reading my blog!
Also thanks for valuable time.
Keep learning, keep upskilling.