docker-compose


If I upgrade an image in docker-compose, will its volumes be lost?


Suppose I have a docker-compose.yaml file that has a postgres 9.1 service:
postgres:
image: postgres:9.1
If it's upgraded to postgres 9.4, will the data in the database be lost? I.e., will the volumes be destroyed and recreated?
tl;dr
The data won't be destroyed. The postgres container will be destroyed and recreated, but links to dependant containers will be migrated. The hard part is actually migrating the data.
Revert to old postgres and take a snapshot. Make sure the resulting migration.psql contains sensible data, or your risk losing everything.
sed -i -e 's/postgres:9.*$/postgres:9.1/g' docker-compose.yml
sudo docker-compose up -d postgres
sudo docker-compose run --rm dbadmin pg_dumpall > migration.psql
Remove old database files from the volume
sudo docker-compose stop postgres
sudo docker run --rm --volumes-from scratch_postgres_1 postgres:9.6 bash -c "rm -r /var/lib/postgresql/data/*"
Upgrade to new postgres and restore snapshot
sed -i -e 's/postgres:9.*$/postgres:9.4/g' docker-compose.yml
sudo docker-compose up -d postgres
sudo docker-compose run --rm dbadmin psql < migration.psql
Details below.
Test setup
We can test this by having a dbadmin service consume the postgres service.
postgres:
image: postgres:9.1
dbadmin:
image: postgres:9.4
links:
- postgres
environment:
PGHOST: postgres
PGUSER: postgres
PGDATABASE: postgres
PGPASSWORD: postgres
volumes:
- ./backup:/backup
command: bash -c "while true; do sleep 1; done"
Start both services and create a table.
docker-compose up -d
docker ps --format "{{.ID}}\t{{.Image}}\t{{.Names}}"
docker-compose exec dbadmin psql -tc "SHOW server_version"
docker-compose exec dbadmin psql -tc "CREATE TABLE foo (bar int);"
fb905d183992 postgres:9.4 scratch_dbadmin_1
9316b431a369 postgres:9.1 scratch_postgres_1
9.1.24
CREATE TABLE
Without recreating the service
Upgrade the service:
postgres:
- image: postgres:9.1
+ image: postgres:9.4
Is the table still there?
docker-compose exec dbadmin psql -tc "\d"
public | foo | table | postgres
Yes, but the service still isn't using version 9.4:
docker ps --format "{{.ID}}\t{{.Image}}\t{{.Names}}"
fb905d183992 postgres:9.4 scratch_dbadmin_1
9316b431a369 postgres:9.1 scratch_postgres_1
Recreate
docker-compose up -d postgres
docker-compose logs postgres
Recreating scratch_postgres_1
postgres_1 | LOG: skipping missing configuration file "/var/lib/postgresql/data/postgresql.auto.conf"
postgres_1 | FATAL: database files are incompatible with server
postgres_1 | DETAIL: The data directory was initialized by PostgreSQL version 9.1, which is not compatible with this version 9.4.10.
That looks like good news: postgres has found a database but it has the wrong version. Since the migration isn't automatic, it's a bit painful.
Migrate
WARNING This may well destroy your data. Only run these commands with this little test project.
First downgrade to 9.1 again. Then use the dbadmin service to take a backup.
docker-compose up -d postgres
docker-compose exec dbadmin pg_dumpall > migration.psql
Delete the contents of the volume:
docker-compose stop postgres
docker run --rm --volumes-from scratch_postgres_1 postgres:9.1 \
bash -c "rm -r /var/lib/postgresql/data/*"
Upgrade to 9.4 again. Because we destroyed the database, this will create a new one. Then use the dbadmin service to import the backup.
docker-compose up -d postgres
docker-compose exec dbadmin psql < migration.psql
docker-compose exec dbadmin psql -tc "\d"
docker-compose exec dbadmin psql -tc "SHOW server_version"
docker ps --format "{{.ID}}\t{{.Image}}\t{{.Names}}"
public | foo | table | postgres
9.4.10
9bf94397f89c postgres:9.4 scratch_postgres_1
fb905d183992 postgres:9.4 scratch_dbadmin_1
Notice that the container ID of dbadmin is the same, therefore it is the same container - however, it is now linked to the upgraded postgres container (which was recreated).

Related Links

Let external container know about subnets created by multiple docker-compose projects
How to monitor / discover more docker containers with prometheus
two docker-compose .yml in the same network with COMPOSE_PROJECT_NAME
docker compose 3.1(swarm), secrets, ssl and nginx
What is the version directive for?
How to run docker-compose up -d at system start up?
In Docker Compose can I have services exiting
Docker compose - secrets Additional property secrets is not allowed
docker-compose mounted volume remain
Should I do anything after upgrading docker-compose?
docker caddy proxy not forwarding
docker share OS volume between services
How dynamic map service name to ENV var
docker-compose v3 services on several networks
URL for cloudant on docker compose
Apache Metron Docker Compose Start Up Error

Categories

HOME
model-view-controller
twitter-bootstrap
ssh
compression
customization
mysqli
conceptual
itext7
ubuntu-14.04
atlassian-fisheye
docker-compose
autocad
install
virtualhost
virtualenv
watson-dialog
amazon-data-pipeline
orange
accordion
project-structure
git-branch
typedef
google-drive-android-api
inline-assembly
jira-zephyr
orgchart
formtastic
lines
ilog
wunderground
ponylang
framebuffer
vertical-alignment
superclass
servicebus
risc
activeandroid
g77
emacs24
openidm
extjs3
dna-sequence
dotnet-cli
enet
richtext
seyren
androiddesignsupport
cc
cardview
blitz3d
virtualenvwrapper
altova
disconnect
scriptengine
falcon
bytearray
phpdesigner
usps
android-progressbar
nugetgallery
abstract
ewam
magnet-uri
php-gd
jain-sip
avrcp
connection-timeout
applinks
teamstudio-unplugged
caption
iphone-6
go-gin
system.net
powerpoint-2010
selenium-grid2
iwork
starling-framework
kuka-krl
xtify
misfire-instruction
myrrix
qgraphicsscene
android-library
pyxplot
gwt2
gmail-imap
freeimage
linear-interpolation
derived-class
mvcmailer
copyfile
execjs
auto-compile
mysql-pconnect
authkit
cstring
asp.net-1.1
weak-typing

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App