Deploying Containers with VS Code

VS Code is an incredible tool. I haven’t been using for that long, maybe 4 or 5 months. There’s always something new to learn, new features and extensions. It integrates perfectly with lots of other appliances.

We’ll see how to deploy a Docker container image to Azure App Service, all from within VS Code.

I ran into a number of issues when trying this one out. I’ll share all of them. Maybe you ran into the same issues too.

This time, we’ll deploy a Django app. Django is another Python-based web framework.

I followed this tutorial to get the idea on how to create a Django app, but for this lab we’ll use the complete app found on Microsoft’s GitHub repo.

The same as before, we’ll clone the repository to our local machine.

git clone https://github.com/microsoft/python-sample-vscode-django-tutorial.git

Then we’ll move into the newly created directory.

cd .\python-sample-vscode-django-tutorial\

After creating our virtual environment, and installing the dependencies, we need to check, as always, if the app is running correctly locally. To run the Django app, we use the runserver command. I encountered a problem, however, when loading the application, A problem easily solved, because the output of the command tells us what we should do to fix it.

As you can see from the screen capture, it says I had 18 unapplied migrations. The command to apply the migrations is migrate, like that:

python manage.py migrate

Now we can safely load the app with the following command:

python manage.py runserver

Here you can see the output of both commands.

The manage.py file is the administrative utility for the project. We use it to run various commands for the project.

Dockerizing the application

Is dockerizing a valid word? It is now, surely.

We’ll now build the app’s Docker image. In Visual Studio Code, on the command palette, select Docker: Build Image. This will build the Docker image from a specified context. In this case, the path where our app is located. It will then prompt you to create a Dockerfile.

Now we run Docker Images: Push. We’ll choose the image we just built and the container registry where we’d like to store it. In my case, I pushed it to my Docker registry.

Did I forget to mention you need the Docker extension for VS Code?

From the Docker extension, under registries, you’ll see Azure and Docker Hub. Search for the image you just pushed, expand it and choose the latest one (you can tag your image with anything you want, but latest sounds like a great tag). Right-click on it an hit Deploy Image to Azure App Service.

Just follow the prompts to select a subscription, a globally unique name for your app’s site, a resource group (or create a new one), an app service plan, a pricing tier (there’s a free tier) and a region.

This process takes some time.

Now we’ll switch to the Azure extension. Our new app is in the App Service tab. Expand it, and right-click on Application Settings. Click on Add New Setting and the following parameter: WEBSITES_PORT (exactly like that, plural). And for the value, we’ll type 8000. That’s the port for the Django app.

When I first ran the application, I got an application error. It took me some time to figure out where the error was. The GUNICORN dependency was missing from the requirements.txt file. You just need to add it to the requirements file. There’s no need to install it. The other thing I had to fix was the CMD line in the Dockerfile. It mentions the wsgi file, which should be at the same level of the Dockerfile, In this case, it was sitting one folder below, inside web_project.

My Dockerfile now looked like this.

You also mustn’t forget to add the app’s URL to the ALLOWED_HOSTS in the settings.py file.

Now for the bad news. After all this, my app isn’t running. I keep getting the same DISALLOWED HOST error, even though everything’s correct, apparently. I couldn’t pinpoint where the error is.

As I mentioned in the beginning, I had tried this before and it worked. The difference is that in my first attempt I built the app from scratch, following the tutorial. For this deployment, I used the app from GitHub. I compared every step, but I’m missing something.

As you can see from above, the URL was added to the ALLOWED HOSTS.

These are two screen captures from my previous attempt.

I got the same mistake, but after adding the host to the settings.py file, the app loaded.

Any ideas? Let’s discuss!

Thank you for sticking with me this far!

UPDATE!

Two days have passed and I tried everything again, from the very beginning. I built the app from the ground up, added my URL to the ALLOWED_HOSTS parameter, but I still got the same mistake!

I’d love to hear the opinion of someone who knows about Django development. Anybody reading this post?

In the end, what I did was replace the URL with a *. Like that.

This means that ALL hosts will be allowed. I believe this might raise security concerns. The whole idea of the ALLOWED_HOSTS is to exactly. This security measure, according to Django’s site, is to prevent HTTP host header attacks.

This is just a test, so it should be OK. But if you’re running this app on a production environment, you should avoid it, and specify the host.

I’ll keep looking for a solution and I’ll let you know.

Thanks for passing by. See you soon!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s