How to Create a Container From Dockerfile (Method-3)

Share via:

How to Create a Container From Dockerfile (Method-3)

Dear Readers,

In this article,we will see  Create a Container From Dockerfile.

First, we need to know about Dockerfile.

What is Dockerfile ?

Dockerfile is normal text file.
We mention required instructions in the Dockerfile.
Docker can build images automatically by reading the instructions from a Dockerfile.
Dockerfile that contains all commands, in order, needed to build a Docker image.
The name must be Dockerfile (D must be uppercase and there is no space between them).

Each instruction creates one layer:

FROM creates a layer from the ubuntu from Docker image.
WORKDIR to change the working directory.
COPY copies a file/directory to your image.
ADD copy a file/directory from to your image, but can also extract TAR files(Auto extract) while copying.
RUN builds your application with make.
CMD specifies what command to run within the container.
ENV to assign variables (key and values).

Steps to Follow

  1. Create a file “Dockerfile” and Add Instructions.
  2. Create image from Dockerfile.
  3. Verify Images(created or not).
  4. Create a container “kt2-container” from image “kt2-image”.
  5. Verify file “kt-file” inside the container.
  6. Create another container from Dockerfile.
  7. Create 2 files “kt-file1” and “kt-file2”.
  8. Apply zip and tar to file “kt-file2”.
  9. Create image “kt3-image” from Dockerfile.
  10. Verify Images(created or not).
  11. Create a container “kt3-container” from image”kt3-image”.
  12. Verify files.

1. Create a file “Dockerfile” and Add Instructions

Note 

:wq! — to save & quit

2. Create image from Dockerfile

3. Verify Images

To see the list of all images inside your machine

4. Create a container “kt2-container” from image “kt2-image”

To see all the containers

Using below command to create a container

5. Verify file “kt-file” inside the container

Exit from the container

6. Create another container from Dockerfile

Note 

We added two instructions for copying files in the Dockerfile.
Those two files must be in current directory.
The second file must be in tar and zip format (kt-file2.tar.zip).
:wq! — to quit

7. Create 2 files “kt-file1” and “kt-file2”

8. Apply zip and tar to file “kt-file2”

Apply tar

Apply zip

Remove file “kt-file2”

9. Create image “kt3-image” from Dockerfile

To see the list of images

Using below command to create image

10. Verify images

11. Create a container “kt3-container” from image”kt3-image”

To see all the images inside your machine

Using below command to create container

We will be there /tmp by default because we mentioned working directory (/tmp) in the Dockerfile.

12. Verify files

To see the my name

Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates

KTEXPERTS is always active on below social media platforms.

 

Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment