# Build image in build step and using it in the next one

**URL:** https://drone.discourse.group/t/build-image-in-build-step-and-using-it-in-the-next-one/10210
**Category:** Drone Support
**Created:** [November 28, 2018, 9:21am UTC](https://drone.discourse.group/t/build-image-in-build-step-and-using-it-in-the-next-one/10210 "2018-11-28T09:21:05Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![xsteadfastx](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/xsteadfastx/32/4183_2.png) [@xsteadfastx](https://drone.discourse.group/u/xsteadfastx)
#### Post date: [November 28, 2018, 9:21am UTC](https://drone.discourse.group/t/build-image-in-build-step-and-using-it-in-the-next-one/10210/1 "2018-11-28T09:21:05Z")

</div>

See also [http://discuss.harness.io/t/build-docker-image-and-re-use-in-the-next-step/6190](http://discuss.harness.io/t/build-docker-image-and-re-use-in-the-next-step/6190)

* * *

im using a specific image for testing my python code. and i want ti build a fresh image on every build without uploading it to a registry all the time. so i thought if i build it, tag it, it will be available to the agent. but doesnt seem to be the case. does the agent use a own docker daemon? why it doesnt seem local images?

here is my .drone.yml

```
kind: pipeline
name: default

steps:

  - name: build test image
    image: docker:latest
    volumes:
      - name: docker_sock
        path: /var/run/docker.sock
    commands:
      - docker build --no-cache -t cookiecutter-python-tests -f Dockerfile.tests .

  - name: py36
    image: cookiecutter-python-tests:latest
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e py36

  - name: py37
    image: cookiecutter-python-tests:latest
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e py37

  - name: flake8
    image: cookiecutter-python-tests:latest
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e flake8

  - name: pylint
    image: cookiecutter-python-tests:latest
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e pylint

  - name: black
    image: cookiecutter-python-tests:latest
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e black-only-check

volumes:
  - name: docker_sock
    host:
      path: /var/run/docker.sock

```

the building works fine… but the agent cant find the image… i get this error:

> Error response from daemon: pull access denied for cookiecutter-python-tests, repository does not exist or may require ‘docker login’

---

<div class="post-metadata">

### Author: ![xsteadfastx](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/xsteadfastx/32/4183_2.png) [@xsteadfastx](https://drone.discourse.group/u/xsteadfastx)
#### Post date: [November 28, 2018, 9:45am UTC](https://drone.discourse.group/t/build-image-in-build-step-and-using-it-in-the-next-one/10210/2 "2018-11-28T09:45:57Z")

</div>

i forget to say… everything else works. the agent is started with /var/run/docker.sock volume… i just dont know why the image cant be found.

---

<div class="post-metadata">

### Author: ![flah00](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/flah00/32/5854_2.png) [@flah00](https://drone.discourse.group/u/flah00)
#### Post date: [November 28, 2018, 5:40pm UTC](https://drone.discourse.group/t/build-image-in-build-step-and-using-it-in-the-next-one/10210/3 "2018-11-28T17:40:23Z")

</div>

Perhaps drone is trying to pull the image from [docker.io](http://docker.io) by default. Have you tried pushing the image to a [docker.io](http://docker.io) repo and redefining your images to match new image path?

```auto
steps:

  - name: build test image
    image: docker:latest
    volumes:
      - name: docker_sock
        path: /var/run/docker.sock
    commands:
      - docker build --no-cache -t ORG/cookiecutter-python-tests -f Dockerfile.tests .
      - docker push ORG/cookiecutter-python-tests:latest

  - name: py36
    image: ORG/cookiecutter-python-tests:latest
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e py36
...

```

---

<div class="post-metadata">

### Author: ![xsteadfastx](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/xsteadfastx/32/4183_2.png) [@xsteadfastx](https://drone.discourse.group/u/xsteadfastx)
#### Post date: [November 29, 2018, 9:58am UTC](https://drone.discourse.group/t/build-image-in-build-step-and-using-it-in-the-next-one/10210/4 "2018-11-29T09:58:14Z")

</div>

i think i found a way that this works… even if i dont know why it does 😉

```
kind: pipeline
name: default

steps:

  - name: build test image
    image: docker:latest
    volumes:
      - name: docker_sock
        path: /var/run/docker.sock
    commands:
      - docker build --no-cache -t cookiecutter-python-tests:${DRONE_COMMIT} -f Dockerfile.tests .

  - name: py36
    image: cookiecutter-python-tests:${DRONE_COMMIT}
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e py36

```

i set a own tag aka the DRONE\_COMMIT hash. and then it works. no idea why its like this.

---

<div class="post-metadata">

### Author: ![Raffo](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/raffo/32/4684_2.png) [@Raffo](https://drone.discourse.group/u/Raffo)
#### Post date: [November 29, 2018, 2:25pm UTC](https://drone.discourse.group/t/build-image-in-build-step-and-using-it-in-the-next-one/10210/5 "2018-11-29T14:25:28Z")

</div>

It works because you are mounting the docker socket, thus the image is already present locally.

---

<div class="post-metadata">

### Author: ![flah00](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/flah00/32/5854_2.png) [@flah00](https://drone.discourse.group/u/flah00)
#### Post date: [November 29, 2018, 3:48pm UTC](https://drone.discourse.group/t/build-image-in-build-step-and-using-it-in-the-next-one/10210/6 "2018-11-29T15:48:27Z")

</div>

@Raffo  
Perhaps I’ve missed something, but it seems to me that @xsteadfastx posted two versions, both mounted the socket. The working version used an env var (`DRONE_COMMIT`) to tag the image. As he said, that seems to be the reason it suddenly works.

Does drone allow steps to be executed on different agents?

---

<div class="post-metadata">

### Author: ![Raffo](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/raffo/32/4684_2.png) [@Raffo](https://drone.discourse.group/u/Raffo)
#### Post date: [November 29, 2018, 10:36pm UTC](https://drone.discourse.group/t/build-image-in-build-step-and-using-it-in-the-next-one/10210/7 "2018-11-29T22:36:32Z")

</div>

I’m unsure why it works actually, the “latest” tag should also work as it’s no different from the other tag.

---

<div class="post-metadata">

### Author: ![xsteadfastx](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/xsteadfastx/32/4183_2.png) [@xsteadfastx](https://drone.discourse.group/u/xsteadfastx)
#### Post date: [December 1, 2018, 11:57am UTC](https://drone.discourse.group/t/build-image-in-build-step-and-using-it-in-the-next-one/10210/8 "2018-12-01T11:57:16Z")

</div>

it doesnt work with latest. i posted two version. i shortened it… here is the full one:

```
kind: pipeline
name: default

steps:

  - name: build test image
    image: docker:latest
    volumes:
      - name: docker_sock
        path: /var/run/docker.sock
    commands:
      - docker build --no-cache -t cookiecutter-python-tests:${DRONE_COMMIT} -f Dockerfile.tests .

  - name: py36
    image: cookiecutter-python-tests:${DRONE_COMMIT}
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e py36

  - name: py37
    image: cookiecutter-python-tests:${DRONE_COMMIT}
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e py37

  - name: flake8
    image: cookiecutter-python-tests:${DRONE_COMMIT}
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e flake8

  - name: pylint
    image: cookiecutter-python-tests:${DRONE_COMMIT}
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e pylint

  - name: black
    image: cookiecutter-python-tests:${DRONE_COMMIT}
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e black-only-check

volumes:
  - name: docker_sock
    host:
      path: /var/run/docker.sock

```

and the whole difference was the docker image tag. with `latest` i dont get it working.

---

<div class="post-metadata">

### Author: ![bradrydzewski](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/bradrydzewski/32/3513_2.png) [@bradrydzewski](https://drone.discourse.group/u/bradrydzewski)
#### Post date: [December 1, 2018, 9:29pm UTC](https://drone.discourse.group/t/build-image-in-build-step-and-using-it-in-the-next-one/10210/9 "2018-12-01T21:29:37Z")

</div>

this is because drone behaves the same way as kubernetes, and will always try to pull the `latest` tag from the registry. In your case, this results in your cached `latest` tag being replaced with the image from the remote registry. If you do not desire this behavior, you can override the default pull behavior like this:

```diff
  - name: py36
    image: cookiecutter-python-tests:latest
+ pull: if-not-exists
    environment:
      TOX_WORK_DIR: /tmp/tox
    commands:
      - tox -e py36

```

See also [Build Docker image and re-use in the next step](http://discuss.harness.io/t/build-docker-image-and-re-use-in-the-next-step/6190)

---

<div class="post-metadata">

### Author: ![xsteadfastx](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/xsteadfastx/32/4183_2.png) [@xsteadfastx](https://drone.discourse.group/u/xsteadfastx)
#### Post date: [December 4, 2018, 12:12pm UTC](https://drone.discourse.group/t/build-image-in-build-step-and-using-it-in-the-next-one/10210/10 "2018-12-04T12:12:21Z")

</div>

this is great. thanks for the answer. that explains it well.
