How to run a docker container on a server

You can leverage the power and flexibility of Opscode's Chef cookbooks to configure software on servers deployed with the Multi-Cloud Orchestrator. Should you come across configuration errors, the Multi-Cloud Orchestrator also enables you to debug and fix them.

Prerequisites

The only prerequisite is a simple custom cookbook that runs a docker container. The Multi-Cloud Orchestrator leverages Chef technology, allowing you to write and use modular and parametrized recipes to specify how to install and configure software on your servers. We will not cover cookbook development in this knowledge base; instead, for this example you will be using one that you can download from here; but extensive documentation on this topic can be found at https://docs.chef.io/cookbooks/.

Write and upload a cookbook

Once you have downloaded or written a very simple custom cookbook that runs a docker container:

  1. Go to Blueprint -> Cookbooks. Blueprint cookbooks
  2. Press the "Add Cookbook" button. Press "Upload cookbook" and select the custom cookbook. Upload cookbook dialog
  3. The cookbook will be processed and in a few seconds it should appear at the end of the cookbook list. Cookbook uploaded If your cookbook ends up in an error state, there may have been some errors processing it. Make sure your cookbook bundles a metadata.json file, as metadata.rb files are explicitly ignored due to security concerns. > If your cookbook does not bundle a metadata.json file, you may generate it from a terminal using Opscode ChefDK command knife. The specific command you can use to generate the file is knife cookbook metadata my_cookbook -o path/to/cookbooks with my_cookbook replaced with the name of your cookbook and path/to/cookbooks replaced with the path under which your cookbook directory is.

Create a template that uses a custom cookbook

  1. Go to Blueprint -> Templates. Blueprint templates
  2. Press the "Add template" button. Fill in the form with a Name for your template, select Ubuntu 20.04 Focal Fossa x86_64 as Generic Image and press the "Add template" button to create the template. New template
  3. In the templates table, click on the name of the template. Template created
  4. Go to the Cookbooks tab Template cookbooks
  5. Now in order to configure the use of the cookbook in the template, press the "Edit" button in the Cookbook list or Run list sections. Editing cookbooks of a template
  6. In the opening dialog, press "Add cookbook" and select the custom cookbook as source for template recipes that can be applied. Adding a cookbook to the template
  7. Press "Add recipe" and select the recipe in the cookbook you want the template to apply. If the recipe you are looking for is missing, it may be possible that you have forgotten to declare it in the cookbook's metadata.json file. Adding a cookbook recipe to the template
  8. Now you should edit the attributes that parametrize your cookbook recipes. If your cookbook declares its attributes in its bundled metadata.json file, you should see the attributes for it in the attributes tree: you can expand it by pressing the triangular arrows. Otherwise you can build the attribute tree you need by appending the right properties of the right types using the actions in the menu under the square that precedes each element or the empty objects / arrays. Attributes for the cookbook in the template
  9. For this example you should
    1. Set the value of the image attribute to alexwhen/docker-204,
    2. Add the string 80:80 to the ports array. In order to do this, click on the square that precedes the (empty array) under ports, then select open up the Append menu by click on its triangular arrow and select String. Adding array element In the new value of the array, type the value in. Attributes filled in
  10. Finally, press the "Update template button". Template recompiling The template will be recompiled and if no errors occur you can proceed to use it to deploy a server. Template ready

Create a server that uses the template

  1. Go to the Servers tab. Template servers
  2. Press the "Add server" button and then select "Add server". Add template server
  3. Fill in the opening form:
    1. Type in a name for your server,
    2. Do not select a VPC,
    3. Select a cloud account and the zone where you want to deploy the server,
    4. Select a server plan,
    5. Select the Default firewall as Firewall Profile, New server form
  4. Press the "Add server" button to create the server.
  5. Wait for your server status to become inactive and then press on the "Boot" action for the server. Boot template server
  6. The server will start booting. Server booting
  7. And then it will proceed to the bootstrapping state, during which software configuration is applied. Server bootstrapping
  8. Click on the server name. Server overview
  9. Then go to the Log tab to show the software configuration progress. Server logs during bootstrapping
  10. When the software configuration finishes without errors the server ends up in an operational state. In this example, however, a typo was introduced in the configuration attributes and it should end in the stalled state.

Debugging and fixing software configuration errors

  1. When a server fails to proceed from the bootstrapping state to the operational one, chances are there was some problem with one of the cookbooks or attributes. The logs of the software configuration process can give you the clue. Stalled server logs

  2. In this example, a typo was introduced in the dockerrun/image configuration attribute, and this is revealed by the following line:

    {"message":"pull access denied for alexwhen/docker-204, repository does not exist or may require 'docker login': denied: requested access to the resource is denied"}
    
  3. The correct value for the dockerrun/image configuration attribute is alexwhen/docker-2048. You are going to fix it on the template and then the server will have its configuration automatically reapplied. Go to the Overview tab of the server and click on the name of the template to access it. Stalled server overview

  4. Go to the Cookbooks tab. Examining the template attributes

  5. Press the "Edit" button on the Attributes section. This will open an edit form just for the attributes. Expand the attributes tree and change the dockerrun/image value to alexwhen/docker-2048. Editing the typo in the attributes

  6. Press the "Update Template" button. Then go back to your server by clicking on the Servers tab and then on your server's name. Then proceed to the Logs tab and wait for the software configuration to run again. This may take up to 15 minutes. Software configuration being reapplied

  7. This time it should be successful and some time after it finishes the server should proceed to the operational state. Software configuration successfully applied

  8. Go to the Overview tab and copy the Public IP of the server. Overview tab of operational server

  9. Paste it on your browser's URL bar and you should see the 2048 game you just deployed as a container on your server. 2048 game served by the container you deployed on a server

Additional Resources