Skip to content

Deploy From a Docker Image

Coherence allows you to deploy services from prebuilt Docker images hosted on Docker Hub. This guide will show you how to deploy Coherence services from a Docker image.

Prerequisites

Build and publish a Docker image

To deploy a Coherence service from a Docker image, you need to build and publish a Docker image to Docker Hub.

  1. From the root of your application repo, build the Docker image:

    docker build -t <your-docker-username>/<your-image-name>:<tag> .
    

    For example:

    docker build -t demo/flask-employees-demo:1.0 .
    
  2. Push the Docker image to Docker Hub:

    docker push <your-docker-username>/<your-image-name>:<tag>
    

Your Docker image is now available on Docker Hub. Make a note of the image name and tag.

Docker Hub

Deploy a Coherence service from a Docker image

To set up your project in Coherence, create an app, add a collection, and create an environment. If you're new to Coherence or need a refresher on these steps, refer to the quick start guide.

Next, deploy a Coherence service from a Docker image.

  1. From the environment dashboard, click the Services tab.
  2. Click New Service.
  3. Select the service type you want to deploy. If you're following the Flask example, select Flask.

  4. Enter "backend" under Name.

  5. Enter the start command defined in your Dockerfile. For the Flask example, it's python app.py 0.0.0.0:8080.
  6. Enter "/" under URL path.

  7. Choose Existing Image under Container source.

  8. Paste in the Docker image you pushed to Docker Hub.
  9. Click Static under Tag source.
  10. Enter the tag you used when you pushed the Docker image to Docker Hub.
  11. Click Create service.

Adding an image

Add a database service

For most applications, you'll need a database service. Here’s how to add a PostgreSQL database to the environment:

  1. Click New service. Select Postgres under the Database section.
  2. Enter the name of the database service, for example, employees.
  3. Enter the database engine version, for example, 14.
  4. Click Create service to save the service configuration.

The database service will be provisioned during the provisioning step. When building and deploying the application, Coherence will automatically insert the database connection string into the application environment variables.

Handle database migrations

If the application requires database migrations, you can run them as part of the build process. Add a migrate step to the cnc.yml to run the migrations.

    services:
      backend:
        ...
        x-cnc:
          type: backend
          system:
            health_check: ''
          migrate: flask db migrate -m "Initial migration." 
          url_path: /
        ...

Provision, build, and deploy

Now you can provision, build, and deploy the application.

If you haven't connected your cloud provider account, you'll need to do so before you can provision infrastructure.

Connect to a cloud provider account

Click Connect to Cloud to connect your cloud provider account.

Create a new app

  • Enter your Google Cloud project ID. Find your project ID on the project's main page in the Google Cloud console.
  • Select the region to use for your cloud instance.
  • Check the boxes to confirm you have admin privileges and sufficient quota.
  • Click Next to proceed to the instructions to add an IAM role in your cloud provider that will allow Coherence to create the resources it needs in your cloud account.

Read our connect to GCP guide if you need more help with this step.

  • Enter your AWS account ID. Find your account ID on the main page of the AWS console.
  • Select the region to use for your cloud instance.
  • Check the boxes to confirm you have admin privileges and sufficient quota.
  • Click Next to proceed to the instructions to add an IAM role in your cloud provider that will allow Coherence to create the resources it needs in your cloud account.

Read our connect to AWS guide if you need more help with this step.

When prompted, you can skip the Import variables modal if you do not have extra variables to define. Variables like DATABASE_URL will be automatically configured.

Provision infrastructure

  1. Go to the Provisioning tasks tab of the collection.
  2. Click Submit new task to provision the infrastructure.
  3. Wait for the provisioning task to complete.

Build and Deploy

Once provisioning is complete, go to the Deployments tab of the environment.

  1. Click New deployment.
  2. Choose the tag you want to use.
  3. Click Submit deployment.

Coherence will now build and deploy the application. When the deployment is successful, you can access the application by clicking on the domain provided in the Domains section in the environment.