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.
Task:
Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)
Build the image using the Dockerfile and run the container
Verify that the application is working as expected by accessing it in a web browser
Push the image to a public or private repository (e.g. Docker Hub )
- Below is an example of a simple web application written in node.js. This is an app.js file that the server will respond with Hello World!
2. package.json
(Node.js application dependencies)
3. Create a Dockerfile:
The first thing we need to do is define from which image we want to build. Here we will use the node:14 image available from the Docker Hub.
Next, we create a directory to hold the application code inside the image, this will be the working directory for your application:\
Copy your application's code inside the Docker image folder app using COPY instruction:
This command installs all the dependencies defined.
This command releases port 3000 within the container, where the Node.js app will run:
This command starts the server and runs the application:
4. Build an Image using Dockerfile:
To build an image using Dockerfile.
docker build -t <image-name> .
By using the docker images command we can see the list of images.
Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port inside the container and the - - name flag for assigning a name to the docker container.
docker run -d - -name <container-name> -p 8000:8000 <image-name>
Verify that the application is working as expected by accessing it in a web browser
http://ec2-instance-public-ip:3000
Push the image to a public or private repository (e.g. Docker Hub
Firstly we need to login docker by using the docker login command
Push the docker image to the docker hub using the command:
docker push <image-name>
Thank you for reading! I hope you find this article helpful.
Happy Learning!