Writing Docker File
A Dockerfile is a blueprint used to create a Docker image. It defines the base operating system image, required libraries and dependencies, source code, and other configurations. Dockerfiles use a Domain-Specific Language (DSL) and contain step-by-step instructions for building a Docker image. it should be create with filename Dockerfile without any extensions.

Docker builds image by reading the instructions from a Dockerfile. The Dockerfile instructions syntax is defined by the specification reference in the Dockerfile reference.Here are the most common types of instructions:
FROM <base-image>
Sets the base image for your Docker image. Every image starts from a base, like Ubuntu or Python.
RUN <commands>
Executes commands in a new layer on top of the current image and commits the results. Can be written in shell form or exec form.
COPY <src> <dest>
Copies files or directories from your local machine into the image.
WORKDIR <path>
Sets the working directory inside the container. All subsequent commands run from this location.
ENV <key>=<value>
Defines environment variables inside the container.
EXPOSE <port>
Documents which port the container listens on at runtime.
CMD ["executable"]
Specifies the default command to run when the container starts.
Example: Dockerfile for a Flask Web App
Scenario:
We want to create a Flask web app using Ubuntu as the base image and run the application on port 8000.
Source code (hello.py):
The following Dockerfile creates a container image, which includes all the dependencies needed and automatically starts your application.
Building docker image
we can build the docker image of the above Dockerfile using the command syntax: docker build -t <imagename:version> path


Running images to Create container
we can run images to create a container , we can give a custom name to container or it can use random name by default.


Last updated