Open In App

How to Access Docker Container From Browser

Last Updated : 08 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In present-day software advancement, Docker is significant for building, shipping, and running applications. It ensures that applications and their dependencies operate seamlessly across environments by enclosing them in containers. This is especially helpful for web applications that need to be tested and put into use in a variety of configurations.
Accessing a Docker container from a browser is common while working with web applications. Whether developing a straightforward web server or a perplexing microservices design, interfacing with your containerized application by means of a web browser is fundamental for testing, development, and deployment.

This guide provides a solid understanding of how to make your Dockerized web application browser accessible by covering the necessary terminology, providing step-by-step instructions, and providing examples, we'll also talk about common questions and problems you might run into.

Primary Terminologies

  • Docker: Docker is an open-source platform that works with the medium of delivering software as packages called containers, used with OS-level virtualization, containers are lightweight, standalone, executable software packages that encompass everything an application's code or runtime, libraries, or dependencies to run the application.
  • Docker Container: A Docker container is a runtime instance of a Docker image. It is lightweight, standalone, and executable—meaning it is just like a small software package that has every element needed for running an application, the containers are isolated from each other and the host system, which means providing a consistent runtime environment.
  • Dockerfile: A Dockerfile is a text document that contains the necessary commands for creating an image in Docker, the Dockerfile consists of instructions on a base image, application code, dependencies, configuration settings, and commands to run within the container.
  • Image: A read-only template used in creating containers. In this template, there is application code, runtime, libraries, environment variables, and configuration files. Images are built as layered and incremental changes, one layer at a time from the previous layer. Images can be stored in container registries, for example, Docker Hub.
  • Port Mapping: Allows access to a container’s ports from the host machine, enabling communication with external clients.
  • EXPOSE: A Dockerfile instruction indicating the port a containerized application listens on for connections.
  • CMD: A Dockerfile instruction specifying the command to run when a container starts.
  • Host: The physical or virtual machine running Docker, providing resources for containers.
  • Network Bridge: A Docker networking mode connecting containers to a private network on the host, facilitating communication.

Step-by-step process to access the docker container from the browser

Step 1: Launch an EC2 Instance

  • Go to AWS Console and log in with your credentials or create a new account
  • Now navigate to EC2 dashboard and launch an EC2 Instance
Launch an EC2 Instance

Step 2: Install docker

  • Install docker in our local machine by using following command
sudo yum -y install docker
 Install docker

Step 3: Start Docker Daemon

  • Now start docker by using following command
sudo systemctl start docker
  • To enable the Docker daemon to start on boot
sudo systemctl enable docker

Verify Docker Daemon Status

  • To verify that the Docker daemon is running, use the following command:
sudo systemctl status docker
Start Docker Daemon

Step 4: Create a Dockerfile

  • To demonstrate accessing a Docker container from a browser, we will create a simple web application using Python’s Flask framework. First, create a directory for your project and inside it, create a Dockerfile:
#dockerfile
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 5000 available to the world outside this container
EXPOSE 5000
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Create a Dockerfile

Step 4: Create a Simple Flask App

  • In the same directory, create an app.py file:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run(host='0.0.0.0')
Create a Simple Flask App
  • And add a requirements.txt file:
flask
flask

Step 5: Build the Docker Image

  • Open your terminal, navigate to your project directory, and build the Docker image using the following command:
docker build -t my-flask-app .
Build the Docker Image

Step 6: Run the Docker Container

  • Run the Docker container and map port 5000 in the container to port 5000 on your host machine:
docker run -p 5000:5000 my-flask-app
Run the Docker Container

Here we see that docker container was successfully running on 5000 port. Now we can access our application in web browser

Step 7: Access the Application in Your Browser

  • Open your web browser and navigate to http://<ip address of local machine>:5000. You should see "Hello GeekforGeeks...!" displayed, indicating that your web application is successfully running inside a Docker container and accessible from your browser.
  • Now navigate to EC2 dashboard and copy public ip of local machine and browse it
Access the Application in Your Browser

Conclusion

The most essential thing that most developers do would be accessing the Docker container from a web browser. All these provide a containerized environment with a lot of consistency and reproducibility in working environments, hence ease during development and testing for deployment. This guide walked you through the steps needed to take a Dockerfile and image to running a container and accessing it from your browser.

We first gave an overview of Docker and the main terminologies that underlie this technology. Understanding these terms will help demystify the process and make it an ideal footing to work with Docker. Later, we demonstrate how to set up a simple web application using Flask, ranging from building to running it inside a Docker container and making it accessible via a web browser.

Throughout this guide, you learned how to effectively expose your containerized web applications to the outside world, thus making them accessible through the browser, this not only makes you more effective in your development workflow but also readies you for more advanced Docker use cases and deployments. Keep exploring and experimenting with Docker to unlock its full potential in your projects.


Next Article
Article Tags :

Similar Reads