0
0
Dockerdevops~5 mins

Compose file versioning in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Docker Compose files use versions to define how the file is read and which features are available. Using the right version helps Docker understand your app setup and run it correctly.
When you want to run multiple containers together with specific settings in one file.
When you need to use new Docker Compose features that require a certain file version.
When you want to ensure your Compose file works with the Docker Compose tool installed on your system.
When you want to share your app setup with others and make sure it runs the same way.
When you want to upgrade your Compose file to use newer syntax or capabilities.
Config File - docker-compose.yml
docker-compose.yml
version: '3.9'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
  db:
    image: postgres:14
    environment:
      POSTGRES_PASSWORD: example

version: Specifies the Compose file format version. Version 3.9 is the latest stable and supports modern features.

services: Defines the containers to run. Here, a web server and a database.

web: Runs an Nginx container exposing port 80 inside mapped to 8080 on the host.

db: Runs a Postgres container with a password set via environment variable.

Commands
Starts the containers defined in the docker-compose.yml file in detached mode so they run in the background.
Terminal
docker compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating volume "default_pgdata" with default driver Creating myapp_db_1 ... done Creating myapp_web_1 ... done
-d - Run containers in the background (detached mode)
Lists the running containers started by Docker Compose to verify they are up and running.
Terminal
docker compose ps
Expected OutputExpected
NAME COMMAND SERVICE STATUS PORTS myapp_db_1 "docker-entrypoint.s…" db running myapp_web_1 "nginx -g 'daemon of…" web running 0.0.0.0:8080->80/tcp
Validates the docker-compose.yml file syntax and version compatibility without printing the full config.
Terminal
docker compose config --quiet
Expected OutputExpected
No output (command runs silently)
--quiet - Only show errors, no output if valid
Key Concept

If you remember nothing else from this pattern, remember: the Compose file version controls which features and syntax you can use and ensures Docker runs your app correctly.

Common Mistakes
Using an outdated version number like '2' when you want to use newer features.
Older versions do not support the latest Compose features and may cause errors or unexpected behavior.
Use the latest stable version like '3.9' to access all current features.
Omitting the version key entirely in the Compose file.
Docker Compose may default to an older version or fail to parse the file correctly.
Always specify the version explicitly at the top of the file.
Mixing syntax from different Compose versions in the same file.
This causes parsing errors because syntax rules differ between versions.
Stick to the syntax rules of the version declared in the file.
Summary
The Compose file version defines the syntax and features available in your docker-compose.yml file.
Use 'docker compose up -d' to start containers defined in the Compose file in the background.
Use 'docker compose ps' to check running containers and 'docker compose config --quiet' to validate the file.