This is a Python console application that counts words in a sentence and saves the result in a file. It demonstrates Docker containerization concepts, including:
- Building and running containers
- Using Docker volumes for persistent data
- Managing containerized Python applications
- Clone the repository:
git clone https://github.com/Bmeera/docker-word-counter.git
cd docker-word-counter\
- Build the Docker image:
docker build -t word-counter .
This creates a Docker image named word-counter.
- Run the container interactively and with volume mounting:
docker run -it --name mycounter -v ~/word_counter/data:/app/data word-counter
✅ You will be prompted to enter a sentence.
✅ The program will count the words in the sentence, display it, and save the result in word_count.txt.
- Check the Saved File
To check the file inside the container:
docker start mycounter
docker exec -it mycounter bash
cat /app/data/word_count.txt
To check the file outside the container
First, exit the container
exit
Then, run:
cat ~/word_counter/data/word_count.txt
- Input a sentence
- Press the enter key
- The word count of the sentence is displayed and saved to word_count.txt
Example:
- Input a sentence: I am an amazing Software Engineer
- Press the enter key
- The word count is: 6
- The word count of the sentence is displayed and saved to word_count.txt
The following Docker concepts are used:
- Base Image: A base image is the foundation of a Docker container. Uses python:3.9-slim for a lightweight container.
- Containerization: App runs in an isolated Docker environment.
- Persistent Storage: Docker volumes save data permanently.
- Tagging & Versioning: Version 1.0 tagged as:
docker tag word-counter word-counter:1.0
Screenshot 1
Screenshot 2
docker stop mycounter
docker rm mycounter
- Create a web-based version using Flask.
Feel free to fork this repository and contribute improvements! If you found this useful, ⭐ Star this repo!