How to run a custom application in a Docker container with a custom port?

DockerDockerBeginner
Practice Now

Introduction

Docker is a powerful platform for building, deploying, and running applications in a containerized environment. In this tutorial, you will learn how to run a custom application in a Docker container and expose a custom port for external access. By the end of this guide, you will have a solid understanding of how to leverage Docker to deploy your applications with greater flexibility and control.


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/port("`List Container Ports`") 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-410106{{"`How to run a custom application in a Docker container with a custom port?`"}} docker/ps -.-> lab-410106{{"`How to run a custom application in a Docker container with a custom port?`"}} docker/port -.-> lab-410106{{"`How to run a custom application in a Docker container with a custom port?`"}} docker/run -.-> lab-410106{{"`How to run a custom application in a Docker container with a custom port?`"}} docker/start -.-> lab-410106{{"`How to run a custom application in a Docker container with a custom port?`"}} docker/stop -.-> lab-410106{{"`How to run a custom application in a Docker container with a custom port?`"}} docker/pull -.-> lab-410106{{"`How to run a custom application in a Docker container with a custom port?`"}} docker/build -.-> lab-410106{{"`How to run a custom application in a Docker container with a custom port?`"}} docker/ls -.-> lab-410106{{"`How to run a custom application in a Docker container with a custom port?`"}} end

Introduction to Docker and Containers

Docker is a popular open-source platform that enables the development, deployment, and management of applications using containers. Containers are lightweight, standalone, and executable software packages that include everything an application needs to run, including the code, runtime, system tools, and libraries.

What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.

Benefits of Using Docker

  1. Consistency: Containers ensure that your application will run the same way, regardless of the environment it is deployed in.
  2. Scalability: Containers can be easily scaled up or down to meet the demands of your application.
  3. Portability: Containers can be run on any system that has Docker installed, making it easy to move your application between different environments.
  4. Efficiency: Containers are lightweight and use fewer resources than virtual machines, making them more efficient to run.

Docker Architecture

Docker uses a client-server architecture, where the Docker client communicates with the Docker daemon, which is responsible for building, running, and distributing Docker containers.

graph LD subgraph Docker Architecture client((Docker Client)) daemon((Docker Daemon)) registry((Docker Registry)) client --> daemon daemon --> registry end

Docker Containers

Docker containers are the basic building blocks of Docker. A container is a standard unit of software that packages up code and all its dependencies, so the application runs quickly and reliably from one computing environment to another.

graph LD subgraph Docker Container app((Application)) runtime((Runtime)) libs((Libraries)) bin((Binaries)) end

Docker Images

Docker images are the basis for containers. An image is a lightweight, standalone, executable package of software that includes everything needed to run an application: the code, a runtime, libraries, environment variables, and configuration files.

Running a Custom App in Docker

Building a Custom Docker Image

To run a custom application in a Docker container, you first need to create a Docker image for your application. This can be done using a Dockerfile, which is a text document that contains all the commands a user could call on the command line to assemble an image.

Here's an example Dockerfile for a simple Flask web application:

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
  2. Sets the working directory to /app
  3. Copies the requirements.txt file and installs the required Python packages
  4. Copies the application code to the container
  5. Specifies the command to run the application (python app.py)

Building and Running the Docker Image

Once you have the Dockerfile, you can build the Docker image using the docker build command:

docker build -t my-flask-app .

This will create a new Docker image with the tag my-flask-app.

To run the application in a Docker container, use the docker run command:

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

This will start a new container based on the my-flask-app image and map port 5000 on the host to port 5000 in the container.

Verifying the Application

You can now access the running application in your web browser by visiting http://localhost:5000. You should see the output of your Flask application.

Exposing a Custom Port

By default, when you run a Docker container, it is isolated from the host system and its network. If your application is running on a specific port inside the container, you need to expose that port to the host system so that you can access the application from outside the container.

Exposing a Port

To expose a port from a Docker container, you can use the -p or --publish flag when running the docker run command. The syntax is:

docker run -p <host_port>:<container_port> <image_name>

For example, if your application is running on port 8000 inside the container, you can expose it to port 80 on the host system like this:

docker run -p 80:8000 my-app

This will map port 80 on the host system to port 8000 inside the container.

Verifying the Exposed Port

You can verify that the port has been exposed correctly by checking the output of the docker run command, which should show the mapped ports:

$ docker run -p 80:8000 my-app
 * Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)

Alternatively, you can use the docker port command to list the mapped ports for a running container:

$ docker port <container_id>
8000/tcp -> 0.0.0.0:80

This shows that port 8000 inside the container is mapped to port 80 on the host system.

Now, you can access your application by visiting http://localhost (or the IP address of the host system) in your web browser.

Summary

In this tutorial, you have learned how to run a custom application in a Docker container and expose a custom port for external access. By containerizing your application, you can ensure consistent and reliable deployments, as well as the ability to scale and manage your application more efficiently. Docker's containerization technology provides a powerful platform for developers to build, ship, and run their applications in a more streamlined and efficient manner.

Other Docker Tutorials you may like