Table of contents
Dockerfile:
Instead of manually creating docker images by running multiple commands one by one, we can write a script where we can specify everything required for our image, and run one command to build the whole image.
Each command in Dockerfile, adds layers to create a Docker image.
Here are some of the most commonly used commands in a Dockerfile:
FROM: Specifies the base image required for our new image.
RUN: Executes a command in the image during the building of the image, like installing packages.
COPY: Copies files from the host machine to the image.
ENV: Sets an environment variable in the image.
EXPOSE: Specifies the ports that should be exposed on the container.
CMD: Executed by container by default, when we launch the image to run the container.
Tasks:
- Create a Dockerfile for a simple web application (e.g. a Node.js or Python app).
Python application cloned: github.com/shreys7/django-todo
The steps to run the application locally are explained in the above link. I am just creating the Dockerfile for the same steps.
Dockerfile:
FROM python:3
RUN pip install Django==4.2.2
COPY . .
RUN python manage.py migrate
CMD ["python", "manage.py", "runserver", "0.0.0.0:8005"]
2. Build the image using the Dockerfile and run the container.
Build the image -
docker build . -t todo-app
Run the Container -
3. Verify that the application is working as expected by accessing it in a web browser.
4. Push the image to a public or private repository (e.g. Docker Hub ).
Firstly, I renamed the image to push in my docker hub repo.
sudo docker tag old:tag new:tag
Now, while pushing I got access denied so I first logged in and then pushed the image.
sudo docker login
sudo docker push imagename
The image is successfully pushed.
Thank you for reading! ๐