Docker Compose - Tips

(how to) revert container to its original image

it might be useful to discard all data persisted inside container.

stop and remove service container:

$ docker-compose stop db
$ docker-compose rm db
$ docker-compose up db

(how to) rebuild image

it might be necessary when configuration options applied at build time are changed - say, you want to edit docker image tag:

  # postgresql.dockerfile

- FROM postgres:10.4
+ FROM postgres:11.1

docker-compose up never rebuilds an image.

You can run docker-compose build to build the images.

$ docker-compose stop db
$ docker-compose build db
$ docker-compose rm db
$ docker-compose up db

NOTE: this destroys all data - if you want to migrate data, see the next tip.

(how to) migrate data from old Postgres version

  # docker-compose.yml

  version: '2'
-     image: postgres:9.4
+     image: postgres:12.0

now you need to migrate data - otherwise you’ll get such error:

$ docker-compose up db
db_1  | 2019-02-18 09:19:23.315 UTC [1] DETAIL:  The data directory was
  initialized by PostgreSQL version 10, which is not compatible with this
  version 11.1 (Debian 11.1-1.pgdg90+1).