How to access a web application running in a Docker container?

DockerDockerBeginner
Practice Now

Introduction

This tutorial will guide you through the process of accessing a web application running in a Docker container. We will cover the basics of Docker containers, deploy a web app in a Docker container, and explore the methods to access the web app from your host machine.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker(("`Docker`")) -.-> docker/ImageOperationsGroup(["`Image Operations`"]) docker(("`Docker`")) -.-> docker/DockerfileGroup(["`Dockerfile`"]) docker/ContainerOperationsGroup -.-> docker/create("`Create Container`") docker/ContainerOperationsGroup -.-> docker/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/run("`Run a Container`") docker/ContainerOperationsGroup -.-> docker/start("`Start Container`") docker/ContainerOperationsGroup -.-> docker/stop("`Stop Container`") docker/ImageOperationsGroup -.-> docker/pull("`Pull Image from Repository`") docker/DockerfileGroup -.-> docker/build("`Build Image from Dockerfile`") docker/ContainerOperationsGroup -.-> docker/ls("`List Containers`") subgraph Lab Skills docker/create -.-> lab-415069{{"`How to access a web application running in a Docker container?`"}} docker/ps -.-> lab-415069{{"`How to access a web application running in a Docker container?`"}} docker/run -.-> lab-415069{{"`How to access a web application running in a Docker container?`"}} docker/start -.-> lab-415069{{"`How to access a web application running in a Docker container?`"}} docker/stop -.-> lab-415069{{"`How to access a web application running in a Docker container?`"}} docker/pull -.-> lab-415069{{"`How to access a web application running in a Docker container?`"}} docker/build -.-> lab-415069{{"`How to access a web application running in a Docker container?`"}} docker/ls -.-> lab-415069{{"`How to access a web application running in a Docker container?`"}} end

Understanding Docker Containers

Docker is a popular containerization platform that allows developers to package their applications and dependencies into isolated, portable, and reproducible environments called containers. Containers provide a consistent and reliable way to deploy and run applications, regardless of the underlying operating system or infrastructure.

What is a Docker Container?

A Docker container is a lightweight, standalone, and executable software package that includes everything needed to run an application: the code, runtime, system tools, libraries, and settings. Containers are created from Docker images, which are templates that define the contents of the container.

Benefits of Docker Containers

  1. Consistency: Docker containers ensure that the application runs the same way, regardless of the environment it's deployed in, eliminating the "it works on my machine" problem.
  2. Scalability: Containers can be easily scaled up or down, making it simple to handle fluctuations in application demand.
  3. Portability: Docker containers can be run on any system that supports Docker, making it easy to move applications between different environments.
  4. Efficiency: Containers are more lightweight and efficient than traditional virtual machines, as they share the host operating system's kernel.

Docker Architecture

Docker uses a client-server architecture, where the Docker client communicates with the Docker daemon, which is responsible for building, running, and managing Docker containers. The Docker daemon can run on the same machine as the client or on a remote machine.

graph LD subgraph Docker Architecture Client -- Communicates with --> Daemon Daemon -- Manages --> Containers Daemon -- Builds --> Images end

Docker Images and Containers

Docker images are the building blocks of containers. An image is a read-only template that defines the contents of a container, including the application code, runtime, system tools, libraries, and settings. Containers are the running instances of Docker images.

graph LR Image --> Container Image -- Defines --> Container

Installing and Running Docker

To get started with Docker, you need to install the Docker engine on your system. On Ubuntu 22.04, you can install Docker using the following commands:

sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker

Once Docker is installed, you can run a simple "Hello, World!" example by using the following command:

docker run hello-world

This command will download the hello-world image and run a container based on that image, displaying a message to confirm that Docker is working correctly.

Deploying a Web App in a Docker Container

In this section, we'll explore how to deploy a simple web application in a Docker container.

Building a Docker Image for a Web App

Let's assume we have a simple web application written in Python using the Flask framework. We can create a Dockerfile, which is a script that defines how to build a Docker image for our application.

Here's an example Dockerfile:

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]

This Dockerfile:

  1. Starts from the python:3.9-slim base image, which provides a minimal Python 3.9 runtime.
  2. Sets the working directory to /app.
  3. Copies the requirements.txt file to the working directory and installs the required Python dependencies.
  4. Copies the application code to the working directory.
  5. Specifies the command to run the application (python app.py).

To build the Docker image, run the following command in the same directory as the Dockerfile:

docker build -t my-web-app .

This will create a Docker image named my-web-app based on the instructions in the Dockerfile.

Running the Web App in a Docker Container

Once the Docker image is built, you can run the web application in a container using the following command:

docker run -p 5000:5000 my-web-app

This command:

  1. Runs a new container based on the my-web-app image.
  2. Maps the container's port 5000 to the host's port 5000, allowing you to access the web application from the host.

After running the command, you should be able to access the web application by opening a web browser and navigating to http://localhost:5000.

Containerizing Dependencies

One of the key benefits of Docker is the ability to package the application and its dependencies into a single, portable container. This ensures that the application will run consistently across different environments, regardless of the underlying system configuration.

By including the application code and its dependencies in the Docker image, you can be confident that the web application will run the same way in development, testing, and production environments.

Accessing the Web App from the Host

Now that we have a web application running in a Docker container, let's explore how to access it from the host machine.

Mapping Ports

When you run a Docker container, you need to map the container's ports to the host's ports to allow external access to the web application. In the previous example, we used the following command to run the container:

docker run -p 5000:5000 my-web-app

This command maps the container's port 5000 to the host's port 5000, allowing you to access the web application at http://localhost:5000.

Accessing the Web App

Once the container is running, you can access the web application from the host machine by opening a web browser and navigating to http://localhost:5000. This will connect to the web application running inside the Docker container.

Understanding the Port Mapping

The port mapping in the docker run command is formatted as host_port:container_port. This means that any traffic sent to the host's port 5000 will be forwarded to the container's port 5000, where the web application is listening.

You can also map multiple ports or use different port numbers for the host and container. For example, the following command maps the container's port 8080 to the host's port 80:

docker run -p 80:8080 my-web-app

In this case, you would access the web application at http://localhost.

Accessing the Web App from Other Hosts

If you want to access the web application from other hosts on the network, you can use the host machine's IP address instead of localhost. For example, if the host machine's IP address is 192.168.1.100, you can access the web application at http://192.168.1.100:5000.

Keep in mind that the host machine must be accessible from the other hosts on the network, and any necessary firewall rules must be configured to allow access to the mapped port.

Summary

By the end of this tutorial, you will have a solid understanding of how to access a web application running in a Docker container. You will learn to deploy a web app in a Docker container and utilize various techniques to access the application from your host machine. This knowledge will be valuable in your Docker-based web development and deployment workflows.

Other Docker Tutorials you may like