Showing posts with label Docker. Show all posts
Showing posts with label Docker. Show all posts

Friday, September 13, 2019

Build locally a docker image and deploy it to an Azure Web App for containers

Topic

this post shows how to locally build a Docker image on Windows 10 Home version, push it to an Azure Container Registry, then, deploy it manually to a Web App for containers so as it can be consume.

Prerequisites

you must have:
  • an access to the Azure Portal
  • an available Azure Container registry (if not do this quick tutorial to have one)
  • Docker Toolbox
    On Windows 10 home version you won't be allowed to install Docker desktop by the Docker installation program but it will install Docker Toolbox that is enough to do the main tasks.

Building the docker image locally

If you have a github account fork the following repo and clone it on your computer, if not, download the code:
acr-build-helloworld-node



Let's examine the content a bit. First it is a real Node.js application even if it's render only "Hello World". It is a good base for future developments for an API and a good way to test the deployment as a container.



we can see next the files that compose the image. the Dockerfile-base:



The Dockerfile-app:



and most of all the Dockerfile, the only file we really need to build locally a docker image. It tells Docker to:
  • build a machine based on Linux alpine with Node engine included.
  • copy all the files in src folder
  • change directory to src folder and perform an NPM install
  • and the node excution for the API is dedicated to the file server.js located in the src folder
Five lines of code an we have a complete environment running an API! Remember how long it took to have the same with a VM...



Now is the time to create the image, so open a command prompt and change directory to the one where is the API code and execute the following:

docker build -t nodeapp .


The image is mounted. We can see docker doing all the jobs described previously:



then we can run the image:

docker run -p 8888:80 nodeapp 




for testing the image locally, because we are using Docker Toolbox, we need the IP to request. It is in the Docker Toolbox command:



As we have set the port to 8888 we can test the running image locally:



Last we can execute a command to list the containers and see hte latest one

docker container ls




Pushing Container to Azure

Now is the time to connect to the Container Registry where to push our Container



this is the command to do it

docker login <your Container registry name>


You are helped by the Container Registry Quick Start:





If you need the credentials you can find them here:



Now, we have to define the container before pushing it:


docker tag nodeapp <your Container registry name>.azurecr.io/nodeapp




then we start the push

docker push <your Container registry name>.azurecr.io/nodeapp



And it's amazing we are really seeing the container being pushed to Azure piece by piece:







And finally it's done:



And we can see the push result in Azure Container registry:



Deploying the container to a Web App for Containers

Now if we want our container to run and display our API we can, for example, deploy it to an Azure Web App for Containers

For doing so, navigate to the container and open the latest push features by clicking on the dots and select "Deploy to web app":



Name your container, chose subscription and resource group, select a service plan. I have one for Linux and had to locate it in North Europe because it is one of the region available for the Linux Web App for containers and we can have only one Free Plan for Linux Web App for Containers. So no choice for me.



After having clicked on the create button, we can check the deployment setps:



And finally it's deployed



We can go to the Overview page of the Web App for Containers where our Linux Alpine container with the Node.js API is deployed:



And we can test it works properly by clicking on the Url:



Well done!

Thursday, September 12, 2019

Troubleshoot Docker registry login timeout issue for Docker Toolbox

topic

Solve error:
docker registry login net/http: request canceled while waiting for connection (Client.Timeout exceeded...
especially
docker azure registry login net/http: request canceled while waiting for connection (Client.Timeout exceeded..
when wanting to log to a Docker registry while using the Docker tool box.

Introduction

If you want to work with Docker using a Windows 10 OS, Home version, you won't be allowed by the installation program to install Docker desktop. Docker desktop needs Hyper V and although Hyper V is now available even on Windows 10 Home, the Docker desktop installation program still checks only the OS version. If it doesn't find a Pro or an Enterprise version on your computer, it stops installation.
Fortunately you can performs the main Docker tasks using Docker tool box.

Error reason

The "Client.Timeout exceeded" issue while attempting to login to a Docker registry is due to a bad network configuration of your Docker Toolbox. When installing Docker Toolbox a default Virtual Box VM is also installed to your computer with 2 networks adapters. It is located at: drive\user\.docker\machine\machines\default



you can open it with Oracle Virtual Box that is also installed with the Docker Toolbox. If you open Settings and Network you will see the 2 adapters.



Warning: If you play with the virtual box and close it, you will close also the connection to your running containers. It is like disable the network adapter that allows you to connect to the running containers and test them locally. If you have this issue, close the docker Toolbox and restart it. A new connection and IP address will be reallocated:









Troubleshouting the issue

For troubleshooting the timeout issue you have to configure the Virtual Box Network Adapters.

Open the settings of Windows 10
Go to Network & Internet settings:



then, Change adapter options



then double click on VirtualBox Host-Only Network
Properties
Select Internet Protocol Version 4
Open properties
Set the DNS to 8 8 8 8

It should fix the timeout issue.



do the same for VirtualBox Host-Only Network # 2

It seems that it has solved also connection troubles for the Docker Extension of Visual Studio Code.