Mounting Azure Blob Storage in Docker File

Viewed 5798

I am creating a dockerfile in Azure Devops and is trying to mount Azure Blob Storage Container (which contains files) into the dockerfile. I understand that there is help on microsoft regarding volume but they are in yaml format which is not what I suitable for me as I am using Azure Devops Pipeline to build my Image. I truly appreciate your help. Thanks in advance

Currently my code looks like this

FROM ubuntu:18.04

# Update the repository sources list
RUN apt-get update
FROM python:3.7.5
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive 
RUN apt-get install -y  build-essential python3-pip python3-dev postgresql-11 postgresql-contrib ufw sudo libssl-dev libffi-dev 
RUN apt-get install -y  libldap2-dev libsasl2-dev ldap-utils 
RUN apt-get install -y  systemd vim

# Install ODBC driver 17
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/ubuntu/18.04/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update -y
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql17 mssql-tools unixodbc-dev redis-server rabbitmq-server

# Install python libraries
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code
RUN pip install --upgrade pip && \
    pip install -r requirements.txt

RUN mkdir /home/workspace
WORKDIR /home/workspace

COPY ./ /home/workspace

# Mount
## *** REQUIRE HELP HERE ***##


RUN service postgresql start
CMD ["python", "home/workspace/python manage.py runserver 0:8000"]

1 Answers

The final runtime in which your container will be deployed can be of relevance because Azure provides you some options or not depending on where you actually deploy it.

Please, consider for instance this question in which I provided some help: as you can see, if you use docker compose and App Service, Azure brings you the opportunity of mounting a Azure File or Blob Storage in your container.

According to your comments you are using AKS. AKS and Kubernetes are a very specific use case. Instead of trying to use the AZ CLI to copy content from the storage account to your container, it would be preferable to take advantage of the mechanisms that Kubernetes provides and try to use volume mounts.

Please, consider for instance read this excellent article. The author exposes in two companion posts a very similar use case to yours, but with SQLite.

Basically, following the guidance provided in the abovementioned post, first, create a Kubernetes Secret with the Azure Storage connection data. You need to provide information about your storage account name and the value of one your access keys. The yaml configuration file will look similar to:

apiVersion: v1
kind: Secret
metadata:
  name: your-secret
  namespace: your-namespace
type: Opaque
data:
  azurestorageaccountname: Base64_Encoded_Value_Here
  azurestorageaccountkey: Base64_Encoded_Value_Here

Then, create the Kubernetes deployment and define the appropriate volume mounts. For example, assuming the name of your Azure Storage file share is your-file-share-name:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-azurestorage-test
  namespace: your-namespace
spec:  
  selector:
      matchLabels:
        app: your-app
  template:
    metadata:
      labels:
        app: your-app
    spec:
      containers:
      - name: your-app
        image: your-registry.azurecr.io/your-app:latest
        volumeMounts:
        - name: azurefileshare
          mountPath: /postgre-require-dmount-path
      imagePullSecrets:
      - name: your-pull-secret-if-you-have-one       
      volumes:
      - name: azurefileshare
        azureFile:
          secretName: your-secret
          shareName: your-file-share-name
          readOnly: false

The important things to note in the deployment are volumeMounts, used to specify the path in which the file share will be mounted inside the container, the appropriate for you PostgreSQL deployment, and volumes, with the azureFile entry, with information about the secret name created previously and the name of your actual Azure Storage File share.

Related