|
| 1 | +# Building Docker Images |
| 2 | + |
| 3 | +## Cargo Features |
| 4 | + |
| 5 | +The Hyperswitch application server makes extensive use of |
| 6 | +[Cargo features][cargo-features] to selectively toggle certain features and |
| 7 | +integrations. |
| 8 | +The list of features can be found in the `[features]` section in the |
| 9 | +[`crates/router/Cargo.toml`][router-manifest] file. |
| 10 | + |
| 11 | +Of these features, the noteworthy ones are the `default` and `release` features: |
| 12 | + |
| 13 | +- `default`: This feature set enables a basic set of necessary features for both |
| 14 | + development and production environments, such as the transactional APIs, |
| 15 | + APIs used by the control center, Stripe compatibility, automatic payment |
| 16 | + retries, in-memory caching, etc. |
| 17 | + |
| 18 | +- `release`: This feature set enables some additional features that are suitable |
| 19 | + for production environments, such as AWS KMS integration, AWS S3 integration, |
| 20 | + AWS SES integration, etc. |
| 21 | + |
| 22 | +Refer to the [documentation on cargo features][cargo-features] to understand how |
| 23 | +to select features with Cargo, when building the application server. |
| 24 | + |
| 25 | +## Building with the Dockerfile |
| 26 | + |
| 27 | +The Docker images for the application server and other components can be built |
| 28 | +using the [`Dockerfile`][dockerfile] using the below commands, substituting the |
| 29 | +Docker image tags with suitable values: |
| 30 | + |
| 31 | +- router: |
| 32 | + |
| 33 | + ```shell |
| 34 | + docker build \ |
| 35 | + --load \ |
| 36 | + --file Dockerfile \ |
| 37 | + --build-arg "BINARY=router" \ |
| 38 | + --tag hyperswitch-router \ |
| 39 | + . |
| 40 | + ``` |
| 41 | + |
| 42 | +- consumer: |
| 43 | + |
| 44 | + ```shell |
| 45 | + docker build \ |
| 46 | + --load \ |
| 47 | + --file Dockerfile \ |
| 48 | + --build-arg "BINARY=scheduler" \ |
| 49 | + --build-arg "SCHEDULER_FLOW=consumer" \ |
| 50 | + --tag hyperswitch-consumer \ |
| 51 | + . |
| 52 | + ``` |
| 53 | + |
| 54 | +- producer: |
| 55 | + |
| 56 | + ```shell |
| 57 | + docker build \ |
| 58 | + --load \ |
| 59 | + --file Dockerfile \ |
| 60 | + --build-arg "BINARY=scheduler" \ |
| 61 | + --build-arg "SCHEDULER_FLOW=producer" \ |
| 62 | + --tag hyperswitch-producer \ |
| 63 | + . |
| 64 | + ``` |
| 65 | + |
| 66 | +- drainer: |
| 67 | + |
| 68 | + ```shell |
| 69 | + docker build \ |
| 70 | + --load \ |
| 71 | + --file Dockerfile \ |
| 72 | + --build-arg "BINARY=drainer" \ |
| 73 | + --tag hyperswitch-drainer \ |
| 74 | + . |
| 75 | + ``` |
| 76 | + |
| 77 | +When our Docker images are built using the [`Dockerfile`][dockerfile], the |
| 78 | +`cargo` command being run is: |
| 79 | + |
| 80 | +```shell |
| 81 | +cargo build --release --features release ${EXTRA_FEATURES} |
| 82 | +``` |
| 83 | + |
| 84 | +- The `--release` flag specifies that optimized release binaries must be built. |
| 85 | + Refer to the [`cargo build` manual page][cargo-build-manual-page] for more |
| 86 | + information. |
| 87 | + |
| 88 | +- The `--features release` flag specifies that the `release` feature set must be |
| 89 | + enabled for the build. |
| 90 | + Since we do not specify the `--no-default-features` flag to the `cargo build` |
| 91 | + command, the build would have the `default` and `release` features enabled. |
| 92 | + |
| 93 | +- In case you create your own features sets and want to enable them, you can use |
| 94 | + the `${EXTRA_FEATURES}` build argument to specify any additional features that |
| 95 | + would have to be passed to the `cargo build` command. |
| 96 | + The build argument could look as follows: |
| 97 | + `EXTRA_FEATURES="--features feature1,feature2,feature3"`, with actual feature |
| 98 | + names substituted in the command. |
| 99 | + |
| 100 | +## Image Variants on Docker Hub |
| 101 | + |
| 102 | +As of writing this document, we have two image variants available on our Docker |
| 103 | +Hub repositories: |
| 104 | + |
| 105 | +- release: These images contain only the tag that was built, and no other |
| 106 | + suffixes, like the `v1.105.1` and `v1.107.0` Docker images. |
| 107 | + |
| 108 | +- standalone: These images contain the tag that was built with a `standalone` |
| 109 | + suffix, like the `v1.105.1-standalone` and `v1.107.0-standalone` Docker images. |
| 110 | + |
| 111 | +The primary difference is that our standalone Docker images do not have some |
| 112 | +features enabled by default in order to support hosting of Hyperswitch outside |
| 113 | +AWS. |
| 114 | +As of writing this document, the standalone images exclude the `email` and |
| 115 | +`recon` features from the `release` feature set, while the release images are |
| 116 | +built from the Dockerfile without any changes to the codebase after the tag is |
| 117 | +checked out. |
| 118 | + |
| 119 | +If you are building custom images and would like to mirror the behavior of our |
| 120 | +standalone images, then you'd have to remove the `email` and `recon` features |
| 121 | +from the `release` feature set. |
| 122 | + |
| 123 | +## Frequently Asked Questions |
| 124 | + |
| 125 | +### What machine specifications would I need to build release images? |
| 126 | + |
| 127 | +Building release (optimized) images needs significant amount of resources, and |
| 128 | +we'd recommend using a machine with at least 8 cores and 16 GB of RAM for this |
| 129 | +purpose. |
| 130 | +Rust is known to have long compile times, and a codebase of this size will |
| 131 | +require a significant time to build, around 45-60 minutes for the above |
| 132 | +configuration. |
| 133 | + |
| 134 | +### Build seems to be stuck at "Compiling router/scheduler/analytics/..." |
| 135 | + |
| 136 | +The compilation process involves compiling all of our dependencies and then |
| 137 | +compiling our workspace (first-party) crates, among which the biggest one |
| 138 | +(in terms of lines of code) is the `router` crate. |
| 139 | +Once all the dependencies of the `router` crate have been built, one of the last |
| 140 | +ones being built is the `router` crate itself. |
| 141 | + |
| 142 | +As mentioned above, building release images takes a significant amount of time. |
| 143 | +It is normal to see nothing else being printed after a line which says |
| 144 | +`Compiling router / scheduler / analytics / ...`. We'd suggest waiting |
| 145 | +for a while. |
| 146 | +If you're still concerned that the compilation process has been stuck for far |
| 147 | +too long, you can check if at least one CPU is being utilized by |
| 148 | +`cargo` / `docker` using a tool like `htop` (if you can access the machine which |
| 149 | +is building the code). Worst case, you can proceed to kill the compilation |
| 150 | +process and try again. |
| 151 | + |
| 152 | +[cargo-features]: https://doc.rust-lang.org/cargo/reference/features.html |
| 153 | +[router-manifest]: https://github.com/juspay/hyperswitch/blob/main/crates/router/Cargo.toml |
| 154 | +[dockerfile]: https://github.com/juspay/hyperswitch/blob/main/Dockerfile |
| 155 | +[cargo-build-manual-page]: https://doc.rust-lang.org/cargo/commands/cargo-build.html |
0 commit comments