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 Docker file 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
Basic Syntax
Each line in a Docker file is an instruction. Common instructions include:
FROM
: Sets the base image for the build.RUN
: Executes commands in the container during the build process.COPY
/ADD
: Copies files/directories from the host system into the image.CMD
: Specifies the default command to run when the container starts.ENTRYPOINT
: Defines the container's main process.WORKDIR
: Sets the working directory inside the container.EXPOSE
: Documents the port(s) the container listens on.ENV
: Sets environment variables.LABEL
: Adds metadata to the image.VOLUME
: Specifies mount points for volumes.
Sample Docker file for a simple node.js application:
#base image
FROM node:16
#Set Working Directory
WORKDIR /app
#Copy application files
COPY package.json .
COPY package-lock.json .
COPY . .
#Install Dependencies
RUN npm install
#Expose the application port
EXPOSE 3000
#Command to start the app
CMD ["npm", "start"]
Task:
Create a Docker file for a simple web application (e.g. a Node.js or Python app)
This file is available on internet, you can also create a hello-world application in python
Contents of app.py
Contents of requirements.txt
Build the image using the Docker file 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 )
In order to push image to docker hub, you need to create your account in it if not present
Now login into your docker hub account in the machine
Go to the directory where your docker file is present and build an image from it
Ensure that you have stopped the previous container which we created at start as it will create a conflict regarding port unavailability which I got.
I stopped the container and run docker command again and it worked.
Now we will push the image we created into our docker hub account
Commands Used :
docker build -t python-web-app . : this command builds a container named as python-webapp from docker file
docker run -d -p 8000:8000 python-web-app : this command will run the container in detached mode on port 8000, in source and destination
docker build -t dockerusername/python-web-app:latest . : this command is used to build a docker image for docker hub, ensure that you mention your docker hub user id and image name followed by tag
docker run -d -p 8000:8000 dockerhub-username/python-web-app:latest : this command is used to run the container locally
docker push dockerhub-username/python-web-app:latest : this command is used to push the image to our docker hub account