# #26 Docker File

**Lecture-9 Dockerfile Part-1**

* A Dockerfile is a text document that contains all the commands you would normally execute manually to build a Docker image. Docker can build images automatically by reading the instructions from a Docker file. In a Dockerfile, an ENTRYPOINT is an optional definition for the first part of the command to be run.
    

**Create a docker file :**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686383304369/5e705982-0912-46f5-8655-870718f3a77d.png align="center")

**Build a Dockerfile :**

Build an image from the current path and will search for the file from the current path.

\-t is a tag

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686383488426/b06780d5-e41c-44e4-a897-f5157c32ad86.png align="center")

Build an image from the current path with the given file name.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686383559768/118504ad-0d38-4425-9129-d14793e84030.png align="center")

**Lecture-10 Dockerfile Part- 2**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686388390773/f57c853b-440b-4729-8941-e42438e84317.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686388456234/cb1a317f-9a67-401c-9cf6-5672e290c68b.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686390315362/a692774b-2093-4de7-ad6c-666a5fe44ca9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686390424412/4548fc3f-d83d-40ea-a02d-31163bb307e3.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686390466167/9dee2d8a-9e3f-4870-9075-36f38c55184b.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686390551971/0b125a9d-01b6-499b-9c54-2b5e0a957284.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686390643324/1727435d-d125-4608-a3b9-ac7e2e9738a3.png align="center")

Package Installation on single command.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686390805233/ad7730b2-307e-4e37-80f8-751e01911176.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686390888800/1e62760f-d6e7-4395-856d-2aaa370054ba.png align="center")

**Docker file layer architecture:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686391271828/0567c0ed-6af7-4d24-97fe-794b50edb59e.png align="center")

FROM centos: In the first layer creates a container from the base image i.e. centos and delete that container.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686391464021/9650841b-3076-4481-bc32-0e6e2594b7dd.png align="center")

2nd layer: run the touch command by using the previous image and created the container and image.After that deleted that container.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686391577535/457c783a-f7ff-4801-992f-1f95728f29d8.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686391633193/907e340e-9a44-486f-85ed-5e6aad93e70f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686391676212/3c1d929f-4905-4645-8a77-f093e4d0c089.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686391709415/b1fe8752-f4c2-42ee-9793-e1157c3789b8.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686392238025/c2f67339-6d18-48e5-be61-7656eeb7f856.png align="center")

Creating a build using cache. and new layer commands will create a container that will create an image and delete that container.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686391938481/29262b04-d554-4ffd-94a2-08f99db2887a.png align="center")

If u append the data from the beginning/in the middle layer will start from that path.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686392356756/1ac44440-856b-4ef4-8fb1-b0735dbad1f2.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686392489877/38d4c28b-8cf8-4c72-8442-fa032ff1802f.png align="center")

**Lecture-11 Dockerfile Part -3**

Lable define on docker file.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686392777097/f022807c-2199-41e4-95f6-eb27409844cb.png align="center")

Define environment variable.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686392909284/fa189c28-96d0-4e99-9d3a-de0b8ae7935c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686392974393/09e56cb2-788c-4386-a5f6-4e3fa026ab87.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686392994247/d29063c6-c0a1-4ea2-b484-a186f4377915.png align="center")

Use case:

**Define Environment variable.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393161400/b73799a0-c1d1-4e27-9365-1e2a8a795a67.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393200939/c21f8c75-c434-4588-bc0a-8648dbf6dedc.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393239415/8bc68a94-bb73-4753-910f-70a88acdcec0.png align="center")

**Instead of login into the root, log in from the mentioned user.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393488353/985b2abb-8af1-418d-863c-ac3a8e0becb3.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393532201/65cac7b6-e29c-4800-a388-0f676b6291e5.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393563159/e8be3d56-942a-4c7e-9ad6-b2e1cefd4f81.png align="center")

**Define the working path:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393389690/a2c1f4dd-c46c-428a-9c52-196d1dce8f19.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393631795/448b8eb3-7db5-4114-9952-7d32332cfb9d.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393683865/ce30f905-a81e-4a15-8a87-96fd38b10bef.png align="center")

**Copy CMD:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393789235/aa695a77-7732-4afd-b610-2bb54bb609e5.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393848219/85370ee2-dfd8-4232-add3-10d869f4313b.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393893632/4a20d70b-2a08-4c30-8858-2c7f35e20efc.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686393940601/73a61520-aaa4-4283-bcb5-b77768eb63cf.png align="center")

**Difference between ADD and COPY commands:**

**COPY command will copy the tar.gz file to mentioned location.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394013288/3e2d3569-81c7-452c-b877-42400e62ae97.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394087803/6a9dc02e-2203-4b2f-a498-8c7254edb2b6.png align="center")

Image build :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394140890/23afd748-40cd-4070-b888-9fc9237c3a27.png align="center")

Container build:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394183978/e5cc3cbd-80c3-4935-b731-205b4ad97ed1.png align="center")

**Add command extracts the tra.gz file and keep the content in the mentioned location.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394235430/c76a2642-b1be-4aaf-91dd-78793d1c5eb1.png align="center")

build img:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394373649/ad6cd8a9-5547-45f6-b2ba-377d2fd39f9f.png align="center")

Build container:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394416211/38c22284-d89a-4289-9ce4-2e0b7a948b73.png align="center")

**The COPY command will copy the whole link in the mentioned path instead ADD command will copy the content of that link.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394520938/540711d7-2413-432a-8a86-daadafcb10a7.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394624439/f683fe68-c471-4df5-b8fe-ee8a2b6d1c22.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394654743/d9ec2e52-6a55-4708-959f-180cb6c2e451.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394687905/1d7321e1-ef30-4696-82a2-37199640aade.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394711009/3b601ec0-2c08-4465-82e4-7bbcc8188052.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394740938/828844fc-17a4-4ece-8a50-baf639341d16.png align="center")

got failed due to which is asking for the source. Instead, ADD command will copy the content work as wget.

**CMD command: When an image is built and deployed in the container First which command needs to be run? which is done by the CMD command.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686394904397/1585e240-3bb9-4e75-9841-a9a596670141.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686395290113/76003294-4d47-4cd5-a3f7-88819e770743.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686395343571/1cacbf8c-ae0e-4cc4-ab8e-3769dd797692.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686395358936/636097c3-4b6a-41e7-8f44-cf08f5c3aac9.png align="center")

**Lecture-12 Dockerfile Part- 4**

PORT mapping:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686395826307/43062447-de0d-40a8-983a-bc726a069373.png align="center")

If you want to expose port/to do the ssh you need to define it in the docker file.

Instaall ssh package.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686395992719/a2b77668-88dd-405a-bfa1-bd356e791dfc.png align="center")

Deamon start :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686396144540/bfb09f09-ec1d-47aa-ada7-81ef9023f420.png align="center")

Deamon restart.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686396030746/9b12f4b1-1adb-4db5-bc93-83a5359f5785.png align="center")

Port expose by EXPOSE 22

Image build :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686396245402/7f310820-3b0a-4e6d-b3cc-41feafa32942.png align="center")

Image deployment :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686396300944/742ac740-3695-4d85-bd4c-f77ddc6da42a.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686396328400/125dead4-3fbf-4f8b-8cce-8de3a4773d91.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686396385140/3a2cf082-b814-4c54-b186-3a080556c792.png align="center")

Done SSH from host machine:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686396427617/95fea5ab-b926-45e4-ba40-e52798a572a6.png align="center")

Do the access from outside/remote/another laptop machine for that required port mapping:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686396652598/c1c55142-083d-40c4-9547-524c271af7b2.png align="center")

mapping to host port: container port.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686396801284/de99e0bd-b73a-4c68-9f5b-0f725d001215.png align="center")

From outside if you want to connect to eth0 IP where AWS allows public IP in IGW which translates to private ip and that private IP translates to container IP {i.e eth0}which is always private. Hence access the container from the outside.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686396865348/ff15a77e-2524-4a47-8d85-4e12293d326a.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686396893564/e4ce101c-f20a-40d0-9f8c-b02068e786a6.png align="center")

**Lecture-13 Dockerfile Part-5**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686397767046/ea7597d2-7618-444f-9dd7-7b1d44f01761.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686397786189/e232ed51-a09b-4e9d-841b-e1b2dfa8c295.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686397829137/547b91c9-a93f-4f8f-8169-1b4c435331a6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686397856260/7ebcfca7-ad39-4486-937c-169271c98be6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686397907263/58359618-09db-4097-8f3f-e38faa3e5ffe.png align="center")

Container IP

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686397951733/52084ce0-13f3-412a-b150-2dcd3501fd81.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686397992705/f5e014b6-48bd-46d1-bee7-ab32b1f59c33.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686398025600/21d6cf8e-1d7c-4f76-b7e9-999462b7f1f8.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686398066386/41799e55-a1cb-47eb-9e6d-31f2e4c2222c.png align="center")

You can access from outside by using public IP who translates to private IP which is connected to the container IP.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686398242885/79b20fcd-65dc-431e-b125-1c9b73e3f1a3.png align="center")

**Lecture-14 Dockerfile Part-6**

Custom name for the docker files.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686398450956/1b7f8a0e-c955-45ff-9440-3d7baf86d86e.png align="center")

Need to provide the proper path to build the image

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686398469063/abc3c6ad-d18c-4509-bebd-12a61daef3eb.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686398530678/96a6ddfd-f520-4bab-946c-c6d1f36082f9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686398756871/65acab0a-d5eb-4783-9f30-17e49927e6df.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686398782192/f7018796-10b6-44ba-8b8c-132a4db90c39.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686398811710/95d6a18c-60fd-46b1-a02e-edd8c95f1435.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686398866903/9b196c9d-398b-49ba-ac8a-9373eb9823f9.png align="center")

Without creating a docker file you can build an image as well.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686398988773/2d547faf-6753-484a-b85c-107a13c38bf9.png align="center")

Build a docker file from the remote machine:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686399057822/63922eb0-3e97-47de-be09-06389a35e896.png align="center")

No need to write an image keyword without that also the image will get built.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686399129602/78130b58-6dfa-4e73-a005-e4196f66ddfc.png align="center")
