How can I build and publish a docker image with a hash tag and the latest tag?

I’m using CircleCI to build a docker image and push it to docker hub. I’m making use of the docker orb.

orbs:
docker: circleci/docker@0.5.13

It’s all working fine except that in order to publish both with a hash and with the latest tag i’m building the container twice. Any tips on how I can remove the duplicated effort?

my config.yml looks like:

version: 2.1

orbs:
  docker: circleci/docker@0.5.13

jobs:
  run_tests:
    docker:
      - image: circleci/node:10.16.3
    steps:
      - checkout
      - run:
          name: update-npm
          command: 'sudo npm install -g npm@latest'
          working_directory: frontend
      - restore_cache:
          key: dependency-cache-{{ checksum "frontend/package.json" }}
      - run:
          name: npm-install
          command: npm install
          working_directory: frontend
      - save_cache:
          key: dependency-cache-{{ checksum "frontend/package.json" }}
          paths:
            - ./node_modules
      - run:
          name: test
          command: npm test
          working_directory: frontend

workflows:
  version: 2
  build_deploy:
    jobs:
      - run_tests
      - docker/publish:
          image: myrepo/typeit-frontend
          requires:
            - run_tests
          filters:
            branches:
              only:
                - master
      - docker/publish:
          image: myrepo/typeit-frontend
          tag: latest
          requires:
            - run_tests
          filters:
            branches:
              only:
                - master

Sure, just use docker tag, which works a little bit like creating symlinks (source on the left, target on the right). Do the build first without a tag, which will give you a mybuild:latest. Then you can do:

docker tag mybuild:latest mybuild:hash
docker push mybuild:latest
docker push mybuild:hash

Hi, thanks for the reply. The commands you’ve given will work, but I’m not executing docker commands. I’m using the CircleCI docker orb. Is there a way to run arbitrary docker commands with it? Should I abandon the orb?

Ah right, I’ve no idea. I’ve not started using Orbs yet. It’s on my list…