Docker File Creation
Table of contents
No headings in the article.
A docker file is created to create images which can later be run as a container in docker in different systems.
A docker file is a script which can be created for the image which further can be run as a container in the system.
Now we will see how to create a docker file and all the important instructions to use.
We create dockerfile by naming the file with the same name called dockerfile.
after creating we write instructions into it.
Most used Instructions:
From
ADD
Copy
Run
Workdir
CMD
Expose
Label
Entrypoint
From
To add a reference image into our image which will be run while creating the container.
Eg. From nginx: version
is written to use nginx in our application.
Add
Adding particular files or urls from outside or within the application.
Eg. Add ./index.html ./app
Copy
Similar to add, but the only difference is that you can not add URLs from copy.
Note: Copy is better to use than add according to best practices to write dockerfile.
COPY only supports the basic copying of local files into the container, while ADD has some features (like local-only tar extraction and remote URL support) that are not immediately obvious
Eg. Copy ./index.html ./app
Run
Run is used to write those commands that you want to run while creating of the container.
Eg. Run apt nginx -y
CMD
CMD is similar to run, the only difference is that commands written under CMD run when the container runs.
Eg. CMD ["service","nginx","start"]
Workdir
Workdir is used to specify the working directory, it is better to use workdir than cd to access the working directory according to best practices.
Eg: Workdir ./app
Label
Label is used to add metadata to the image.
Eg:
Label user="tarang"
Expose
Expose instruction is used to define the port for the container which will be accessed.
EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime
Entrypoint
ENTRYPOINT instruction is used to configure the executables that will always run after the container is initiated.
If you want to watch a detailed video about docker, docker architecture and docker files, you can subscribe to my youtube channel, where I will be posting videos regularly.
Channnel: https://www.youtube.com/@tarangsharma2650/videos
Next I will be posting blogs on docker architecture , kubernetes architecture , best practices of creating dockerfile etc.
stay tunned, and subscribe to my newsletter.
Thank you,take care.