Search code examples
dockerdocker-composeazure-web-app-servicedocker-runazure-web-app-for-containers

My docker image is not seeing the enviornemnt variables when using the `--env` yet it sees with using docker-compose


I created a docker image of my OrchardCore project. I tried to run it using the following command

docker container run --detach --publish 8085:80 --name imageName MYIMAGE -e OrchardCore__DatabaseProvider=SqlConnection -e OrchardCore__Default__State='Uninitialized' -e OrchardCore__OrchardCore_DataProtection_Azure__ContainerName='dataprotection' -e OrchardCore__OrchardCore_Shells_Azure__ContainerName='hostcontainer' -e OrchardCore__ConnectionString='myConnectionString' -e OrchardCore__OrchardCore_DataProtection_Azure__ConnectionString='myBlobConnection' -e OrchardCore__OrchardCore_Shells_Azure__ConnectionString='myBlobConnection'

However, the image does not run. When I inspect the container logs, I see the following error

Unhandled exception. System.ArgumentNullException: The 'OrchardCore.Shells.Azure' configuration section must be defined (Parameter 'BlobShellStorageOptions')

The above exception is due to a missing configuration which is being set using the -e flag

Next I created the following docker-compose file and the image workes as expected with no errors! But the same image is not working when using the docker container run above or in

version: "3.9"
services:
    oc_app:
        image: "myimage:tag"
        container_name: "oc_app"
        ports:
            - "8000:80"
        depends_on:
            - db
        environment:
            OrchardCore__Default__State: "Uninitialized"
            OrchardCore__Default__TablePrefix: "Default"
            OrchardCore__OrchardCore_DataProtection_Azure__ConnectionString: "DefaultEndpointsProtocol=https;AccountName=<MyAccountName>;AccountKey=<MyKey>;EndpointSuffix=core.windows.net"
            OrchardCore__OrchardCore_DataProtection_Azure__ContainerName: "dataprotection-local"
            OrchardCore__OrchardCore_Shells_Azure__ConnectionString: "DefaultEndpointsProtocol=https;AccountName=<MyAccountName>;AccountKey=<MyKey>;EndpointSuffix=core.windows.net"
            OrchardCore__OrchardCore_Shells_Azure__ContainerName: "hostcontainer-local"
            OrchardCore__DatabaseProvider: "SqlConnection"
            OrchardCore__ConnectionString: "Server=db;Database=master;User=sa;Password=someStringPassword"
    db:
        image: "mcr.microsoft.com/mssql/server"
        container_name: sqlserver
        environment:
            SA_PASSWORD: "someStringPassword"
            ACCEPT_EULA: "Y"

Its like the app isn't seeing the variables passed by the --env or -e yet it's seeing the variables when passed using the environment section in the docker-compse.yml file.

What am I doing wrong here? How can I run my image using the docker container run command?

Note: the reason why I want to run the image using the docker container run is because I want to host the app on "Azure Web Service" and that passes the environment variables using the --env flag. After deploying my image to Azure Web Service, the container fails to run and shows the same error as the one I get when running docker run command locally.


Solution

  • Docker options need to go before the image name. Anything after the image name becomes a command for the image. So you need to do

    docker container run --detach --publish 8085:80 --name imageName -e OrchardCore__DatabaseProvider=SqlConnection -e OrchardCore__Default__State='Uninitialized' -e OrchardCore__OrchardCore_DataProtection_Azure__ContainerName='dataprotection' -e OrchardCore__OrchardCore_Shells_Azure__ContainerName='hostcontainer' -e OrchardCore__ConnectionString='myConnectionString' -e OrchardCore__OrchardCore_DataProtection_Azure__ConnectionString='myBlobConnection' -e OrchardCore__OrchardCore_Shells_Azure__ConnectionString='myBlobConnection' MYIMAGE