build_docker.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/sh
  2. set -eu
  3. export VERSION=${VERSION:-$(git describe --tags --first-parent --abbrev=7 --long --dirty --always | sed -e "s/^v//g")}
  4. export GOFLAGS="'-ldflags=-w -s \"-X=github.com/ollama/ollama/version.Version=$VERSION\" \"-X=github.com/ollama/ollama/server.mode=release\"'"
  5. # We use 2 different image repositories to handle combining architecture images into multiarch manifest
  6. # (The ROCm image is x86 only and is not a multiarch manifest)
  7. # For developers, you can override the DOCKER_ORG to generate multiarch manifests
  8. # DOCKER_ORG=jdoe PUSH=1 ./scripts/build_docker.sh
  9. DOCKER_ORG=${DOCKER_ORG:-"ollama"}
  10. RELEASE_IMAGE_REPO=${RELEASE_IMAGE_REPO:-"${DOCKER_ORG}/release"}
  11. FINAL_IMAGE_REPO=${FINAL_IMAGE_REPO:-"${DOCKER_ORG}/ollama"}
  12. BUILD_ARCH=${BUILD_ARCH:-"amd64 arm64"}
  13. # Set PUSH to a non-empty string to trigger push instead of load
  14. PUSH=${PUSH:-""}
  15. # In CI mode, we break things down
  16. OLLAMA_SKIP_MANIFEST_CREATE=${OLLAMA_SKIP_MANIFEST_CREATE:-""}
  17. OLLAMA_SKIP_IMAGE_BUILD=${OLLAMA_SKIP_IMAGE_BUILD:-""}
  18. if [ -z "${PUSH}" ] ; then
  19. LOAD_OR_PUSH="--load"
  20. else
  21. echo "Will be pushing ${RELEASE_IMAGE_REPO}:$VERSION for ${BUILD_ARCH}"
  22. LOAD_OR_PUSH="--push"
  23. fi
  24. if [ -z "${OLLAMA_SKIP_IMAGE_BUILD}" ]; then
  25. for TARGETARCH in ${BUILD_ARCH}; do
  26. docker build \
  27. ${LOAD_OR_PUSH} \
  28. --platform=linux/${TARGETARCH} \
  29. --build-arg=VERSION \
  30. --build-arg=GOFLAGS \
  31. -f Dockerfile \
  32. -t ${RELEASE_IMAGE_REPO}:$VERSION-${TARGETARCH} \
  33. .
  34. done
  35. if echo ${BUILD_ARCH} | grep "amd64" > /dev/null; then
  36. docker build \
  37. ${LOAD_OR_PUSH} \
  38. --platform=linux/amd64 \
  39. --build-arg=VERSION \
  40. --build-arg=GOFLAGS \
  41. --target runtime-rocm \
  42. -f Dockerfile \
  43. -t ${RELEASE_IMAGE_REPO}:$VERSION-rocm \
  44. .
  45. fi
  46. fi
  47. if [ -z "${OLLAMA_SKIP_MANIFEST_CREATE}" ]; then
  48. if [ -n "${PUSH}" ]; then
  49. docker manifest create ${FINAL_IMAGE_REPO}:$VERSION \
  50. ${RELEASE_IMAGE_REPO}:$VERSION-amd64 \
  51. ${RELEASE_IMAGE_REPO}:$VERSION-arm64
  52. docker manifest push ${FINAL_IMAGE_REPO}:$VERSION
  53. # For symmetry, tag/push the rocm image
  54. if [ "${RELEASE_IMAGE_REPO}" != "${FINAL_IMAGE_REPO}" ]; then
  55. echo "Tagging and pushing rocm image"
  56. docker pull ${RELEASE_IMAGE_REPO}:$VERSION-rocm
  57. docker tag ${RELEASE_IMAGE_REPO}:$VERSION-rocm ${FINAL_IMAGE_REPO}:$VERSION-rocm
  58. docker push ${FINAL_IMAGE_REPO}:$VERSION-rocm
  59. fi
  60. else
  61. echo "Skipping manifest generation when not pushing images are available locally as "
  62. echo " ${RELEASE_IMAGE_REPO}:$VERSION-amd64"
  63. echo " ${RELEASE_IMAGE_REPO}:$VERSION-arm64"
  64. echo " ${RELEASE_IMAGE_REPO}:$VERSION-rocm"
  65. fi
  66. fi