How to Access Docker Container From Browser
Last Updated :
08 Jul, 2024
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
Step 2: Install docker
- Install docker in our local machine by using following command
sudo yum -y 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
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"]
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')
- And add a requirements.txt file:
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 .
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
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
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.
Similar Reads
How to Automate Docker Container Updates?
In this fast-paced software development environment, the current status of application security should be important to the DevOps team. Docker is a widely used platform for application containerization, which gives a flexible and efficient way in managing application dependencies and deployments. Ho
9 min read
How To Create a Docker Container from an Existing Image?
Docker is an open-source software, that is used to contanerize our applications. Containerizing applications makes deployment a lot easier. For containerizing applications, docker uses Docker images, which act like templates for making containers. Today we will learn how to create a container from a
10 min read
How to Remove Docker Containers
Docker is a platform that provides a set of PaaS (Platform as a Service) products that help developers containerize applications, allowing them to run consistently across various supported environments. The core component of Docker is Docker Engine which consists of: Docker Daemon: The process respo
2 min read
How To Communicate Between Docker Containers Via "Hostname"?
In dealing with containers, Docker easily gets the place of a universal tool for both handling and development of applications. Docker containers are equivalent to isolated environments, therefore, the application of microservices architecture demands no further effort. Among the microservices princ
6 min read
Getting Docker Container ID from Container Name
Docker is a platform that allows developers to design, deploy, and manage programs within lightweight, portable containers. Containers bring together a program and its dependencies to maintain consistency across environments. Container management is critical in DevOps, and one typical duty is obtain
4 min read
Docker Compose Volumes for Container Data
For modern application development, data persistence is one of the most important aspects of containerized applications. One way to achieve this need for robustness when using Docker volumes is by using Docker Compose, a tool that makes orchestrating multi-container Docker applications easier. Volum
7 min read
Docker - Containers & Hosts
A common containerization tool in DevOps is Docker. It is an application deployment platform as a service for Docker containers. It consumes the least amount of resources, can be deployed more rapidly, and can scale easily while running your application inside of a container. Containers - Containers
5 min read
How Do I Edit A File After I Shell To A Docker Container?
In containerization, the `docker exec` command stands out as a powerful tool for interacting with running Docker containers. This article explores the capabilities and usage of `docker exec` and edits the files in the docker container, detailing how it facilitates seamless communication and control
4 min read
Connecting Two Docker Containers Over the Same Network
Whenever we expose a container's port in docker, it creates a network path from the outside of that machine, through the networking layer, and enters that container. In this way, other containers can connect to it by going out to the host, turning around, and coming back in along that path.Docker of
3 min read
How to Continue a Docker Container Which has Exited
Docker containers have reformed how programming is developed, deployed, and managed, offering unmatched productivity and consistency across various conditions. In any case, overseeing containers, particularly when they've exited, requires a nuanced way to deal with guarantee smooth tasks. At the poi
6 min read