Skip to main content
In this guide, you’ll learn how to migrate a basic Docker Compose setup from version 1 to version 3. We’ll remove deprecated options, configure environment variables for PostgreSQL, and explore build and deployment best practices.

1. Starting with the Basic Compose File (Version 1)

Here’s a simple Compose file in version 1 syntax defining five services:
Version 1 is straightforward but lacks support for:
  • Automatic network creation
  • Built-in DNS service discovery
  • Named volumes and advanced deployment options

2. Upgrading to Version 3

Compose version 3 unlocks Docker Swarm compatibility, automatic networking, and enhanced resource definitions. Follow these steps:
  1. Add version: "3" at the top.
  2. Nest all services under the services: key.
  3. Remove links:—service names now resolve via DNS.
In version 3, Docker Compose automatically creates a default network. Services communicate by name, e.g., redisdb.
The image shows a section of the Docker documentation website, specifically the Compose and Docker compatibility matrix, listing different Compose file versions and their corresponding Docker versions.

3. Specifying Build Options

You can define how images are built directly in your Compose file. Examples from Docker’s docs: Simple build context:
Custom Dockerfile and build args:
Build with image tag:
In Swarm mode, docker stack deploy ignores the build option. You must build images locally with docker build or push them to a registry before deploying.

4. Deploying with Docker Compose

With your version 3 Compose file in place, start all services:
Sample output:
Here, code_default is the auto-generated network, and all containers are prefixed with the project name (code_).
If you want to run in detached mode, add -d:

5. Handling PostgreSQL Initialization

On first run, PostgreSQL requires a superuser password. Without it, you’ll see:
Add environment variables under the db service:
Restart in detached mode:

PostgreSQL Environment Variables

6. Verifying the Setup

Once all containers are running, open your browser and test the apps:
  1. Cast a vote for one option (e.g., cats).
  2. Confirm the result updates correctly.
  3. Vote again (e.g., dogs) and verify real-time results.
Your Docker Compose setup is now:
  • Version 3 compliant
  • Automatically networked
  • Securely initializing PostgreSQL

Further Reading

Watch Video