Jump toUpdate content

Getting started with the Docker InstantApp

Reviewed on 10 May 2021Published on 13 May 2019
  • compute
  • gaming
  • server
  • apps
  • docker

Getting Started with the Docker InstantApp

This page shows you how to use the Docker InstantApp on your cloud instance.

Requirements:

Creating and Starting a New Cloud Instance Using the Docker Instantapp

Before starting, click the Create an Instance button in the management console.

You will land on the server-creation page where you can choose the Docker image in the InstantApps tab for your Cloud Instance:

Choose the instance type, enter additional parameters like tags and click the Create a new instance button. This action starts your instance with Docker pre-installed.

Connecting to Your Instance

Connect to your instance via SSH. To do so, you must have configured an SSH key pair on your local computer and uloaded the public key to your management console.

On Windows, you can use Putty to connect to your instance.

On Linux or MacOs, you can open a terminal and type the following command to connect to your instance:

john@localhost:~$ ssh -i ~/.ssh/your_private_key root@your_instance_ip

Once logged in, you will be greeted by the following screen showing some parameters about the instance and the Docker version already pre-installed on the instance:

Building your First Container

Docker allows you to ship applications in containers. Containers are standardized units of software that bundle the applications code and all its dependencies into one package, so the application runs quickly and reliably from one computing environment to another.

Docker can build images automatically by reading the instructions from a Dockerfile. This is a text document that contains all the commands a user would execute on the command line to assemble an image.

Create a new Dockefile for a small Python web application with Flask, that will run on the instance:

  1. Create a new directory to develop your environment

    mkdir my-application
  2. Enter into the directory

    cd my-application
  3. Create a new file, called Dockerfile, copy & paste the following content into it and save the file

    # Use an official Python runtime as a parent image
    FROM python:3.7-slim

    # Set the working directory to /app
    WORKDIR /app

    # Copy the current directory contents into the container at /app
    ADD . /app

    # Install the Flask framework
    RUN pip install --trusted-host pypi.python.org Flask

    # Make port 80 available to the world outside this container
    EXPOSE 80

    # Define environment variable
    ENV NAME World

    # Run app.py when the container launches
    CMD ["python", "app.py"]
  4. Now as the Dockerfile is ready, it is time to create the application. Open a text editor, copy paste the following content and save the file as app.py.

    from flask import Flask
    import os
    import socket

    app = Flask(__name__)

    @app.route("/")
    def hello():

    html = "<h1>Hello {name}!</h1>" \
    "<b>Hostname:</b> {hostname}<br/>"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(),)

    if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80)

    The application will run a simple web server on port 80 and return the values of the environment variable NAME set in the Dockerfile, as well as the hostname of the container when it is running.

  5. Build the image. The -t tag is used to set a friendly name for the image:

    docker build -t helloworld .

    Docker will now download the python base image and process the tasks defined in the Dockerfile:

    Sending build context to Docker daemon  18.43kB
    Step 1/7 : FROM python:3.7-slim
    3.7-slim: Pulling from library/python
    743f2d6c1f65: Already exists
    977e13fc7449: Pull complete
    de5f9e5af26b: Pull complete
    0d27ddbe8383: Pull complete
    228d55eb5a23: Pull complete
    Digest: sha256:219796fa5fe743d1f3fb15b568037b984f211acd5325d4dcf357339bdbcbe97c
    Status: Downloaded newer image for python:3.7-slim
    ---> ca7f9e245002
    Step 2/7 : WORKDIR /app
    ---> Running in 331321ef0b98
    Removing intermediate container 331321ef0b98
    ---> 26eb64400af2
    Step 3/7 : ADD . /app
    ---> e4e9c2e39897
    Step 4/7 : RUN pip install --trusted-host pypi.python.org Flask
    ---> Running in c16f61d74ce6
    Collecting Flask
    Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
    Collecting click>=5.1 (from Flask)
    Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
    Collecting itsdangerous>=0.24 (from Flask)
    Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
    Collecting Jinja2>=2.10 (from Flask)
    Downloading https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
    Collecting Werkzeug>=0.14 (from Flask)
    Downloading https://files.pythonhosted.org/packages/18/79/84f02539cc181cdbf5ff5a41b9f52cae870b6f632767e43ba6ac70132e92/Werkzeug-0.15.2-py2.py3-none-any.whl (328kB)
    Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->Flask)
    Downloading https://files.pythonhosted.org/packages/98/7b/ff284bd8c80654e471b769062a9b43cc5d03e7a615048d96f4619df8d420/MarkupSafe-1.1.1-cp37-cp37m-manylinux1_x86_64.whl
    Installing collected packages: click, itsdangerous, MarkupSafe, Jinja2, Werkzeug, Flask
    Successfully installed Flask-1.0.2 Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.2 click-7.0 itsdangerous-1.1.0
    Removing intermediate container c16f61d74ce6
    ---> 7f11e6936652
    Step 5/7 : EXPOSE 80
    ---> Running in 95206b498b0c
    Removing intermediate container 95206b498b0c
    ---> ff7c2ff519f1
    Step 6/7 : ENV NAME World
    ---> Running in 41a0ec62a063
    Removing intermediate container 41a0ec62a063
    ---> 3a1f028858de
    Step 7/7 : CMD ["python", "app.py"]
    ---> Running in c39ecd605cca
    Removing intermediate container c39ecd605cca
    ---> 71368b10bc5c
    Successfully built 71368b10bc5c
    Successfully tagged helloworld:latest

    The image is now available in the local docker registry:

    docker image ls

    REPOSITORY TAG IMAGE ID CREATED SIZE
    helloworld latest 71368b10bc5c 3 minutes ago 153MB

Running the Application

Run the application with the -p option to map the port 4000 of your instance to port 80 of the container:

docker run -p 4000:80 helloworld

A message that the application called app is running is shown:

* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)

The message displays the information, that the application is running on port 80.

Remember, this is the internal port of the container and to reach the application type http://your_instances_ip:4000 in the address bar of your browser:

The application displays the internal hostname of the container and retrieves the word “World” from the environment variable that was set in the Dockerfile.

Sharing the Application

Docker allows easily to ship images to a registry to share it with others.

In this example, the public docker registry is used, but it also possible to set up a private registry to distribute the image on your infrastructure.

It is required to have a Docker account for this part of the tutorial. If you do not have one, you can create an account at hub.docker.com.

  1. Login into the Docker registry:

    docker login

    To associate a local image with a repository on a registry, you have to use the notation username/repository:tag.

    The tag is not mandatory, but recommended as it is the mechanism that registries use to give Docker images a version.

    To push your image you have to run docker tag with your username, repository, and tag, so your image uploads to the desired destination.

  2. Tag the image:

    docker tag imagename username/repository:tag
  3. Publish your image:

    docker push username/repository:tag

    Once the image has been pushed to Docker Hub it will be publicly available and you can see the image with its pull command in your account.

Pulling and Running an Image from a Remote Repository

As your image is available on the Docker Hub now, you can run it from everywhere

docker run -p 4000:80 username/repository:tag

If the image is not available yet on the local machine, Docker will download it from the registry.

No matter on which machine you run it, it will always run in the same environment that you have configured when you have built the image.