Docker-compose.yml: from V1 to V2

A short guide for converting to the new format

Image for post
Image for post

A docker-compose V1 file

dbpostgres:
image: postgres:9.4 #define the image to get
volumes_from:
— dbstore
ports:
— “5432:5432”
express-app-container:
build: .
ports:
— “3000:3000”
volumes:
— ./:/app
links:
— dbpostgres

docker create — name=dbstore -v /var/lib/postgresql/data postgres:9.4
var pg = require(‘pg’);
var connectionString = “postgres://postgres:postpwd@dbpostgres/mybigdb”

Step one on v2 — only minimal changes

version: ‘2’
services:
dbpostgres:
image: postgres:9.4 #define the image to get
volumes_from:
— container:dbstore #this is a container already defined
ports:
— “5432:5432”
express-app-container:
build: .
image: express-app #assign a name to the built image
ports:
— “3000:3000”
volumes:
— ./:/app
links:
— dbpostgres
version: ‘2’
services:
volumes_from:
— container:dbstore #this is a container already defined
volumes_from:
— dbstore

Step two — removing links

version: ‘2’
services:
dbpostgres:
image: postgres:9.4 #define the image to get
volumes_from:
— container:dbstore #this is a container already defined
ports:
— “5432:5432”
express-app-container:
build: .
image: express-app #assign a name to the built image
depends_on:
— dbpostgres #wait for dbpostgres to be started, not for ready
ports:
— “3000:3000”
volumes:
— ./:/app
links:
— dbpostgres:postgresdb
depends_on:
— dbpostgres #wait for dbpostgres to be started, not for ready

Step three — using volumes:

version: ‘2’
services:
dbpostgres:
image: postgres:9.4 #define the image to get
volumes:
— pgvolume:/var/lib/postgresql/data #using the declared volume
ports:
— “5432:5432”
express-app-container:
build: .
image: express-app #assign a name to the built image
depends_on:
— dbpostgres #wait for dbpostgres to be started, not for ready
ports:
— “3000:3000”
volumes:
— ./:/app
volumes:
pgvolume: #declare a volume named pgvolume
volumes:
pgvolume: #declare a volume named pgvolume
docker-compose rm 
volumes:
— pgvolume:/var/lib/postgresql/data #using the declared volume

Final step: using external volumes

version: ‘2’
services:
dbpostgres:
image: postgres:9.4 #define the image to get
volumes:
— pgvolume:/var/lib/postgresql/data #using the declared volume
ports:
— “5432:5432”
express-app-container:
build: .
image: express-app #assign a name to the built image
depends_on:
— dbpostgres #wait for dbpostgres to be started, not for ready
ports:
— “3000:3000”
volumes:
— ./:/app
volumes:
pgvolume:
external:
name: nodedocker_pgvolume
volumes:
— pgvolume:/var/lib/postgresql/data #using the declared volume

Final though

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store