Container installation and execution

We need to install and run two containers, one for the frontend and another for the backend. The frontend one is a static application, and does not need to be compiled, so we will get it directly from the Git repository, but you can just copy-paste the directory as well. For the backend project, you get the source code, and build it with dotnet core using dotnet publish.

When we have our package ready, either static pages or compiled binaries (as a result of dotnet publish, for example), we need to first build the Docker images for the new container. Then, we need to run the newly built docker image as a running container.

Running the frontend container

To run a new container, here, we will first download the source code from source control. In this example, we are cloning a Git repository, building the docker image and then executing the docker container based on the image we just built. Follow these steps in order to run our frontend container.

Note that I have generated my RSA private key, and added it online to my bitbucket login as well as in the SSH in the VM, using the ssh-keygen and ssh-add commands. If you want to simulate the same steps on your own repository, then please see more information here: https://confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html:

  1. You can download the source code or do a Git clone.
  2. Current directory: /home/centos/containerization/app.
  3. Online Git repository: https://bitbucket.org/packt_ea_net_core/chapter11_cc_fe.
  4. Git clone--[email protected]:packt_ea_net_core/chapter11_cc_fe.git fe.
  5. We named the newly created folder/directory named as fe.
  6. In the index.html page, remember to change the URL inside the html file from http://eaaagents.eastus.cloudapp.azure.com:8081 to http://localhost:8081 so that it's accessible to be tested locally from the local (port forwarded) VM.
  7. Build the Docker image like this:
   docker build . -t mathappfe
  1. Run the Docker container from the image just built as follows:
   docker run --name mathappfe-container -p 8080:80 mathappfe
  1. You can now access http://localhost:8080 on your host machine hosting the VM, and you will see the response (as we have already forwarded this port in VirtualBox), as seen in this screenshot:
Output of a frontend microservice container

Running the backend container

Again, to run a new container we will first clone the Git repository, build the image and execute the container. Here, since our backend container is a .NET Core based code, we will need to compile the C# code as well. Follow these steps in order to run our backend container:

  1. You can download the source code or do a Git clone.
  2. Current directory: /home/centos/containerization/app
  3. Online Git repository: https://bitbucket.org/packt_ea_net_core/chapter11_cc_be
  4. Clone the git repository using the following command git clone: [email protected]:packt_ea_net_core/chapter11_cc_be.git be
  5. We named the new folder name be, and all the source can be found at: /home/centos/containerization/app/be.
  6. For the backend case, the source code is C# based on .NET Core, and we did not install the .NET core build environment on our VM; so, in this case, we will build our source code in to the binary package from within Windows (host machine) with visual studio CE 2015 installed (though, in my case, I have VS also installed in a separate Windows VM).
  7. Now run the following command under the project directory:
   dotnet publish
Note that, from the source code, you could see the presence of the Docker file; for this to publish from within Visual Studio, you need to have Docker client installed on the same machine; otherwise, like me, you create a published package from the CLI.
  1. In this case, we published the folder to the Windows host, and transferred to our VM using WinSCP at this location: /home/centos/containerization/app/dynamic/mathwebapi_publish
  2. The flow of these commands looks as seen in the following screenshot:
Building the Docker container image from dotnet published directory
  1. From the preceding screenshot, you can notice that docker images resulted in only one image, which is installed in this VM. Executing the docker build will create a new docker container image for our .NET Core-based backend WebAPI microservice:
        docker build -t mathwebapi mathwebapi_publish

Take a look at the following screenshot:

Building the docker container image from dotnet published directory
  1. Run the docker container from the image just built, as follows:
        docker run -it -d -p 8081:80 mathwebapi

Take a look at the following screenshot:

Output from docker run command

You can now access http://localhost:8080 on your host machine (which hosts the VM), and you will see the response (as we have already forwarded this port in VirtualBox).

You can also verify the output using curl, which will basically call the API directly, as follows:

curl "http://localhost:8081/api/math?a=1&b=11"

Executing our two static and .NET Core-based microservices inside the two Docker containers that are inside Linux VM, we get an output similar to this screenshot:

Successful output of executing two microservices (locally)

We have now successfully verified executing both the containers locally. The next step is to prepare and achieve the same results from the Azure cloud.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.10