Skip to content

Pytorch

Exemplo de Dockerfile single-stage para python/pytorch.

# original base image
FROM pytorch/pytorch:2.3

# updating to avoid security issues
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get clean -y && \
    apt-get autoremove -y && \
    rm -rf /var/lib/apt/lists/*

# set working directory
WORKDIR /app

# copy the requirements from project to container
COPY ./requirements.txt /app

# install the dependencies
RUN pip install --no-cache-dir -r requirements.txt

# creating user and grupo to run unprivileged
RUN addgroup --system app && adduser --system --group app

# definind user o run
USER app

# copy the application code
COPY --chown=app:app . /app

# expose the port
EXPOSE 5000

# defining entrypoind and cmd
ENTRYPOINT ["python"]
CMD ["api.py"]

Exemplo de docker-compose para essa app.

---
services:

  pytorch_app:
    build: .
    container_name: pytorch_app
    restart: always
    ports:
      - 5000:5000
    networks:
      - pytorch_app

networks:
  pytorch_app: