📖 Scenario: You are setting up a Docker Compose file to run a simple web application. You want to specify the version of the Compose file format to ensure compatibility with your Docker environment.
🎯 Goal: Create a Docker Compose file with a specific version, add a service configuration, and display the version used.
📋 What You'll Learn
Create a Docker Compose file with version '3.8'
Add a service named 'web' using the 'nginx:latest' image
Print the Compose file version to confirm
💡 Why This Matters
🌍 Real World
Docker Compose files are used to define and run multi-container Docker applications. Specifying the version ensures compatibility with Docker features.
💼 Career
Understanding Compose file versioning helps in configuring containerized applications reliably, a key skill for DevOps roles.
Progress0 / 4 steps
1
Create the Compose file with version
Create a dictionary called compose_file with a key version set to the string '3.8'.
Docker
Hint
Use a Python dictionary with the key 'version' and value '3.8'.
2
Add a service configuration
Add a key services to the compose_file dictionary. Set it to a dictionary with a key web whose value is another dictionary with the key image set to 'nginx:latest'.
Docker
Hint
Remember to nest dictionaries for services and the web service configuration.
3
Access the Compose file version
Create a variable called version_used and set it to the value of the version key in the compose_file dictionary.
Docker
Hint
Use dictionary key access to get the version value.
4
Print the Compose file version
Write a print statement to display the text "Compose file version: " followed by the value of version_used.
Docker
Hint
Use string concatenation or f-string to combine the text and variable.
Practice
(1/5)
1. What does the version field in a Docker Compose file specify?
easy
A. The version of the operating system hosting Docker
B. The version of the Docker Engine installed on the system
C. The version of the application running inside the container
D. The format and features Docker Compose should use to read the file
Solution
Step 1: Understand the role of the version field
The version field tells Docker Compose which syntax and features to expect in the file.
Step 2: Differentiate from other version types
This field is not about Docker Engine, app, or OS versions but about Compose file format.
Final Answer:
The format and features Docker Compose should use to read the file -> Option D
Quick Check:
Compose file version = file format and features [OK]
Hint: Version field controls Compose syntax, not Docker or app versions [OK]
Common Mistakes:
Confusing Compose file version with Docker Engine version
Thinking it sets app container version
Assuming it relates to OS version
2. Which of the following is the correct way to specify the Compose file version in YAML?
easy
A. version: "3.9"
B. version : 3.9
C. version = "3.9"
D. "version" : 3.9
Solution
Step 1: Recall YAML syntax for key-value pairs
YAML uses colon and space to assign values, and strings should be quoted if they contain dots.
Step 2: Identify correct quoting for version
The version number with a decimal should be quoted as a string: "3.9".
Final Answer:
version: "3.9" -> Option A
Quick Check:
YAML version field quoted with colon = correct [OK]
Hint: Use colon and quotes for version like version: "3.9" [OK]
When running docker-compose up, you get an error. What is the likely cause?
medium
A. The ports mapping syntax is incorrect
B. The image name is invalid without a tag
C. The version number is not quoted, causing a YAML parsing error
D. Docker Compose does not support version 3.8
Solution
Step 1: Identify YAML syntax issue
The version field with a decimal must be quoted as a string to avoid YAML parsing errors.
Step 2: Check other fields for errors
Image name without tag is valid (defaults to latest), ports syntax is correct, and version 3.8 is supported.
Final Answer:
The version number is not quoted, causing a YAML parsing error -> Option C
Quick Check:
Unquoted decimal version causes YAML error [OK]
Hint: Always quote version numbers with decimals in Compose files [OK]
Common Mistakes:
Not quoting version with decimals
Assuming image needs explicit tag
Misreading ports syntax as error
5. You want to use a new Compose feature available only in version 3.9, but your current Compose file uses version "3.7". What is the best way to update your Compose file to use the new feature safely?
hard
A. Add the new feature without changing the version field, it will work automatically
B. Change the version field to "3.9" and test the file with your Docker Compose version
C. Downgrade your Docker Compose to an older version that supports 3.7 features only
D. Remove the version field entirely to let Docker Compose decide
Solution
Step 1: Update the version field to match the new feature
Set the version field to "3.9" to indicate use of new features introduced in that version.
Step 2: Test compatibility with your Docker Compose version
Ensure your Docker Compose installation supports version 3.9 to avoid errors.
Final Answer:
Change the version field to "3.9" and test the file with your Docker Compose version -> Option B
Quick Check:
Update version field and verify Compose support [OK]
Hint: Match version field to features and test Compose version support [OK]
Common Mistakes:
Using new features without updating version field
Removing version field causing unpredictable behavior
Ignoring Compose version compatibility
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.