Build your docker image using Dockerfile

 Docker can build images by reading the instructions from Dockerfile. A Dockerfile contains one or more commands that an user can run on the command line to assemble an image. Using 'docker build' users can build the image in an automated way that executes several command line instructions as defined in a Dockerfile. 

Let's understand this with few examples.

Example 01 [webapp01]

We have a python app that can print the IP address of the system which will run the application URL of a flask application. The reference code is looking something like below one:


 This application will listen on port 8443 in the container.

Now lets create a Dockerfile that will perform the following steps:

  • Use an 'ubuntu' image 
  • Install python3 and flask package
  • Copy the app.py application from the build directory to the docker image
  • Execute the script when we start a container from that image

The Dockerfile will look like below one:

 

Once the Dockerfile is ready, refer the following steps to build the image, run a container using that image and test it.


Image Build:

a. Get inside the build location where you have the app.py and Dockerfile.

b. Run the following command to build the docker image with 'myweb-app:v1.0' as name:

    $ docker build -t  myweb-app:v1.0  .

c. Now run the below command to check the newly created image:

     

Run the Container:

As docker image is ready, its time to run a container out of it. To run the container, we will use the following component:

  • host port: 80
  • container port: 8443 
  • host ip: 192.168.122.30

And we have to run it in detached mode so that we can use the same terminal for further activity:

  

Application Test: 

To test the application, run the 'elinks' command on the dockerhost itself. You should be able to see the dockerhost IP address as the that's the system making IP request to the application:

Now run the same command from another system in the same 192.168.122.0/24 subnet and see the out put:

 


Example 02 [webapp02]

 In this example we have another python app that will take your name as an input on the URL and print some statement. You can see the application code as shown below:


And we will use the same Dockerfile as our port is also same 8443 to listen the application. Dockerfile is something like below one:


Image Build:

a. Get inside the build location where you have the app.py and Dockerfile.

b. Run the following command to build the docker image with 'myweb-app:v2.0' as name:

    $ docker build -t  myweb-app:v2.0  .

c. Now run the below command to check the newly created image:


 

Run the Container:

As docker image is ready, its time to run a container out of it. To run the container, we will use the following component:

  • host port: 80
  • container port: 8443 
  • host ip: 192.168.122.30

And we have to run it in detached mode so that we can use the same terminal for further activity:


 

Application Test: 

To test the application, run the 'elinks' command on the dockerhost itself. You will see the instructions:

 

But if you want to check the same inside the docker container, then you have to use port 8443. Refer the below commands to test it:


And if you want to try from another system which is in the same 192.168.122.0/24 network like your dockerhost, then you should be able to use the standard url with port 80:

As testing is done, you can stop and delete the container. And then delete the image using the following commands:

 

To know more about the basic docker commands, refer Docker Cheat Sheet .

Comments

Popular posts from this blog

VNC Configuration using Ansible in CentOS 7

How to build Ubuntu Server 20.04 LTS OVA with vAPP Properties ?

[RHOSP] Red Hat Openstack 11 Deployment on Nested KVM Infrastructure