Open In App

Docker – Deploying WebApps on Docker

Docker applies abstraction at the software layer of the operating system to create an instant and portable environment that includes everything you need: a Linux kernel, libraries, system tools like grep or sshfs, plus applications (web servers/services) and all dependencies needed by those applications (e.g., database engines). When deployed, these containers are all bundled up and run in a single, isolated process. The benefits stem from using containers rather than virtual machines and enabling you to run the exact same application production environment on different servers without modifying it (or without having to purchase servers for every deployment).

Using the right tools ensures that you are going to be delivering a great user experience, and if you want that experience to be easier and more consistent across all your environments, you should consider Docker. The developer community has been successful in growing open-source tools and frameworks in many industries because they allow companies to rapidly iterate on their products without having to go through expensive tooling investments. The environments Docker creates are not the same as virtual machines, but the benefits you will see when you use them should outweigh these costs.



Prerequisite

Docker is an open-source containerization platform for apps and services. It allows developers to build, ship, and run any application in a consistent, reproducible way across whatever machines they choose. It was developed by Solomon Hykes in 2013 while working at dotCloud as a way to speed up their development process.

When should Docker be used?

Docker is not a replacement for virtual machines, but rather a way for you to use containers and host your larger web applications closer to their users. Docker’s greatest advantage is that it can help you scale out your applications, which means more instances running at any given time. It also provides isolation between containers so that they don’t step on each other.



Why Web Apps? 

Web applications are the most prevalent types of applications built in the 21st century, and they are a great way for organizations to expand their reach and increase their revenue. Web apps have the potential to become more convenient and seamless than ever with services like Google Docs or Salesforce.com. You may want to take advantage of Docker in order to do this.

When Not To Use Docker?

You might want to consider using Docker if you need to run multiple instances of your application on a single host. You may also want to look into other tools like systemd containers support, rkt (pronounced as “rocket”, a CLI tool for running app containers on Linux) written from scratch for security and ease of use, or lightweight Hypervisor VirtualBox.

How to Deploy a Web App on Docker

First we need to install the docker on the servers for that refer tot he following

Step 1: Creating Dockerfile

Dockerfiles are meant to be used from within the terminal, in a text editor, or on the command line. Compilers include support for defining programs by embedding a text representation of an executable image into a Dockerfile. The result is an entirely self-contained container image. However, it opens you up to vulnerabilities that could potentially give outside parties access to your source code or private keys if someone were to hack or brute-force your Docker daemon. 

FROM tomcat:8.0.43-jre8
ADD helloworld.war /usr/local/tomcat/webapps/
EXPOSE 8080
CMD chmod +x /usr/local/tomcat/bin/catalina.sh
CMD ["catalina.sh", "run"]

Step 2: Containerize your application

Build a Docker Image of your web app.

$ docker build -t [image name] .

docker build -t myfirstwebapp .

List the Docker Images

docker images

Step 3: Push the docker image to a docker repository

Login to docker hub

docker login

Retag docker image

docker tag [existing_image_name:tag] [new_image_name:tag]

Push docker image

docker push image_name:tag

Step 4: Pull the docker image and run in Linux

To pull and run a Docker image in Linux, use the docker run command with the image name. This command automatically pulls the image if it’s not available locally and starts a container based on that image.

docker pull image_name:tag

docker  run -i -t -d -p 8080:8080 image_name:tag

This docker run command creates and runs a new Docker container based on the specified image. Let’s break down the options:

Step 5: Aceess the docker conatainer

To access a Docker container from an EC2 instance and localhost, you need to ensure that the container is running and exposed on a port accessible from both the EC2 instance and localhost. Once the container is running and the port is exposed, you can use the EC2 instance’s public IP or DNS name to access the container from the EC2 instance, and you can use localhost or 127.0.0.1 along with the mapped port to access the container from localhost.

Deploying WebApps on Docker – FAQ’s

How to deploy web API in Docker?

To deploy a web API in Docker, create a Dockerfile specifying the base image, copy the API files into the container, expose the necessary port, and run the API application. Then, build the Docker image and run it as a container using the docker run command.

How to deploy Java web application using Docker?

To deploy a Java web application using Docker, create a Dockerfile defining the application environment and dependencies, then build the Docker image and run it as a container, exposing the necessary ports for access.

How to connect to API in Docker?

To connect to an API in Docker, ensure the container has network access to the API’s endpoint and use the appropriate HTTP client within the Docker container to make requests to the API.

What is the default port for Docker API?

The default port for the Docker API is 2375 for unencrypted communication and 2376 for encrypted communication using TLS. These ports are used for Docker client-server communication and management operations.

What is Docker daemon?

The Docker daemon is a background process responsible for managing Docker objects such as images, containers, networks, and volumes. It handles client requests, communicates with the Docker API, and manages the container lifecycle on the host system.


Article Tags :