[英]How to create a Dockerfile for strealit app
I am aiming to deploy a web-app written with Sreamlit,我的目标是部署一个用 Sreamlit 编写的网络应用程序,
I can run this on my local machine by running streamlit run Home.py in my command line.我可以通过在命令行中运行 streamlit run Home.py 在本地机器上运行它。
However, I'm not sure how to create the docker file.但是,我不确定如何创建 docker 文件。
Any advice?有什么建议吗?
Try something like this.尝试这样的事情。
FROM python:3.7
# Expose port you want your app on
EXPOSE 8080
# Upgrade pip and install requirements
COPY requirements.txt requirements.txt
RUN pip install -U pip
RUN pip install -r requirements.txt
# Copy app code and set working directory
COPY . .
WORKDIR /app
# Run
ENTRYPOINT [“streamlit”, “run”, “Home.py”, “–server.port=8080”, “–server.address=0.0.0.0”]
This is a docker file that would work for you:这是一个适合您的 docker 文件:
FROM python:3.9
WORKDIR /app
COPY requirements.txt ./requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
EXPOSE 8201
ENTRYPOINT ["streamlit", "run"]
CMD ["app/Home.py"]
This is a series of instructions and each instruction creates a layer.这是一系列指令,每条指令创建一个层。 The base image here is
python:3.9
where you create a directory called /app
, then copy and install everything in your requirements.txt
.此处的基本映像是
python:3.9
,您可以在其中创建一个名为/app
的目录,然后复制并安装您的requirements.txt
中的所有内容。 Then copy your entire folder over and finally specify the command to run within the container.然后复制整个文件夹,最后指定要在容器中运行的命令。 When you run the container it will be exposed on port
8201
.当您运行容器时,它将在端口
8201
上公开。
Build and run the container with the following if you are going for a local set up.如果您要进行本地设置,请使用以下内容构建并运行容器。 Run the commands from within the directory your
Dockerfile
is.从
Dockerfile
所在的目录中运行命令。
docker build -f Dockerfile -t app:latest .
docker run -p 8201:8201 app:latest
Now you will be able to access the container on your browser at http://localhost:8201/
现在您将能够在浏览器上通过
http://localhost:8201/
访问容器
In order to Dockerise your app you need two files:为了对您的应用进行 Dockerise,您需要两个文件:
Dockerfile
) Dockerfile
)创建一个容器The answer of Javier Roger is providing a very minimal Dockerfile that seems to work: Javier Roger的答案是提供了一个非常小的 Dockerfile,它似乎可以工作:
FROM python:3.7
# Expose port you want your app on
EXPOSE 8080
# Upgrade pip and install requirements
COPY requirements.txt requirements.txt
RUN pip install -U pip
RUN pip install -r requirements.txt
# Copy app code and set working directory
COPY . .
WORKDIR /app
# Run
ENTRYPOINT [“streamlit”, “run”, “Home.py”, “–server.port=8080”, “–server.address=0.0.0.0”]
If you combine this answer with the following docker-compose then you have a nice container containing your app:如果您将此答案与以下 docker-compose 结合使用,那么您将拥有一个包含您的应用程序的漂亮容器:
services:
streamlit:
container_name: "The name you want to have your container"
build:
dockerfile: ./Dockerfile
context: ./
ports:
- 'The port you want your app to have:8080'
Please note that both Dockerfile
and docker-compose.yml
should be placed in the root of your app.请注意,
Dockerfile
和docker-compose.yml
都应该放在应用程序的根目录中。
To run the app just cd
to the root dir and run:要运行应用程序,只需
cd
到根目录并运行:
docker compose up -d
if docker compose is not installed please use:如果未安装 docker compose,请使用:
sudo apt-get update
sudo apt-get install docker-compose-plugin
In case that you cannot install it so easily use this link如果您无法轻松安装它,请使用此链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.