- Write your Python program and make sure it is working as expected.
- Create a
Dockerfile
for your Python program. ADockerfile
is a text file that contains instructions for building a Docker image. Here is an example of aDockerfile
for a Python program:
FROM python:3.8
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["main.py"]
This Dockerfile
specifies that the Docker image should be based on the Python 3.8 base image, and it should copy all the files in the current directory into the /app
directory inside the container. The WORKDIR
directive sets the working directory for any subsequent RUN
, CMD
, and ENTRYPOINT
directives. In this case, the RUN
directive installs the required Python packages listed in requirements.txt
. The ENTRYPOINT
directive specifies the command to run when the container is started, and the CMD
directive provides the default arguments for the ENTRYPOINT
command.
- Build the Docker image using the
Dockerfile
. To build the Docker image, open a terminal and navigate to the directory containing theDockerfile
. Then, run the following command:
docker build -t my-python-app .
This will build a Docker image with the name my-python-app
using the instructions in the Dockerfile
.
- Run the Docker container. To run the Docker container, use the
docker run
command:
docker run -it --rm my-python-app
This will start a new container based on the my-python-app
image, run the python main.py
command inside the container, and then stop and remove the container when the command finishes. The -it
flag runs the container in interactive mode, allowing you to see the output of the program. The --rm
flag removes the container when it exits.
I hope this helps! Let me know if you have any questions or need further clarification.