This project demonstrates how to Dockerize a simple Python-based Tic-Tac-Toe game. By the end of this guide, you'll have a containerized Python application that you can run anywhere using Docker.
To get started, clone the repository and navigate into the project folder:
git clone https://github.com/YogitaBadhe/Dockerizing_python_Code.git
cd Dockerizing_python_Code
Before you begin, ensure that you have the following installed:
Ensure that Docker is installed on your system. If not, you can install Docker using the following command:
sudo yum install docker -y
You need Git to clone the repository. If you don't have Git installed, use:
sudo yum install git -y
You will need Python installed to test the Tic-Tac-Toe game. Install Python if it's not already on your system:
sudo yum install python -y
Start the Docker service and enable it to start on boot:
sudo systemctl start docker
sudo systemctl enable docker
Verify your Docker installation:
docker --version
Configure your Git user details (optional but recommended):
git config --global user.name "YogitaBadhe"
git config --global user.email "[email protected]"
Create the Python script for your Tic-Tac-Toe game:
sudo touch tic-tac-toe.py
sudo nano tic-tac-toe.py
Paste your Python code into the file, then test it locally:
python3 tic-tac-toe.py
Create a Dockerfile
in the root of your project directory:
touch Dockerfile
sudo nano Dockerfile
Add the following content to the Dockerfile
:
# Use the official Python image from the Docker Hub
FROM python:3.8-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Run tic-tac-toe.py when the container launches
CMD ["python", "tic-tac-toe.py"]
-
Log in to Docker Hub (if you haven’t already):
sudo docker login
-
Build the Docker image:
sudo docker build -t ybadhe/pythonproject .
-
Verify the Docker image is built:
sudo docker images
-
Push the Docker image to Docker Hub:
sudo docker push ybadhe/pythonproject
-
Run your Docker container:
sudo docker run ybadhe/pythonproject
-
List all running Docker containers:
sudo docker ps -a
Once the container is running, you'll interact with the Tic-Tac-Toe game through the terminal. The game will prompt you to enter row and column numbers for player moves (e.g., 0 1
for row 0, column 1).
Tic-Tac-Toe Game
| X | | |
---------
| | O | |
---------
| | | X |
Player X, enter row and column (0-2): 1 1
Player O, enter row and column (0-2): 0 0
...
To stop the container, use the following command:
sudo docker stop <container_id>
You can get the <container_id>
by running:
sudo docker ps -a
By following these steps, you have successfully Dockerized a Python Tic-Tac-Toe game. You can now run the game as a Docker container on any machine that supports Docker.
If you encounter any issues, please open an issue on the repository, and I'll assist you as soon as possible.
This README provides clear instructions for setting up, Dockerizing, and running the Python Tic-Tac-Toe game.