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

Django cookiecutter with Docker add pip package
Docker Compose + Swarm + Machine Networking issue
docker-compose build image parameters --force-rm --pull
linking 2 containers through docker compose
Files created by manage.py commands not chown'd from root user
Is it possible to create an intermediate image within docker-compose?
docker-compose service listen on specified interface
Does Docker-Compose allow overwriting of ports when using extends?
Jhipster Docker-compose conflict
Build multiple images with Docker Compose?
JHipster Cassandra Docker Build
Defining project name inside docker-compose configuration
Docker-compose named mounted volume
cannot ping linked docker container service
How to do variable substitution in env_file?
How to create stack in tutum with docker-compose.yml file?

Categories

HOME
asterisk
reactjs
repository
primefaces
leaflet
process
file-upload
crash
google-docs-api
msbi
confluence-rest-api
rtc
google-app-maker
spring-data-solr
wowza
hdf5
yui
facebook-marketing-api
versioning
conditional-formatting
scotty
udeploy
computation-theory
jq
ecmascript-2017
suitescript
datanucleus
orgchart
proof-general
boosting
ds-5
blockly
spring-oauth2
qt-designer
apache-falcon
yelp
mybatis-generator
ng-lightning
fisheye
ssdp
aura-framework
contenteditable
intervention
amazon-fire-tv
chrome-custom-tabs
spring-webflow-2
denodo
evaluation
django-tables2
jcs
dynamic-linking
selection-sort
reloaddata
petapoco
acceleo
scanf
vertex-shader
fileopendialog
mavlink
symantec
eclipse-mars
git-push
tinybox2
netlink
transaction-isolation
paypal-subscriptions
ooad
cascading
opendolphin
imageicon
maven-antrun-plugin
android-togglebutton
esb-toolkit-2.1
tidesdk
ultraedit
indices
shunting-yard
breeze-sharp
powerpoint-2010
xts
android-relativelayout
cvi
xcode6.1-gm-seed
myrrix
tlbimp
angularjs-select2
playing-cards
leap-year
gwt2
servicemanager
cloudmade
brew
biztalk-deployment
optimistic-concurrency
ruby-1.9
scriptmanager
copyfile
xpolog
funambol
lightopenid
sessiontracking
appointment
user-preferences
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