Update Docker image: docker compose commands and Granite.so

By Mark Hayes
Update Docker image: docker compose commands and Granite.so

There's a moment every Docker user hits sooner or later: you push a fix, run docker pull on the server, refresh the site, and nothing has changed. The new image is there. The old container is still running.

That's the core of how you update a Docker image in practice. Images and containers are separate things, and a container never switches to a newer image by itself. This guide covers the correct commands for plain Docker and Docker Compose, how to tag images so updates and rollbacks are predictable, and how to hand the whole process to a platform so an image update isn't something you do by hand over SSH.

Images vs containers: why pulling isn't enough

A Docker image is an immutable, layered filesystem plus metadata, identified by a content digest (sha256:...). A tag like myapp:1.4.2 or myapp:latest is just a movable pointer to one of those digests.

A container is a running instance created from a specific image digest. When you run docker pull myapp:latest, Docker moves the local latest tag to the new digest. The existing container keeps the old digest it was created from.

So updating always takes two steps:

  1. Get the new image (pull it or build it).
  2. Recreate the container from that image.

Everything below is a variation on those two steps.

Updating a single container with plain Docker

For a container started with docker run:

# 1. Pull the new version
docker pull ghcr.io/acme/app:1.5.0

# 2. Stop and remove the old container
docker stop app
docker rm app

# 3. Start a new one with the same options
docker run -d --name app \
  --restart unless-stopped \
  -p 3000:3000 \
  --env-file /etc/app/.env \
  ghcr.io/acme/app:1.5.0

To confirm which image a container is actually running:

docker inspect --format '{{.Config.Image}} {{.Image}}' app

The first value is the tag you asked for, the second is the exact digest in use. Compare it with docker image inspect --format '{{.Id}}' ghcr.io/acme/app:1.5.0 to be sure.

The weak spot of this approach is that you have to remember every docker run flag. That's why most people move to Compose.

Docker compose update image: the correct commands

With Docker Compose v2 (docker compose, not the old docker-compose binary), the configuration lives in a file, so recreating a container is safe and repeatable.

Services that use a registry image

# compose.yaml
services:
  web:
    image: ghcr.io/acme/app:${APP_TAG:-latest}
    restart: unless-stopped
    ports: ["3000:3000"]
    env_file: .env

To update:

docker compose pull web
docker compose up -d web

up -d compares the running container with the current config and image. If the image digest changed, it recreates the container. If nothing changed, it leaves it alone. You can combine both steps:

docker compose up -d --pull always web

If other services depend on web and you don't want them restarted, add --no-deps.

Services built from a Dockerfile

If the service uses build: instead of image:, pulling does nothing for it. Rebuild instead:

docker compose build --pull web   # --pull refreshes the base image too
docker compose up -d web

docker compose up -d --build web does it in one step, but it won't refresh the base image. --pull on build is easy to forget and important. Without it you keep building on a cached node:22-alpine or python:3.13-slim base with last month's security patches.

Updating everything at once

docker compose pull
docker compose up -d --remove-orphans

--remove-orphans removes containers for services you've deleted from the file.

Watch out: updating everything also updates your database image if it uses a floating tag like postgres:latest. A major Postgres version jump can't read the old data directory, and the container won't start. Always pin database images to a major version, like postgres:16.

Tags that make updates predictable

Most update pain comes from tagging, not from commands.

Stop deploying latest

latest isn't "the newest version". It's whatever was last pushed with that tag. Two servers pulling latest an hour apart can end up running different code, and there's no easy way to say which version was running when a bug happened.

Tag every build with something immutable and traceable:

  • the Git commit SHA (app:3f9c2ab),
  • a semantic version (app:1.5.0),
  • or both.

A GitHub Actions workflow that does this and pushes to GitHub Container Registry:

# .github/workflows/image.yml
name: image
on:
  push:
    branches: [main]
    tags: ["v*"]

permissions:
  contents: read
  packages: write

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: docker/setup-buildx-action@v3
      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - id: meta
        uses: docker/metadata-action@v5
        with:
          images: ghcr.io/${{ github.repository }}
          tags: |
            type=sha
            type=semver,pattern={{version}}
            type=raw,value=latest,enable={{is_default_branch}}
      - uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max

Pin by digest when it really matters

For third-party images you depend on, pinning by digest guarantees you get exactly the bytes you tested:

services:
  cache:
    image: redis:7.4@sha256:<digest>

You then update deliberately, by changing the digest, ideally through a tool like Renovate or Dependabot that opens a PR for each new version.

Rolling back

With immutable tags, a rollback is just an update to an older tag:

APP_TAG=1.4.2 docker compose up -d web

This only works if the old image still exists locally or in the registry, which is one more reason not to overwrite tags. Keep at least the last few versions in your registry's retention policy.

Keep in mind that a rollback only restores the code. If the new version ran a database migration that dropped a column, the old code may no longer work. Make migrations backward-compatible (add first, remove in a later release) so rolling back one version is always safe.

Cleaning up old images

Each update leaves the previous image on disk. On a small VPS this is a real cause of full disks:

docker system df           # see what's using space
docker image prune -f      # remove dangling images
docker image prune -a -f --filter "until=168h"   # unused images older than 7 days

Don't run prune -a without a filter on a box where you might need to roll back. It removes every image not used by a running container.

What about docker commit?

You can create a new image from a running container's changes with docker commit:

docker commit app acme/app:hotfix-manual

It works, but it makes an image nobody can reproduce: there's no Dockerfile history, and whatever state was in the container (temp files, secrets in env, half-applied changes) gets baked in. Use it for debugging or forensics, not as a way to update production. The right fix is to change the Dockerfile or code, rebuild, and push.

Automatic updates: options and trade-offs

Once the manual flow works, the next question is how to stop doing it yourself.

Watchtower watches running containers and recreates them when the tag they use points to a new digest. It's simple and fine for home labs. For production it has two drawbacks: it only makes sense with floating tags, which brings back the latest problem, and it has no health-check-based rollback.

CI with SSH builds the image, then SSHes into the server and runs docker compose pull && docker compose up -d. This is predictable, but now CI holds an SSH key to production, and you still own the server, its disk, its OS updates and its database.

A managed platform takes over the recreate step entirely. You provide a new image or push new code, and the platform starts new instances, checks their health, moves traffic over and stops the old ones.

Updating images on Granite.so

On Granite.so there's no server, SSH key or Compose file on a host to manage. You get to the same result in one of two ways:

  • From Git: connect the repository, and every push to your deploy branch builds a new image from your Dockerfile and deploys it automatically.
  • From an image: deploy a service from a Docker image in any registry you already use (GHCR, Docker Hub or your own). When the tag you deploy, say ghcr.io/acme/api:main, is pushed again and its digest changes, Granite.so picks up the new digest and redeploys automatically. Your CI pipeline pushing the image is the deploy step. You can also switch the service to a different tag to pin or roll back a release.

This is where automated scaling changes the picture. If your app is running several instances because of load, a manual update on a VPS means updating them one by one or accepting downtime. On a platform, new instances with the new image start alongside the old ones and take traffic only when healthy, and the same happens across every instance the autoscaler is running. Once the update is live, scaling up adds more instances of the new version, never a stale one.

Your database, storage and backups aren't affected by the app update, because they're managed separately. That's the key difference from a single VPS where docker compose pull could accidentally touch your postgres container.

On cost, the Hobby plan is $9/month with $15 of usage credits included, and updating images more often doesn't change it. When you need more headroom, the Advanced plan ($39/month with $50 of usage credits) is the next step. Check the pricing details before you plan around it.

Keep the good habits anyway: immutable tags, pinned base images and backward-compatible migrations matter just as much on a platform. The platform removes the manual steps, not the need to version your images properly.

Quick reference

GoalCommand
Pull and recreate one Compose servicedocker compose pull web && docker compose up -d web
Same, in one commanddocker compose up -d --pull always web
Rebuild with fresh base imagedocker compose build --pull web && docker compose up -d web
Update all servicesdocker compose pull && docker compose up -d --remove-orphans
Check running image digestdocker inspect --format '{{.Image}}'
Roll backAPP_TAG=docker compose up -d web
Free disk spacedocker image prune -a -f --filter "until=168h"

Wrapping up

To update a Docker image correctly: get the new image, recreate the container, and use tags that let you say exactly what's running and go back to what ran before. Docker Compose makes that repeatable. A managed platform makes it automatic.

If you'd rather push a commit and let the rest happen, including rolling out across scaled instances without touching the database, try deploying your service on Granite.so. Your next image update becomes a git push.

Keep reading

More posts tagged “Tutorials”.
Be first in line for updates
and special pricing
Get early access to new features and exclusive discounts delivered straight to your inbox