Azure functions - there is chance to install packages via apt-get? - azure

My azure functions needs some linux package to work, but i cant install them by apt-get, because i get error "sudo command not found" or "(Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied).
So my question is: There is any chance to install these packages without sudo?
Im using Bash in Azure App Service(kudu)

You can build and run your Azure Functions from a custom container. This way you can also install other packages.
This sample project does pretty much exactly that.
FROM mcr.microsoft.com/azure-functions/node:2.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
COPY . /home/site/wwwroot
# Install FFMPEG
RUN apt-get update && \
apt-get install -y ffmpeg
RUN cd /home/site/wwwroot && \
npm install
Source

Related

Install Linux dependencies in Azure App Service permanently using Azure DevOps

I created a CI/CD DevOps pipeline for deploying the Django app. After the deployment, manually I go to SSH in the azure app service to execute the below Linux dependencies
apt-get update && apt install -y libxrender1 libxext6
apt-get install -y libfontconfig1
After every deployment, this package is removed automatically. Is there any way to install these Linux dependencies permanently?
I suppose you are using Azure App Service Linux. Azure App Service Linux uses its own customized Docker image to host your application.
Unfortunately you cannot customize Azure Linux App Service Docker image, but you can use App Service Linux container with your own custom Docker image that includes your Linux dependencies.
https://github.com/Azure-Samples/docker-django-webapp-linux
Dockerfile example:
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim
EXPOSE 8000
EXPOSE 27017
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
# Install pip requirements
COPY requirements.txt .
RUN python -m pip install -r requirements.txt \
&& apt-get update && apt install -y libxrender1 libxext6 \
&& apt-get install -y libfontconfig1
WORKDIR /app
COPY . /app
RUN chmod u+x /usr/local/bin/init.sh
EXPOSE 8000
ENTRYPOINT ["init.sh"]
init.sh example:
#!/bin/bash
set -e
python /app/manage.py runserver 0.0.0.0:8000
https://learn.microsoft.com/en-us/azure/developer/python/tutorial-containerize-deploy-python-web-app-azure-01

Azure ACR Flask webapp deployment

I'm trying to deploy a Flask app on Azure by using the container registry. I used Az DevOps to pipeline building and pushing the image. Then I configure a web app to pull this image from ACR. But I'm running into one or the other error. The container is timing out and crashing and there are some errors in the application log as well. 'No module named flask'.
This is my Dockerfile
FROM python:3.7.11
ENV PATH="/root/miniconda3/bin:${PATH}"
ARG PATH="/root/miniconda3/bin:${PATH}"
RUN apt-get update
RUN apt-get install -y wget && rm -rf /var/lib/apt/lists/*
WORKDIR /app
ADD . /app/
RUN wget \
https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
&& mkdir /root/.conda \
&& bash Miniconda3-latest-Linux-x86_64.sh -b \
&& rm -f Miniconda3-latest-Linux-x86_64.sh
RUN python -m pip install --upgrade pip
RUN pip3 install Flask==2.0.2
RUN pip3 install --no-cache-dir -r /app/requirements.txt
RUN conda install python=3.7
RUN conda install -c rdkit rdkit -y
EXPOSE 5000
ENV NAME cheminformatics
CMD python app.py
I had to install miniconda because the rdkit package can be installed only from conda. I also add a PORT: 5000 key value to the configuration of the web app, but it hasn't loaded even once. I've been at this for 2 hours now. Previously, I've built images on local machine and pushed to dockerhub and ACR and was able to pull those images but it's the first time I used DevOps and I'm not sure what's going wrong here. Can someone please help?

Attempting to host a flutter project on Azure App Services using a docker image; local image and cloud image behave differently

I am having trouble with azure and docker where my local machine image is behaving differently than the image I push to ACR. while trying to deploy to web, I get this error:
ERROR - failed to register layer: error processing tar file(exit status 1): Container ID 397546 cannot be mapped to a host IDErr: 0, Message: mapped to a host ID
So in trying to fix it, I have come to find out that azure has a limit on uid numbers of 65000. Easy enough, just change ownership of the affected files to root, right?
Not so. I put the following command into my Dockerfile:
RUN chown -R root:root /usr/local/flutter/bin/cache/artifacts/gradle_wrapper/
Works great locally for changing the uids of the affected files from 397546 to 0. I do a command in the cli of the container:
find / -uid 397546
It finds none of the same files it found before. Yay! I even navigate to the directories where the affected files are, and do a quick
ls -n to double confirm they are fine, and sure enough the uids are now 0 on all of them. Good to go?
Next step, push to cloud. When I push and reset the app service, I still continue to get the same exact error above. I have confirmed on multiple fronts that it is indeed pushing the correct image to the cloud.
All of this means that somehow my local image and the cloud image are behaving differently.
I am stumped guys please help.
The Dockerfile is as below:
RUN apt-get update
RUN apt-get install -y curl git wget unzip libgconf-2-4 gdb libstdc++6 libglu1-mesa fonts-droid-fallback lib32stdc++6 python3 psmisc
RUN apt-get clean
# Clone the flutter repo
RUN git clone https://github.com/flutter/flutter.git /usr/local/flutter
# Set flutter path
ENV PATH="/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin:${PATH}"
# Enable flutter web
RUN flutter upgrade
RUN flutter config --enable-web
# Run flutter doctor
RUN flutter doctor -v
# Change ownership to root of affected files
RUN chown -R root:root /usr/local/flutter/bin/cache/artifacts/gradle_wrapper/
# Copy the app files to the container
COPY ./build/web /usr/local/bin/app
COPY ./startup /usr/local/bin/app/server
COPY ./pubspec.yaml /usr/local/bin/app/pubspec.yaml
# Set the working directory to the app files within the container
WORKDIR /usr/local/bin/app
# Get App Dependencies
RUN flutter pub get
# Build the app for the web
# Document the exposed port
EXPOSE 4040
# Set the server startup script as executable
RUN ["chmod", "+x", "/usr/local/bin/app/server/server.sh"]
# Start the web server
ENTRYPOINT [ "/usr/local/bin/app/server/server.sh" ]```
So basically we have made a shell script to build web BEFORE building the docker image. we then use the static js from the build/web folder and host that on the server. No need to download all of flutter. Makes pipelines a little harder, but at least it works.
New Dockerfile:
FROM ubuntu:20.04 as build-env
RUN apt-get update && \
apt-get install -y --no-install-recommends apt-utils && \
apt-get -y install sudo
## for apt to be noninteractive
ENV DEBIAN_FRONTEND noninteractive
ENV DEBCONF_NONINTERACTIVE_SEEN true
## preesed tzdata, update package index, upgrade packages and install needed software
RUN echo "tzdata tzdata/Areas select US" > /tmp/preseed.txt; \
echo "tzdata tzdata/Zones/US select Colorado" >> /tmp/preseed.txt; \
debconf-set-selections /tmp/preseed.txt && \
apt-get update && \
apt-get install -y tzdata
RUN apt-get install -y curl git wget unzip libstdc++6 libglu1-mesa fonts-droid-fallback lib32stdc++6 python3 python3 nginx nano vim
RUN apt-get clean
# Copy files to container and build
RUN mkdir /app/
COPY . /app/
WORKDIR /app/
RUN cd /app/
# Configure nginx and remove secret files
RUN mv /app/build/web/ /var/www/html/patient
RUN cd /etc/nginx/sites-enabled
RUN cp -f /app/default /etc/nginx/sites-enabled/default
RUN cd /app/ && rm -r .dart_tool .vscode assets bin ios android google_place lib placepicker test .env .flutter-plugins .flutter-plugins-dependencies .gitignore .metadata analysis_options.yaml flutter_01.png pubspec.lock pubspec.yaml README.md
# Record the exposed port
EXPOSE 5000
# Start the python server
RUN ["chmod", "+x", "/app/server/server.sh"]
ENTRYPOINT [ "/app/server/server.sh"]

Azure: How to create an environment where the VM has a special package installed?

I am about to deploy a model on Azure but the model needs a special package installed on Ubuntu. My model is written in python and I have a python-wrapper installed (and other necessary pip packages) already in the environment.
The challenge is that the wrapper needs the special package to be installed on the Ubuntu. How and at what point I need to specify what packages I want to be installed on Ubuntu when creating the environment? The package is not a default one.
The following code snippet helped me to solve this. Just substitute the package you want to install into "<'package-1'>".
FROM <prebuilt docker image from MCR>
# Switch to root to install apt packages
USER root:root
RUN apt-get update && \
apt-get install -y \
**<package-1>** \
...
<package-n> && \
apt-get clean -y && \
rm -rf /var/lib/apt/lists/*
# Switch back to non-root user
USER dockeruser
The complete tutorial can be found here: https://learn.microsoft.com/en-us/azure/machine-learning/how-to-extend-prebuilt-docker-image-inference

Running SSH script during Microsoft Azure Web App deployment

I am deploying a web app using the Python-Django framework to Microsoft Azure.
I have succeeded in deploying it, but every time I deploy, I have to open the Azure SSH tool and run the command apt-get install libgtk2.0-dev which I gather is some Linux dependency for the opencv-python image processing library.
I wonder if there is a way to install the required software using deploy.sh files.
deploy.sh
echo "Running Linux Deployment Script..."
apt-get update && apt install -y libxrender1 libxext6
apt-get install -y libfontconfig1
apt-get install libgtk2.0-dev
Thanks in advance for your help.
You can create a script to install libgtk2.0-dev, say test.sh under /home/site.
And then add an app setting under 'Configuration' called PRE_BUILD_SCRIPT_PATH with /home/site/test.sh as the value.
You can run a script on every Webapp startup. Just adjust your script as described here: https://stackoverflow.com/a/69923647/2606766
Create a start.sh file, e.g. like this:
# install package & start app
apt-get update -y
apt install -y libxrender1 libxext6
apt-get install -y libfontconfig1
apt-get install libgtk2.0-dev
# don't forget to start your webapp service at the end of this script, e.g.:
python manage.py runserver
Set it as your startup script:
Note: There are two pitfalls to this approach:
The script must be executable, so either install w/ unix and chmod 755 start.sh or use a git command (see SO).
The packages are installed on every startup, thus you depend on external servers/repositories when starting the webapp.
You can set SCM_POST_DEPLOYMENT_ACTIONS_PATH environment variable to configure a folder. All scripts in this folder will be executed after deployment. As far as I can see this should work both on Windows and Linux.
If you need root permissions then I would suggest to use a custom docker container which has these packages already installed.
You can start by adding this command directly to the startup script, As mentioned in the answer by #HeyMan. But instead of adding a file just add the command there apt-get update && apt install -y libxrender1 libxext6 && apt-get install -y libfontconfig1 && apt-get install libgtk2.0-dev
Add this command in a single line there.
If this method also does not work for you, then you should follow the container based approach.
Create a dockerfile and add all the required dependency there.
docker build to create a docker image.
Push that image to Azure container registry using docker push
Instead of deploying from local git, deploy with help of docker.
Look at this link for help
https://learn.microsoft.com/en-us/azure/container-registry/container-registry-get-started-docker-cli?tabs=azure-cli

Resources