
In-Class Exercise - Docker Commit Workflow¶
- Start an
alpinecontainer and keep it running for modifications - Create a new file inside the running container
- Use
docker committo capture a new image with the file included - Run a container from the committed image and verify the file exists
- Hint: Combine
docker run,docker exec,docker commit, anddocker run --rm
Solution
Step-by-Step Solution¶
- Run a modifiable alpine container
- Write a file inside the running container
- Validate the file inside the original container (optional check)
- Create a new image from the modified container
- Run a container from the committed image and verify the file exists
Expected output:
- Clean up resources
Explanation¶
- docker run -d … sleep infinity: Starts a container that stays alive for edits
- docker exec … echo ‘…’ > file: Writes a file into the running container
- docker commit: Captures the container’s filesystem changes into a new image
- docker run –rm new-image cat file: Launches the new image, verifies the persistent file, and removes the container when done
- Cleanup commands: Remove the temporary container and image to free resources