How can a Python application be deployed using Docker?

To deploy a Python application, you can package the application into a container using Docker. Here is a simple set of steps:

  1. File used to create and build a Docker container.
# 使用Python 3.9作为基础镜像
FROM python:3.9

# 设置工作目录
WORKDIR /app

# 复制应用程序文件到容器中的位置
COPY . /app

# 安装应用程序所需的依赖项
RUN pip install --no-cache-dir -r requirements.txt

# 暴露应用程序使用的端口(如果有)
EXPOSE 8000

# 运行应用程序
CMD ["python", "app.py"]
  1. List of requirements
Flask
  1. Run the following command in the root directory of the application to build the Docker image.
docker build -t my-python-app .

my-python-app is the name you have given to the image, with “.” representing the current directory where the Dockerfile is located.

  1. After the construction is completed, run the container using the following command:
docker run -d -p 8000:8000 my-python-app

– The option -d indicates running the container in the background, -p 8000:8000 indicates mapping port 8000 of the container to port 8000 on the host machine, and my-python-app is the name you previously gave to the image.

Your Python application should now be successfully running in a Docker container. You can access the application by visiting http://localhost:8000. If the application requires any other ports, make sure to map them accordingly.

bannerAds