Keep docker images between jobs

Hello,

I’d like to build and commit docker image in one job and in another use the committed image.
Is it possible using CircleCI? I know that it’s possible for sure in Gitlab CI something like that.
I think that example below will be more explanatory:

version: 2
jobs:
  build:
    working_directory: /var/www/application
    docker:
      - image: docker:17.05.0-ce-git
        environment:
          REPOSITORY: 12345.dkr.ecr.us-west-1.amazonaws.com/application-api
          CONTAINER_BUILD: application-api-$CIRCLE_SHA1
          BUILD_IMAGE: $REPOSITORY:$CIRCLE_SHA1
          STAGE_IMAGE: $REPOSITORY:stage-$CIRCLE_SHA1
          LIVE_IMAGE: $REPOSITORY:live-$CIRCLE_SHA1
          AWS_ELASTICBEANSTALK_REGION: us-west-1
    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: Install dependencies
          command: |
            apk add --no-cache \
              py-pip=9.0.0-r1
            pip install \
              awscli==1.11.76
      - run:
          name: Build
          command: |
            docker build -t ${REPOSITORY}:${CIRCLE_SHA1} .
            docker run -d --name application-api-${CIRCLE_SHA1} -v /tmp/composer/cache:/root/.composer/cache ${REPOSITORY}:${CIRCLE_SHA1}
            docker exec application-api-${CIRCLE_SHA1} composer install --no-interaction
            docker commit application-api-${CIRCLE_SHA1} ${REPOSITORY}:${CIRCLE_SHA1}

  test:
    working_directory: /var/www/application
    docker:
      - image: docker:17.05.0-ce-git
        environment:
          REPOSITORY: 12345.dkr.ecr.us-west-1.amazonaws.com/application-api
          CONTAINER_BUILD: application-api-$CIRCLE_SHA1
          BUILD_IMAGE: $REPOSITORY:$CIRCLE_SHA1
          STAGE_IMAGE: $REPOSITORY:stage-$CIRCLE_SHA1
          LIVE_IMAGE: $REPOSITORY:live-$CIRCLE_SHA1
          AWS_ELASTICBEANSTALK_REGION: us-west-1
    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: Tests
          command: |
            docker run -d --name application-api-${CIRCLE_SHA1} -v /tmp/composer/cache:/root/.composer/cache ${REPOSITORY}:${CIRCLE_SHA1}
            docker exec application-api-${CIRCLE_SHA1} bin/phpspec run

workflows:
  version: 2
  build_and_test:
    jobs:
      - build
      - test:
          requires:
            - build
          filters:
            branches:
              only: develop

I’ve got error below:
Unable to find image ‘12345.dkr.ecr.us-west-1.amazonaws.com/application-api:23’ locally

What means that my committed image is not passed between jobs.

Additionally I’d like to know if I can prepare one environment section for all jobs?

+1 on this.

Can someone from CircleCI comment on whether this use-case is supported?

The original post is not that clear to me, but I suspect that the answer is “yes”. What is meant by “committing” a Docker image - is “push” meant here? :smile:

Unless there is some nuance I have missed then yes, I do this. However, I install Docker Compose and do it all in there, so I don’t know how similar or different that is to what you’re wanting to achieve. I do a docker login, then a series of docker pull and docker tag operations, and then docker-compose up <params> and finally docker exec ... to run tests. Works a treat.

Update

Ah, I re-read the end of the OP. As far as I know, images are not kept around - I push them from one project and pull them from another. There may be something clever you can do with the cache feature though…