{"slug": "building-a-pytorch-container-image", "title": "Building a PyTorch Container Image", "summary": "A developer documented a workflow for cloning the PyTorch source repository and building custom container images from source for both CPU and GPU systems, then pushing the resulting image to a container registry. The CPU build uses the repo's Dockerfile and produced an 11.9GB image, while the GPU build relies on docker.Makefile with CUDA dependencies and took roughly two hours to complete.", "body_md": "PyTorch is a flexible machine learning library for building deep learning models that can perform a wide range of tasks such as image recognition and language processing. This guide walks through cloning the PyTorch source repository, building a container image for either CPU or GPU systems, and pushing the finished image to a container registry. By the end, you'll have a custom PyTorch container image built from source and published to a registry, ready to pull into any Docker-compatible environment.\n\nStart the Docker service.\n\n``` bash\n$ sudo systemctl start docker\n```\n\nAdd the non-root user to the Docker group. For example, `linuxuser`.\n\n``` bash\n$ sudo usermod -aG docker linuxuser\n```\n\nSwitch to the user:\n\n``` bash\n$ su - linuxuser\n```\n\n**1. Create a new directory to store your PyTorch project files:**\n\n``` bash\n$ mkdir llama-project\n```\n\n**2. Switch to the directory:**\n\n``` bash\n$ cd llama-project\n```\n\n**3. Clone the PyTorch project repository using Git:**\n\n``` bash\n$ git clone https://github.com/pytorch/pytorch\n```\n\n**4. List all files and verify that a new `pytorch` directory is available:**\n\n``` bash\n$ ls\n```\n\n**5. Switch to the `pytorch` project directory:**\n\n``` bash\n$ cd pytorch\n```\n\n**6. List all directory files and verify the available Dockerfile resources:**\n\n``` bash\n$ ls -a\n```\n\nThe PyTorch project directory includes the following Dockerfile resources:\n\n`Dockerfile`: Contains the PyTorch build context for CPU systems.` docker.makefile`: Contains advanced GPU build context and image automation directives with support for custom dependency packages.\nUse these resources in the next sections to build a CPU or GPU system container image.\n\nFollow the steps below to build a new PyTorch container image for CPU systems using the `Dockerfile` resource on your server.\n\n**1. Build a new PyTorch container image using the `Dockerfile` resource. Replace `pytorch-image` with your desired image name:**\n\n``` bash\n$ docker build -f Dockerfile -t pytorch-image .\n```\n\n**2. View all Docker images and verify that your new PyTorch image is available:**\n\n``` bash\n$ docker images\n```\n\nOutput:\n\n```\nREPOSITORY     TAG                                                   IMAGE ID       CREATED       SIZE\npytorch-image  latest                                                d3674f1db4d5   2 hours ago   11.9GB\n```\n\nFollow the steps below to build a new PyTorch container image for GPU systems using `docker.Makefile`, which contains the CUDA dependency build context compatible with NVIDIA GPU servers.\n\n**1. Build a new PyTorch container image for GPU using the `docker.Makefile`:**\n\n``` bash\n$ docker build -f Dockerfile -t pytorch-image .\n```\n\nWait at least `2 hours` for the build process to complete. When successful, your output should look like the one below:\n\n``` php\n#29 29224.1 copying torch/include/ATen/ops/_sparse_coo_tensor_with_dims_ops.h -> build/lib.linux-x86_64-cpython-310/torch/include/ATen/ops\n#29 29224.1 copying torch/include/ATen/ops/absolute_compositeimplicitautograd_dispatch.h -> build/lib.linux-x86_64-cpython-310/torch/include/ATen/ops\n#29 29224.1 copying torch/include/ATen/ops/gradient_ops.h -> build/lib.linux-x86_64-cpython-310/torch/include/ATen/ops\n#29 29224.1 copying torch/include/ATen/ops/crow_indices_compositeexplicitautograd_dispatch.h -> build/lib.linux-x86_64-cpython-310/torch/include/ATen/ops\n#29 29224.1 copying torch/include/ATen/ops/isinf.h -> build/lib.linux-x86_64-cpython-310/torch/include/ATen/ops\n#29 29224.1 copying torch/include/ATen/ops/_fore\n#29 29224.1 [output clipped, log limit 2MiB reached]\n#29 29235.3 warning: no previously-included files matching '*.o' found anywhere in distribution\n#29 29235.4 warning: no previously-included files matching '*.dylib' found anywhere in distribution\n#29 29235.7 warning: no previously-included files matching '*.swp' found anywhere in distribution\n#29 DONE 29239.6s\n\n#30 [dev 1/1] COPY --from=build /opt/conda /opt/conda\n#30 DONE 10.8s\n\n#31 exporting to image\n#31 exporting layers\n#31 exporting layers 41.0s done\n#31 writing image sha256:d3674f1db4d5e9585a5e5b64a0cdfcb0c7d312358e267000e23b6fa9d8e7e917 done\n#31 naming to docker.io/root/pytorch:v1.8.0-rc1-38416-g4dc160864b3-cuda12.1-cudnn8-devel done\n#31 DONE 41.0s\n```\n\n**2. View all Docker images on the server and verify that a new `root/pytorch` image is available:**\n\n``` bash\n$ docker images\nREPOSITORY     TAG                                                   IMAGE ID       CREATED       SIZE\nroot/pytorch   v1.8.0-rc1-38416-g4dc160864b3-cuda12.1-cudnn8-devel   d3674f1db4d5   3 hours ago   20.9GB\n```\n\n**3. Verify the PyTorch GPU image's target CUDA version and confirm your system uses that driver version or higher:**\n\n``` bash\n$ cat docker.Makefile | grep CUDA_VERSION\nCUDA_VERSION_SHORT       ?= 12.1\nCUDA_VERSION             ?= 12.1.1\nBASE_DEVEL                = nvidia/cuda:$(CUDA_VERSION)-cudnn$(CUDNN_VERSION)-devel-ubuntu22.04\n```\n\nBased on the above output, NVIDIA GPU servers with CUDA version `12.1` or higher can run your PyTorch GPU container image.\n\n**1. Open your container registry's management panel and copy the registry URL, username, and API key (or password) to use when accessing the registry.**\n\n**2. Switch to your server terminal session and log in to your container registry. Replace `your-registry-url`, `exampleregistry`, `exampleuser`, and `registry-password` with your actual registry details:**\n\n``` bash\n$ docker login https://your-registry-url/exampleregistry -u exampleuser -p registry-password\n```\n\n**3. Tag your PyTorch container image with your desired registry tag. For example, tag your PyTorch GPU image ID with a registry repository tag such as `your-registry-url/exampleregistry/pytorch-gpu-image`:**\n\n``` bash\n$ docker tag d3674f1db4d5 your-registry-url/exampleregistry/pytorch-gpu-image\n```\n\nThe above command tags the Docker image with container ID `d3674f1db4d5` with your registry tag.\n\n**4. View all Docker images on the server and verify that the new tagged image is available:**\n\n``` bash\n$ docker images\nREPOSITORY           TAG                                                   IMAGE ID       CREATED       SIZE\nroot/pytorch         v1.8.0-rc1-38416-g4dc160864b3-cuda12.1-cudnn8-devel   d3674f1db4d5   4 hours ago   20.9GB\nyour-registry-url/exampleregistry/pytorch-gpu-image   latest                 d3674f1db4d5   4 hours ago   20.9GB\n```\n\n**5. Push the tagged image to your container registry repository:**\n\n``` bash\n$ docker push your-registry-url/exampleregistry/pytorch-gpu-image\n```\n\n**6. Open your container registry's management panel and check the repositories list to verify that the new repository is available.**\n\nFor the full guide with additional tips, visit the original article on **[Vultr Docs](https://docs.vultr.com/how-to-build-a-pytorch-container-image)**.", "url": "https://wpnews.pro/news/building-a-pytorch-container-image", "canonical_source": "https://dev.to/vultr/building-a-pytorch-container-image-i3l", "published_at": "2026-10-29 17:05:13+00:00", "updated_at": "2026-10-29 17:20:27.783632+00:00", "lang": "en", "topics": ["machine-learning", "ai-infrastructure", "developer-tools", "mlops"], "entities": ["PyTorch", "Docker", "NVIDIA", "GitHub"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/building-a-pytorch-container-image", "markdown": "https://wpnews.pro/news/building-a-pytorch-container-image.md", "text": "https://wpnews.pro/news/building-a-pytorch-container-image.txt", "jsonld": "https://wpnews.pro/news/building-a-pytorch-container-image.jsonld"}}