The Wayback Machine - https://web.archive.org/web/20240907061811/https://www.geeksforgeeks.org/docker-deploying-webapps-on-docker/
Open In App

Docker – Deploying WebApps on Docker

Last Updated : 23 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

  • Containerization vs Virtualization: Docker uses “containers” for its containers. Containers are basically a mechanism for labeling resources with a namespace. For example, an application’s database and all of its files would be stored under a container’s file system namespace. What this means is that Docker makes it easier to deploy your applications on any machine. Docker also provides isolation between containers and makes life easier for deployment automation tooling by providing a safe, consistent environment to run in (the Docker daemon creates this environment automatically).
  • Single Process Containers versus Multiple Process Containers: Docker is not a single-process containerization framework. It does not automatically create processes for each of your applications. Instead, it creates a container for each running application and allows you to manage a process tree. Applications can still be written as multiple processes, and Docker will intelligently link them together (in the same way that multiple processes are linked together under the same application). 
  • Built by Docker, Not for Stopping Docker: Docker currently does not have great integration with other environments or systems like Google App Engine or Heroku. Docker is focused on the developer, and it is important to think about how your application will be run. Do you want to run your app in a secure, robust environment? If so, Docker is not for you.
  • No support for lots of containers: Docker currently has only limited support for scaling and auto-scaling containers across multiple hosts. Additionally, Docker does not do any type of load balancing or failover. These are important features in production environments because they allow you to scale out as needed without having to manually rebuild all of your containers (failure and availability). Support for this kind of orchestration will come with time, though.

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.

  • Docker Images: Instead of working directly with an application’s code, developers work with something called “Docker images,” which are templates for building out containers—full environments where containers can be started, stopped, and otherwise manipulated. These containers are what is actually executed on the server.
  • Containers: Containers are lightweight, resource-isolated instances that run in parallel to other containers. They can be started and stopped quickly and do not require any additional resources to run. Each container is an isolated process running within a single kernel (OS process). These processes can share resources with other containers running on the same host, but they cannot communicate directly by address space or sockets. The host namespaces provide a way to share host system resources without compromising the isolation of containers.
  • Docker Compose: Docker Compose is a tool that helps you define, build and run Docker applications from an easy-to-use YAML file. By taking advantage of the Docker remote API, Composer makes it super simple to create and manage your applications.

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"]
  • FROM tomcat:8.0.43-jre8: Specifies the base image to use, which is Tomcat version 8.0.43 with Java Runtime Environment (JRE) version 8.
  • ADD helloworld.war /usr/local/tomcat/webapps/: Adds the helloworld.war file from the local filesystem into the /usr/local/tomcat/webapps/ directory within the container. This directory is where Tomcat looks for web applications to deploy.
  • EXPOSE 8080: Informs Docker that the container listens on port 8080 at runtime. This does not actually publish the port; it serves as documentation for users about which ports are intended to be published.
  • CMD chmod +x /usr/local/tomcat/bin/catalina.sh: Makes the catalina.sh script executable within the container.
  • CMD ["catalina.sh", "run"]: Specifies the command to run when the container starts. In this case, it runs the catalina.sh script with the argument run, which starts Tomcat and makes it serve the deployed web application

Step 2: Containerize your application

Build a Docker Image of your web app.

$ docker build -t [image name] .

docker build

docker build -t myfirstwebapp .

List the Docker Images

docker images

docker images

Step 3: Push the docker image to a docker repository

Login to docker hub

docker login

docker login

Retag docker image

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

Retag docker image

Push docker image

docker push image_name:tag

Push docker image

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.

  • Pull the image from the hub using the below command
docker pull image_name:tag

docker pull image

  • Run the docker image using the below command as shown image below.
docker  run -i -t -d -p 8080:8080 image_name:tag

docker  run

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

  • -i: Keeps STDIN open even if not attached.
  • -t: Allocates a pseudo-TTY, which allows you to interact with the container’s shell.
  • -d: Detaches the container and runs it in the background.
  • -p 8080:8080: Maps port 8080 of the host machine to port 8080 of the container.
  • image_name:tag: Specifies the name and tag of the Docker image used to create the container.

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.

Aceess the docker conatainer

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.



Previous Article
Next Article

Similar Reads

Google Cloud Platform - Deploying Django & its Content Management Systems
Django is a web framework written in Python that handles serving web pages for you. You define data models as Python objects, and Django simplifies communicating these to a database. Cloud Run is a managed serverless platform, where each server runs stateless. No data is stored on the servers themselves. But Cloud Run can connect to the state for b
4 min read
Microsoft Azure - Deploying Web Applications Using GitHub Actions
In this article, we will learn, how to deploy to your web app using GitHub Actions. GitHub Action is a workflow automation utility that has CICD built right in. This means a GitHub user can trigger a workflow run if there's a new commit, a new pull request, or a new branch is created. Below is a web app, go into the Deployment Center on the left. A
3 min read
Microsoft Azure - Container Registry For Deploying .NET Core Apps
Azure Container Registry to run a .NET Core App Azure Container Registry is a place where you can store your container images securely. Let's deploy a container image to it and see how we can use it. To follow along, we'll need to Visual Studio and Docker Desktop. Dockerfile is the source code for Docker image before pushing the images into the ACR
6 min read
Deploying NGINX Image To AKS Using Azure CLI and Advanced Kubectl Commands
Pre-requisite: AKS Features Azure Kubernetes Service (AKS) is a managed Kubernetes service that enables you to quickly deploy and manage clusters. AKS makes it easy to deploy and manage containerized applications without having to worry about the underlying infrastructure. It is a fully managed service, meaning that Microsoft handles the maintenanc
3 min read
Deploying Web Applications On AWS Lightsail
Amazon Web Service offers a service called Lightsail which is used for deploying and managing web applications. It is well suited for small-scale businesses and developers it will scale and manage the application in the cloud. What is Amazon Lightsail?For anyone who needs to develop websites or web apps, Amazon Lightsail is the simplest way to get
8 min read
Deploying A Node.js Application In kubernetes
Kubernetes, or K8s, is an open-sourced container orchestration technology that is used to automate the manual processes of deploying, managing, and scaling applications with the help of containers. Kubernetes was originally developed by engineers at Google, and In 2015, it was donated to CNCF (Cloud Native Computing Foundation). In this tutorial, w
9 min read
Planning And Deploying AWS Outposts
Amazon Web Services offers various services to its customers as a pay-as-you-go model. Various businesses use AWS services inside their software products, and they deploy their applications in various regions that AWS offers. Some businesses want to use services offered by AWS, but because of latency and data loss concerns, they hesitate to use tho
6 min read
Deploying An Application Using Jenkinsfile
In the IT world, initially, there were silos between the development and testing teams that created delays in moving the application to user availability. DevOps came into the picture with a culture practice that brings automation with a lot of tools. Jenkins is one of the popular CI/CD tools. Jenkinsfile methodology helps automate the entire pipel
5 min read
Deploying Static Sites On Netlify With Continuous Integration
when we are talking about DevOps In today's fast-paced digital world. deploying the website is one of the most crucial tasks. we need to focus on different parameters to make a website production-ready. The website updates from time to time according to the requirements, and whenever we make changes in the code, we need to push the changes in the c
6 min read
Creating a GKE Cluster and Deploying a Container
In this article, we are going to see how we can create a Google Kubernetes Engine Cluster for deploying a containerized application. We are also going to see how we can deploy a containerized application in this cluster. We will use a simple application developed by GCP (Google Cloud Platform), which runs through an NGINX server. So, let's get to i
8 min read
Deploying Scalable Applications with Azure
In modern software development, deploying a scalable application is an important task. With the increase in the number of users, the applications have to handle a large user database, for this, they require a robust infrastructure that is also scalable which will ensure seamless performance and reliability. Primary TerminologiesAzure: Azure is a cl
8 min read
Deploying NPM Modules in AWS Lambda
AWS Lambda definitely does change the way in which developers build and deploy their applications. it provides a serverless computing environment in which code can be executed in response to specific events without the need to manage the environment. When working with AWS Lambda, especially with Node.js, it's common to depend on external libraries
6 min read
How to Use Docker For Fault Tolerance with Docker Replicas?
Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. A cluster of docker engines is called a swarm. Docker Swarm is a cluster management and orchestration feature embedded in the Docker Engine. Docker ReplicasDocker replicas refer to running multiple instances of a doc
4 min read
How to Use Docker For Cross-Platform Containerization with Docker Buildx?
Docker has completely modified the manner in which software program is evolved and deployed, with the aid of introducing containerization generation. In order to package programs with their dependencies and make certain consistency of conduct across many contexts, packing containers offer a compact and portable solution. Deploying containerized app
7 min read
How to Use Docker Content Trust to Verify Docker Container Images
The world of containerized applications is based on trust. You rely on Docker images to be exactly what they say they are: secure, reliable, robust, and built with the right elements. But just like you can not just blindly trust any random ingredient in your kitchen, similarly the Docker image needs a kind of verification so we don't need to be con
12 min read
Docker Compose vs Docker Swarm
Docker is one of the most widely used container-based software on the market. Docker is an open-source platform for creating, deploying, and managing containerized applications. Docker allows us to simply bundle our apps into containers and can be deployable on any platform that supports docker software acting as a platform independent. What Is Doc
6 min read
Docker CLI vs Docker Desktop
Docker is an open-source platform. It is used to containerize applications. This is done by packaging applications along with their dependencies into containers that can be easily deployed. Users interact with Docker primarily using either the Docker CLI or the Docker Desktop Application. In this article, we'll be looking into each one in detail an
5 min read
Docker: How To Use Bash With An Alpine Based Docker Image?
Docker is a tool that is used to encapsulate the application with all its dependencies, called Docker containers. On the other hand, Alpine Linux is a lightweight and minimal Linux distribution. Here in this guide, I will first discuss what Docker is. Then I will discuss what Alpine is. After this, I will walk you through the different steps to ins
5 min read
How To Use Docker Security Tools To Secure Docker Container Images
Docker is a tool that is used to package the application and its dependencies into compact units called Docker containers. In this guide, I will first discuss what a docker is. Then I will discuss a security tool called Trivy, which is used to scan and detect Docker image vulnerabilities. After this, I will walk you through the different steps to d
11 min read
Docker - Publishing Images to Docker Hub
Docker is a container platform that facilitates creating and managing containers. In this article, we will see how docker stores the docker images in some popular registries like Dockerhub and how to publish the Docker images to Docker Hub. By publishing the images to the docker hub and making it public it gives users more flexibility in creating e
8 min read
Docker - Using Public Repositories To Host Docker Images
Docker is a software platform for creating isolated virtualized environments for building, deploying, and testing applications with ease. In this tutorial, we will learn how to host public repositories on docker hub which is a hosted repository service provided by Docker for finding and sharing container images. Just like GitHub allows the hosting
10 min read
Docker - Docker Container for Node.js
Node.js is an open-source, asynchronous event-driven JavaScript runtime that is used to run JavaScript applications. It is widely used for traditional websites and as API servers. At the same time, a Docker container is an isolated, deployable unit that packages an application along with its dependencies, making it highly scalable and maintainable.
12 min read
Docker Logs Inside the Docker Container
While running containers, it is important to know about various information like running processes generated by the container. Docker logs allow us to get this information regarding the containers. While it's pretty straighforward to check logs directly from Docker CLI, here we will see how to check logs from within a container. What is Docker?Dock
5 min read
Docker Tips : about /var/run/docker.sock
Docker has thus gained lots of usage today in software development. It allows developers to create, deploy, and run their applications in isolated environments. Such isolation will guarantee consistency between development, testing, and production environments and, finally, app security and scalability. A central part of the functionalities of Dock
8 min read
How To Use Docker Desktop To Deploy Docker Containerized Applications
Docker Desktop is an user-friendly tool that simplifies the deployment of Docker containerized applications on your local machine. It provides a graphical interface for managing containers, images, and volumes, making it accessible for developers and DevOps professionals. In this guide, we will walk you through the essential steps of using Docker D
9 min read
Docker Start vs Docker Run
Docker is a platform as a service(PaaS) that allows engineers to use OS-level virtualization to automate the deployment of applications inside lightweight and portable packages called containers. The containers have a very big advantage as they allow apps to run on different platforms effortlessly, and reduce the old headache of "it runs on my mach
7 min read
How to Link Multiple Container in Docker ?
Docker is a free software created by Docker Inc. and it enables clients to create free and confined environments where the users can deploy their applications. A container is a standard unit of software that packages up code and all its dependencies from one computing environment and runs it quickly and reliably into another computing environment.
2 min read
Installing Helm & Kubernetes in Docker
Helm is simply a package manager for Kubernetes. It helps you manage Kubernetes applications — Helm Charts helps you define, install, and upgrade even the most complex Kubernetes application. Implementation: Follow the below steps to install Helm and Kubernetes in Docker: Step 1: Installing Docker First, download the Docker.exe from the official si
2 min read
Docker - Containers & Shells
Docker is a set of platforms as a service (PaaS) products that use Operating system-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. All containers are
5 min read
Build, Test and Deploy a Flask REST API Application from GitHub using Jenkins Pipeline Running on Docker
Nowadays even for small web applications or microservices, we need an easier and faster way to deploy applications that is reliable and safe. These applications may be simple but they undergo rapid changes from the business requirement, to handle these changes we definitely need a CI/CD pipeline for deployment. Jenkins is one such tool that can mak
4 min read
Article Tags :