How to deploy a docker image that I previously built

I am trying to build the Docker image for my service, push it to Okteto, and then later deploy it. However, what I’m finding is that although the image is pushed into place, the deploy step takes a different version of the image. (The reason I’m doing it in two different steps is to have testing of my image before I deploy it within my pipeline)

My steps are:

  • docker build with the name of the image tagged as latest which pushes it to the Okteto registry
  • docker pull to pull down that image
  • okteto deploy --wait

In my okteto.yaml file I have:

name: berlin-clock

  berlin-clock:
    context: .
    dockerfile: Dockerfile

deploy:
  compose: compose.yml

dev:
  berlin-clock:
    image: registry.cloud.okteto.net/xxx/berlin-clock:latest
    command: npm ci --omit=dev --ignore-scripts && npm run start:app
    workdir: /usr/src/app
    forward:
      - 9229:9229
      - 9080:9080
  goss:
    image: aelsabbahy/goss
    environment:
      - NODE_ENV=production

In my docker compose file I have:

version: '3.8'

services:
  berlin-clock:
    image: registry.cloud.okteto.net/xxx/berlin-clock:latest
    container_name: berlin-clock
    build:
      context: .
      dockerfile: ./Dockerfile
    volumes:
      - .:/src
      - goss:/goss
    environment:
      NODE_ENV: production
    ports:
      - 9080:9080
    restart: always
    healthcheck:
      test: ['CMD', '/goss/goss', 'validate']
      interval: 10s
      timeout: 10m
      retries: 5
      start_period: 10s
  goss:
    image: aelsabbahy/goss
    container_name: goss
    volumes:
      - goss:/goss
    command: ['tail', '-f', '/dev/null']

volumes:
  goss:

Any suggestions on what I should change to get it to work correctly?

Hi @beans! If you remove the image field from the compose.yaml and okteto.yaml for the berlin-clock service, okteto will automatically build the correct image for you on demand based on the contents of your repo. This will work wether you run it from the CLI using okteto deploy (in which case it will build with the latest code, committed or not), or through the UI (in which case it will build with the latest committed code for the selected branch).

Docker Compose on Kubernetes with Okteto | Okteto Documentation has an example you can follow.

Let me know if this works?

Hey @ramiro,

Thanks for your suggestion. I tried that out and found that I ended up with a different image being deployed from the one I built. I’m looking to have a pipeline that builds the image → deploys it to dev → runs some tests → creates a release and deploys it to staging → runs some smoke tests → deploys it to production.

This way I build it once and each environment has exactly the same image. I’m not seeing how to do that with a docker-compose.yml and okteto.yml. Maybe I need to use a different deployment strategy?

Hi @beans ,

If I understood correctly, you need to do the build in a different step than the deploy. In your first message, you mention that you are executing docker build and you are pushing the image registry.cloud.okteto.net/xxx/berlin-clock:latest, right?

One thing that might happen is that you are not doing docker push to push the image, so the latest version of the image built is not really being pushed to the remote registry. If it’s not pushed, Okteto cannot take the latest version.

Instead of using docker build, you can use okteto build -t okteto.dev/berlin-clock:latest -f Dockerfile . . When you specify the -t parameter, Okteto will push automatically the image to the registry without having to do 2 commands.

Doing that, the deployment should take the latest version you have pushed.

Other thing that might be happening is that the pullPolicy of the deployment is not Always, so as you are always building the same tag, Kubernetes is not detecting that the images are different (even if the content is different) and it’s not pulling the new image. Could you verify the pullPolicy of the pod?