Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

Webpage_Container_on_Azure.md 21 KB

You have to be logged in to leave a comment. Sign In

Website Running From Docker Container On An Azure Virtual Machine

Introduction And Background

Docker Logo

Find this and all the other lessons of this Docker Mastery journey in my Docker Mastery repo on DagsHub. DagsHub is the Cloud Git Repo system for data scientists and machine learning engineers. And just so you know, you can get a free DagsHub account. I hope you will look through the earlier lessons for this Docker Mastery Journey if you feel like you are missing something in this lesson.

The next step following from the last two lessons is to do the same thing we did in those lessons in Azure. I confess that I dreaded this step of the journey. The terrain is known to be more difficult by me and my friends - the Azure terrain. If a Microsoft person is reading, I am seeking to be constructive. The last two lessons covered how to serve a static website from a Docker container running in virtual machines on DigitalOcean and AWS. Those exercises went pretty smoothly. It wasn't horrible in Azure. It just seems that many things are a bit more challenging in Azure.

Azure is like DigitalOcean, AWS, and GCP in principle, but it has always struck me as being more difficult. In defense of Microsoft, I know they work with many entities that requires top security, and I think most of the challenges that I encounter are security related. Again, I only want to be matter of fact, and my experiences don't represent the whole of users. People I respect work with Azure and seem to like it OK. Overall, I've found Azure to not be as intuitive. However, the more I've worked with it, the more sense it begins to make.

As in the previous two lessons, in this lesson, we are only learning how to launch the same container we've been launching from a virtual machine. This time, we are doing that in Azure. This is ONLY a conceptual level lesson to help with this learning step. There's still more ground to cover for:

  • establishing a domain name for the page,
  • establishing greater security,
  • making the page dynamic,
  • adding a backend,
  • and adding a lot more standard website functionality,
  • creating an API,
  • building a test platform,
  • adding secure logins for users,
  • and many more things to make a complete functional website or SaaS.

The High Level Procedure

Some of you, who have never done this, may only need the high level procedure. This would be due to your overall experience with such things. Even if you need every detail, this high level procedure will help you. The high level steps of our procedure will also serve as the approximate or exact titles for each section that covers the procedural steps in detail.

Here are the high level procedural steps that we will follow.

  1. Create a free Azure account.
  2. Create a new directory within your Azure account.
  3. Create a virtual machine within your Azure account.
  4. Establish a secure shell connection between our local machine and our Azure VM.
  5. Use the command line of our VM from our local machine.
  6. Install the Docker engine and Docker Compose on our droplet.
  7. Setup SFTP between our local machine and our VM to transfer files from our local machine to the VM file system.
  8. SFTP our Dockerfile and our HTML file to our VM.
  9. Build our Docker image to create a Docker container on our VM.
  10. Run the Docker container in detached mode on the VM.
  11. Check to see that we can access our simple static website that is running in a Docker container on our Azure VM.

There's other important information at this point that I covered in the previous two lessons. I will ask you to review those lessons to get that information. However, one paragraph bears repeating here. Why is my procedure the way that it is? Two things. First, I REALLY like to find the simplest way to do something. Then, after I see how to do that, I am eager to add helpful sophistication. Second, I am now beginning to realize that I am a bit old school. I like the simple low level tools that have been around a long time. And trust me, there are steps that I take, that true computational elders would snicker at. The main thing I want to encourage you to do is to see my procedure conceptually. Then, over time, change this procedure to use step descriptions and tools that you prefer.

Create A Free Azure Account

Go to Azure to sign-up for your free account if you do not already have one. I will forgo going through the details on how to sign up for Azure. A good web search on how to sign-up for Azure will guide you well, but even the sign-up process itself will guide you. I will advise that it may be best to look at the latest YouTube materials that you can find in addition to Microsoft documentation.

Setting Up Your Virtual Machine

Once your account is setup, you'll see a top level screen something like that shown below.

Azure Home

We want to navigate to Products.

Azure Products

We want to create a virtual machine, so we click on that.

Azure Virtual Machines

I did elect to do the survey. I just finished it while writing this, and it was very thorough. After getting that dialog box for the survey out of my way, I clicked on "Try Virtual Machines Free".

Virtual Machine Account Types

I am cheap, so you know I took "Start free". I entered my information, and Azure took me to the next screen below.

Free Virtual Machine Sign Up

The "Go to the Azure portal" button took me to the following screen.

The Quick Start Center

I chose to deploy a virtual machine, which revealed the next screen.

Define an Azure Virtual Machine

At this point, I need to say that I hope what was happening to me does not happen to you. There were multiple areas that showed loading with little circular process indicators. I think they'd have gone on for a long time if I hadn't suspected I needed to do something different. I went back and realized that my name had been attached to many different accounts, and the last one that I had used with the email that I was using in this exercise had no azure setup. Once I changed that to the correct "directory", I was able to choose a "Subscription" and proceed with the virtual machine configuration.

Virtual Machine Data Group 1

The next window was part of my Azure woes. Azure is suggesting a machine and telling me it's unavailable for the region that I had to choose. Both DigitalOcean and AWS were pretty clear on what region I should pick and what resources I could pick. With Azure, I felt like I was on "Candid Camera" and people were laughing at me while watching me on camera. Maybe you're laughing at me now - it's OK. I know I am not the only one to encounter such Azure challenges. It felt like ... there's nothing to really compare it to I guess. I just wanted to create a virtual machine!

Virtual Machine Choice Issues

I clicked on see all sizes and got the following non helpful page.

Select a VM Size Issues

Then I went back and chose the little check box "Azure Spot instance" as shown in the next image. This gave me some other options that seemed reasonable for my experiment, so I chose those too. Notice that I planned to only use my already created ssh key. If you do not know how to use ssh keys, I strongly encourage you to do so. They are great. If you need conceptual help with ssh keys, reach out to me. I'll try to help, but they are truly great to use.

Trying Azure Spot Instance

The next screen just shows my other choices. I blurred out the shown portion of my public ssh key not that you could do anything with the portion shown.

SSH Key and Ports

Finally, I was able to choose all the above and move into Disk selection. I encourage you to explore the options available, but, since this was a simple test, I only chose an HDD, which was cheapest. I am also trying to keep this very inexpensive, since it's just a test of serving a simple page through a Docker container running on a virtual machine in the cloud.

Configure Virtual Machine Disk

I next went to networking. Again, please explore as you like, but I was aiming for default and/or simple.

Networking Setup For Virtual Machine

Next, I went to management. Again, I only made simple or default choices.

Management of Azure Virtual Machine

Enough of this. Let's create this virtual machine! I received an indication of what this machine would cost me to run it.

Pre Virtual Machine Creation Alerts

I am OK with that cost. Also, we can stop the and start the virtual machines that we create, so that we do not add up charges unnecessarily. I clicked "Create". While waiting for your virtual machine to be created, you will see something like the following.

Virtual Machine Deployment In Progress Screen

The creation and deployment happened in a reasonable amount of time. The next screen is like what you'd see when the virtual machine is complete.

Virtual Machine Deployment Complete

Now you can click "Go to resource" to find information and controls for your virtual machine. The most important thing to grab is your virtual machine's IP address.

Now that we have a virtual machine, we need to connect to it so that we can install Docker on it. Things were a little different here than they were for DigitalOcean and AWS, but they weren't bad. There were some other ways to connect to the virtual machine too that I'd like to go back to explore. However, I simply went the route for how to connect a terminal to the virtual machine using ssh. From my local Linux machine, the structure of the command to connect is: sudo then ssh then the -i to indicate that the next info is the path to the private file on my machine and finally a connection string of user@<ip_address>. My exact commmand is shown next.

sudo ssh -i /home/thom/.ssh/id_rsa thom@20.106.60.44

I next show the output to my terminal after connecting.

thom@thom-PT5610:~$ sudo ssh -i /home/thom/.ssh/id_rsa thom@20.106.60.44
[sudo] password for thom:          
The authenticity of host '20.106.60.44 (20.106.60.44)' can't be established.
ECDSA key fingerprint is SHA256:vQJN7dU/IoPYtd15vkKuIiwgbeZEx8e3Fh7wpgTjJ5o.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '20.106.60.44' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-1085-azure x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Mon Jun 20 00:32:55 UTC 2022

  System load:  0.0               Processes:           109
  Usage of /:   4.9% of 28.90GB   Users logged in:     0
  Memory usage: 3%                IP address for eth0: 10.0.0.4
  Swap usage:   0%


0 updates can be applied immediately.

New release '20.04.4 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Mon Jun 20 00:29:26 2022 from 159.118.152.110
thom@Web-Page-1:~$

Install The Docker Engine (And Docker Compose) On Our Droplet

This is the same procedure from the previous two lessons, so you can skip it if you've seen those.

Now we need to install Docker to our virtual machine in Azure.

For the command line commands in this section, we will not show the output lines of the terminal except for the last command. That command is a great way to verify our install. I want to provide THAT output so that you can detect if anything went wrong with your Docker installation. Let's run these next two commands on our droplet as good stewards of it.

sudo apt-get update

sudo apt-get upgrade

The next group of commands prepare our droplet for our Docker engine installation. Please see the learning step Docker Concepts and Install to understand the details of the install steps.

sudo apt-get install ca-certificates

sudo apt-get install curl

sudo apt-get install gnupg

sudo apt-get install lsb-release

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) stable nightly test" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Finally, we do an update for the new packages installed.

sudo apt-get update

This next line actually installs the Docker programs that we need to run the Docker engine.

sudo apt-get install docker-ce docker-ce-cli containerd.io

The next command line command tries to run the hello-world image to launch it in a container. It is not yet on our droplet, so Docker will then pull that image / container from Docker Hub, and then run it.

sudo docker run hello-world

The output on my Linux box for this command is shown below.

thom@Web-Page-1:~/web_page$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:13e367d31ae85359f42d637adf6da428f76d75dc9afeb3c21faea0d976f5c651
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

thom@Web-Page-1:~/web_page$ 

Docker compose is not yet installed. We do not need it for this step, but we will use it in the future. If you want to install it for the experience of it, the commands are below. Again, please see the learning step Docker Concepts and Install to understand the details of the install steps. I also like this tutorial by "did code" for explanations.

Obtain Docker Compose, install it, and set it up.

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Make sure it is executable.

sudo chmod +x /usr/local/bin/docker-compose

Some people like to create this symbolic link. You can determine if you need or want this.

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

A good way to check your Docker Compose install is to check the version from the command line with the following command.

docker-compose --version

You will see something similar to the following output if all is good.

docker-compose version 1.29.2, build 5becea4c

Connect FileZilla To Our Azure Virtual Machine

We do this to transfer files from our local machine and to the virtual machine. FileZilla is a great tool for doing this with secure shell FTP (secure shell file transfer protocol - SFTP). I recommend installing it from your Linux Distro's software management application. You can install FileZilla, or something like it, on Windows and Mac also. I used this guide on Docker Docs, How to Transfer Files to Droplets With FileZilla, to install FileZilla and begin to use it. Remember, when you are telling FileZilla where to find your private ssh key file, you are telling a program installed on your machine that is controlled by your security where to find your private key, so that your machine can connect with machines (such as droplets on DigitalOcean) that have your public ssh key.

Below, I show my setup for using it with my Azure virtual machine. This is much like we did with the other less connections, and you can see the history of those in the left side of the below window.

FileZilla Setup For Azure Virtual Machine

With this minimal amount of information entered, I was able to connect using SFTP mode in FileZilla.

FileZilla Connected To Azure Virtual Machine

For the next screen, I am trying to show how to create and move into a new directory, but I could not get my screen capture to allow the right click context menu to stay open. All you need to do is right click on your username under home in the above screen (mine behind thom of course), and chose the option to create and then move into that new directory. The next screen shows the dialog box that shows up. I called my new directory web_page as you can see.

Create A New Directory For Our Dockerfile and our Web Page File

With that created, we can simply highlight the Dockerfile and Hello_Docker.html file and drag them to the right side into our new directory. This completed action is shown next.

File Transfers From Local Machine To Azure Virtual Machine Complete

Building The Docker Image And Running It In A Docker Container

Docker is installed on our Azure virtual machine. Our Dockerfile is on our Azure virtual machine. The webpage that we want to launch is in the same directory as our Dockerfile. Now we want to run cd web_page to be in that directory and run our docker build command from the terminal that is connected via ssh to our virtual machine.

Build The Docker Image

I hope yours build as easily as mine did. Next we make sure the image was built and that no containers are yet running as shown below.

thom@Web-Page-1:~/web_page$ sudo docker images
REPOSITORY    TAG       IMAGE ID       CREATED              SIZE
website       0.1       136c486f1892   About a minute ago   142MB
hello-world   latest    feb5d9fea6a5   8 months ago         13.3kB
thom@Web-Page-1:~/web_page$ sudo docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
thom@Web-Page-1:~/web_page$ 

Great! Our image shows up. Now to run our image in a Docker container. As seen below, we obtain a long container id number indicating that our container is running the image that we built.

thom@Web-Page-1:~/web_page$ sudo docker run -p 80:80 -d -P website:0.1
9f0f9ffde124de37b6544178cb96d4d4f2bda6a9ee5f98ed2a5579a30d55bbaf
thom@Web-Page-1:~/web_page$

Now to test our web page using http://20.106.60.44. Dang, it didn't work! Why Azure - why? It just perpetually tries to connect and doesn't do it. A web search resulted in me finding

This Stackoverflow answer helped me feel like someone had known my same pain, and that someone had found a workable answer - Can't get Azure Virtual Machine to serve websites. Here's the answer from that StackOverflow question answer page that I used.

What fixed the issue for me was to go into the Azure Portal, browse to 'Network Security Groups', select the VM and then create an inbound rule to allow traffic to port 80.

After following the guidance from that answer, I was at the page indicated in the next image with the new rule. The "inbound rule to allow traffic to port 80" is the second line that starts with Priority 310. That line wasn't there when I first arrived. You only need to click the "Add" area and search for a way to add an inbound rule. Did that fix things?

Adding A Network Security Rule To Allow Our Website To Serve The World

YES! The first image below shows the page working in Chrome. The second image shows it working in Firefox.

Working From Chrome

Working From Firefox

Summary

While Azure threw some difficulties our way, it was possible to get around them. While I prefer the elegance and simplicity of DigitalOcean and AWS, Azure is not horrible. I simply wish it worked as smoothly as the other two.

Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...