9951 explained code solutions for 126 technologies


python-pytorchHow do I create a Docker image for a Python/PyTorch application?


  1. Create a Dockerfile in the root directory of your project.
FROM python:3.7
RUN pip install torch
WORKDIR /app
COPY . /app
CMD python main.py
  1. Build the Docker image using the docker build command.
docker build -t my-python-app .
  1. Run the image using the docker run command.
docker run -it --rm --name my-running-app my-python-app

This will create a Docker image for a Python/PyTorch application.

Parts of the Dockerfile:

  • FROM python:3.7 - This specifies the base image to use for the Docker image.
  • RUN pip install torch - This command will install PyTorch in the Docker image.
  • WORKDIR /app - This sets the working directory for the Docker image.
  • COPY . /app - This copies the project files to the Docker image.
  • CMD python main.py - This specifies the command to run when the image is executed.

Helpful links

Edit this code on GitHub