This article explores upgrading a Docker Compose file to version 3, highlighting configuration improvements and enhanced networking features.
In this article, we’ll explore an enhanced version of a Docker Compose file, showcasing improvements over the original version. This guide details the migration from an older, simpler version of the Compose file to a more advanced version 3 format, enabling better configuration and robust networking features.
Previously, we demonstrated a very basic, version 1 Docker Compose file. While the older format is straightforward, it lacks support for advanced features. Therefore, we will upgrade the configuration to version 3.
For more details on the differences between Docker Compose file versions, consult the Docker Documentation. Although a compatibility matrix is available in the documentation, this article focuses on real-world changes, so no visual diagram is provided.
Organizing Configuration with the Services Section
In version 3, migrate all configuration settings under a top-level services section. For example, when using version 3.9, your Docker Compose file might look like this:
Copy
Ask AI
version: "3.9"services: webapp: build: ./dir
With more advanced options, you can specify the build context and Dockerfile explicitly:
The following updated Docker Compose file demonstrates these changes by specifying the version and nesting all service definitions under the services key. In Visual Studio Code, simply indent your existing service definitions under services:.
With Docker Compose version 3, a default network is automatically created for all services. This allows containers to communicate using service names (e.g., the vote service can reach redis or db simply by their names). Consequently, the explicit links section is no longer necessary.
Once you’ve updated your Compose file, deploy your application using the docker-compose up command. If your Docker Compose file resides in a directory named “code”, the directory name is used as the project name:
Copy
Ask AI
admin@docker-host $ docker-compose upWARNING: The Docker Engine you're using is running in swarm mode.Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.To deploy your application across the swarm, use `docker stack deploy`.Creating network "code_default" with the default driverCreating code_worker_1 ... doneCreating code_redis_1 ... doneCreating code_result_1 ... doneCreating code_db_1 ... doneCreating code_vote_1 ... done
Below is the final version of our Docker Compose file without the obsolete links:
The Docker Compose process creates objects like networks and containers with names prefixed by the project name (in this case, “code”). For example, the network is named “code_default” and the containers follow a similar nomenclature (e.g., “code_worker_1”).
At deployment, you might encounter errors such as “no such device or address” or “waiting for DB” in the logs. These indicate that the worker and result services are having trouble connecting to the PostgreSQL database. This issue occurs because the PostgreSQL image now requires a default superuser password via the POSTGRES_PASSWORD environment variable.To address this, update the DB service by adding the necessary environment variables. The configuration below sets POSTGRES_USER and POSTGRES_PASSWORD to “postgres” for consistency:
You can now verify the setup by accessing the voting app at http://localhost:5000 and viewing the results app on port 5001. When you cast a vote for “cats,” the results should immediately update to show 100% for cats. Changing your vote to “dogs” should prompt a similar update, confirming that the configuration is correct and the Docker Compose file is working as intended.That’s it for this lesson. Happy Dockering, and see you in the next article!