- Write your Python program and make sure it is working as expected.
- Create a
Dockerfilefor your Python program. A
Dockerfileis a text file that contains instructions for building a Docker image. Here is an example of a
Dockerfilefor a Python program:
FROM python:3.8 COPY . /app WORKDIR /app RUN pip install -r requirements.txt ENTRYPOINT ["python"] CMD ["main.py"]
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
ENTRYPOINT directives. In this case, the
RUN directive installs the required Python packages listed in
ENTRYPOINT directive specifies the command to run when the container is started, and the
CMD directive provides the default arguments for the
- Build the Docker image using the
Dockerfile. To build the Docker image, open a terminal and navigate to the directory containing the
Dockerfile. 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
- Run the Docker container. To run the Docker container, use the
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.