在Azure App Service上为Flask Web应用安装unixodbc-dev

问题描述:

我正在尝试在Azure应用服务上部署Flask Web应用.这个应用程式正在使用pyodbc连线至mssql;但是,当我部署应用程序时,出现此错误

I am trying to deploy Flask web app on the Azure App Service. This app is using pyodbc to connect to mssql; however, when I deploy the app I get this error

ImportError:libodbc.so.2:无法打开共享对象文件:否这样 文件或目录

ImportError: libodbc.so.2: cannot open shared object file: No such file or directory

我知道我可以通过以下行安装unixodbc/unixodbc-dev来解决此问题:

I got to know that I can solve this issue by installing unixodbc/unixodbc-dev with the following line:

sudo apt-get install unixodbc-dev

我尝试通过访问Kudu的SSH手动执行此操作;但是,问题仍然存在.

I tried to do this manually by accessing the SSH of Kudu; however, the issue still persists.

我正在使用本地存储库,并将带有git的代码推送到Azure.收到此错误后,我无法从Kudu访问控制台,因此我不得不注释代码并再次推送.然后,我尝试通过执行apt-get install unixodbc-dev手动安装此软件包(因为无法识别sudo),并且效果很好.然后,我再次用pyodbc推送了代码,并显示了相同的错误.我认为每次执行操作都会删除软件包,因为我发现在执行推送操作后,该软件包创建的某些文件夹会被删除.

I am using a local repository and pushing the code with git to Azure. When I get this error, I can't access the console from Kudu, so I had to comment the code and push again. Then I tried to install this package manually by doing apt-get install unixodbc-dev (because sudo is not recognized) and it works well. Then I pushed the code again with pyodbc on it and the same error is showing. I think the packages are being erased each time I do that since I found some of the folders created by this packages being deleted after the push action.

我总是得到的错误如下:

The error I always get is as follow:

根据您的描述,您正在使用基于Docker的Linux版Azure WebApp.因此,您在容器中所做的任何更改都将被写入container layer中,当删除容器时(包括停止/重新启动操作),该内容将被删除,作为正式的Docker文档

According to your description, you were using Azure WebApp for Linux which be based on Docker. So any changes you did in a container just be writen in the container layer which will be deleted when a contaner is deleted (includes stop/restart operation), as the offical Docker document About images, containers, and storage drivers said as below.

图像和图层

创建新容器时,可以在基础层之上添加新的可写层.该层通常被称为容器层" .对运行中的容器所做的所有更改,例如写入新文件,修改现有文件和删除文件,都将写入此薄的可写容器层.

When you create a new container, you add a new writable layer on top of the underlying layers. This layer is often called the "container layer". All changes made to the running container, such as writing new files, modifying existing files, and deleting files, are written to this thin writable container layer.

容器和层

容器和图像之间的主要区别在于可写层最高.在容器中添加新数据或修改现有数据的所有写操作都存储在此可写层中. 删除容器后,可写层也将删除.基础图像保持不变.

The major difference between a container and an image is the top writable layer. All writes to the container that add new or modify existing data are stored in this writable layer. When the container is deleted, the writable layer is also deleted. The underlying image remains unchanged.

因此,如果要将更改保存在运行的容器中,则必须通过命令 SSH support for Azure App Service on Linux

Therefore, if you want to save your changes in your running container, you must to commit these changes to create a new image via command docker commit. Or in your scenario for installing the required packages, you can follow the Azure documents SSH support for Azure App Service on Linux and Use a custom Docker image for Web App for Containers to add these commands as below into your Dockerfile to create a image to push and deploy it to Azure WebApp for Linux.

# Add unixodbc support
RUN apt-get update \
        && apt-get install -y --no-install-recommends unixodbc-dev