Search code examples
djangoherokudocker-composecircleci

How to retrieve the docker image of a deployment on heroku via circleci


I have a django application running locally and i've set up the project on CircleCi with python and postgres images. If I understand correctly what is happening, CircleCi would use the images to build a container to test my application with code database. Then I'm using the job heroku/deploy-via-git to deploy it to Heroku when the tests are passed.

Now I think Heroku is using some images too to run the application.

I would like to get the image used by heroku to run my site locally on another machine. So pull the image then push it to Docker Hub and finally download it back to my computer to only have to use a docker compose up.

Here is my CircleCi configuration's file

version: 2.1

docker-auth: &docker-auth
  auth:
    username: $DOCKERHUB_USERNAME
    password: $DOCKERHUB_PASSWORD

orbs:
  python: circleci/[email protected]
  heroku: circleci/[email protected]

jobs:
  build-and-test:
    docker:
      - image: cimg/python:3.10.2
      - image: cimg/postgres:14.1
        environment:
          POSTGRES_USER: theophile

    steps:
      - checkout
      - run:
          command: pip install -r requirements.txt
          name: Install Deps 
      - run:
          name: Run MIGRATE
          command: python manage.py migrate 
      - run:
          name: Run loaddata from Json
          command: python manage.py loaddata datadump.json
      - run:
          name: Run tests
          command: pytest

          
workflows:
  heroku_deploy:
    jobs:
      - build-and-test
      - heroku/deploy-via-git:
          requires:
              - build-and-test

I don't know if it is possible, if not, what should be the best way to proceed ? (I assume that there is a lot of possibilites)

I was considering to build an image from my local directory with docker compose up then use this image direclty on CircleCi, then i would be able to use this image an on other computer. But building images into images with CircleCi seems really messy and I'm not sure how I should proceed.

I've tried to pull images from Heroku but it seems I can only pull the code or get/modify the database but I can't get the image builds itself.

I hope this question is relevant and clear, as the CircleCi and Heroku documentation seems not clear and it's my first post on stackoverflow ! Thanks in advance


Solution

  • Heroku's platform is proprietary, so we can't be sure how it works internally.

    We know that their stacks are based on Ubuntu LTS releases, and we know that they use open-source buildpacks to compile application slugs from source code, but details about the underlying infrastructure are murky. They certainly don't provide base images like heroku/python:3.11.0 for you to download.

    If you want to use the same image locally, on CircleCI, and Heroku, a better option would be to start deploying with Heroku's Container Registry instead of Git. This allows you to build an image locally, push it into the container registry, and release it as the next version of your application.

    I suggest you read the entire documentation page linked above, but the short version is:

    1. Log into the container registry using the Heroku CLI:

      heroku container:login
      
    2. Assuming you already have a Dockerfile for your application, build and push an image:

      heroku container:push web
      

      In this case we are building from Dockerfile and pushing the resulting image to be used as a web process.

    3. Release your application:

      heroku container:release web
      

    That's a basic Docker deployment from your local machine, and even if that's not your final plan I suggest you start by getting that working.

    From there, you have options. One option would be to move this flow to CircleCI—continue to build images there, but have CircleCI push the resulting container to Heroku's Container Registry.

    Another option might be as you suggest in your question: to build images locally and use them with both CircleCI and Heroku.