Hometutorials
docker instantapp

Jump toUpdate content

Getting started with the Docker InstantApp

Reviewed on 14 February 2022 • Published on 13 May 2019
  • compute
  • gaming
  • server
  • Docker
  • Docker-InstantApp
  • virtual-instance

Docker is an open source containerization platform. It allows developers to package their applications into containers, using OS-level virtualization. This page shows you how to create an Instance with Docker pre-installed using the Docker InstantApp, and then how to start using Docker for your containerization projects.

Requirements:

Creating and connecting to your Docker InstantApp Instance

  1. Create the Instance by following our dedicated instructions. When choosing your Image, click the InstantApps tab and select the Docker InstantApp.

  2. Connect to your Instance via SSH.

Building your first container

Docker allows you to ship applications in containers. Containers are standardized units of software that bundle the application’s 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.

Here, we create a new Dockerfile 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 and 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"]

    The Dockerfile is now ready, and we can proceed to creating the application.

  4. 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 now downloads the Python base image and processes the tasks defined in the Dockerfile to create the image for our application:

    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 displays, showing that the application called app is running:

* 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. 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 makes it easy to ship images to a registry to share with others.

In this example, the public Docker registry is used (“Docker Hub”), but it also possible to set up a private registry to distribute the image on your infrastructure, or to use the Scaleway Container Registry.

Note:

You need 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. Log into the Docker registry:

    docker login
  2. Tag the image, using the docker tag command followed by the notation imagename username/repository:tag. This associates your local image with a repository on the registry. The tag at the end is not mandatory, but recommended as it is the mechanism that registries use to give Docker images a version.

    docker tag imagename username/repository:tag
  3. Push your image to the Docker Hub:

    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

Your image is now available on the Docker Hub, and you can run it from anywhere

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

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

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