Pytorch, an open-source ML library, is popular among researchers due to its dynamic computing graph and efficient GPU acceleration. The heart of the Pytorch lies in its tensor library, which is the foundation of neural networks.
PermalinkDocker
In the world of modern software development, Docker has emerged as a transformative technology that simplifies the development and management of applications.
Docker installation link below:
https://docs.docker.com/engine/install/
PermalinkDockerfile
The Dockerfile is a script that instructs Docker on how to build a container image. It specifies the base image, set up the environment, copies files, installs dependencies, and configures runtime settings.
Here's a breakdown of the key concept in a Dockerfile that is needed in setting up Pytorch.
Base Image: Choose a base image that provides the foundation of the container. It could be a minimal OS, a programing language, or a framework like pytorch which we will use.
RUN: The RUN instruction in a Dockerfile is used to execute commands during the image build process. It is typically employed to install packages, setup dependencies, and perform any necessary configuration.
WORKDIR: The WORKDIR instruction sets the working directory for any subsequent instructions in the Dockerfile.
CMD: The CMD instruction specifies the default command to run when a container is started from the image.
#Use latest pytorch base image
FROM pytorch/pytorch:latest
#Upgrade pip
RUN pip install --upgrade pip
#install essential libraries
RUN pip install numpy pandas opencv-python
#Setting the default working directory
WORKDIR /app
#Run a command in the background that will keep container running
CMD while true;do echo "Hello,world!"; sleep 1; done
Permalinkdocker-compose.yaml
While the Dockerfile defines a single container, the docker0compoase.yaml file enables you to define and manage multi-container applications. Let's write a compose file.
Services: Define each service(container) you want to run as a separate section in the file.
pytorch-container defines a service name built from the Dockerfile.
build section specifies the build context, which is the directory containing the Dockerfile and any resource needed during the build process.
dockerfile: Specifies the name of the Dockerfile to use for building the image.
ports: Maps port 8080 on the host to port 8080 on the container for the service. This allows you to access the web application running the docker via 'localhost:8080'
volumes: ./ copy all the files in the docker-compose.yaml on the host into the /app directory in the pytorch-container. This allows you to make changes to the code on your local machine and have them immediately reflected in the container.
version : '3'
services:
pytorch-container:
build:
context: .
dockerfile: Dockerfile
ports:
- 8080:8080
volumes:
- ./:/app
PermalinkBuild the docker image
sudo docker-compose up --build
Execute the docker-compose.yaml
sudo docker exec -it pytorch-container bash