Deploy canister with preset canister id

Is there a way to set the canister id to be deployed before calling dfx deploy …? so that when I do some local tests of the ledger or ckbtc, etc I can use the same canister id in my tests and mo files?
I try doing this by presetting my canister_ids.json i.e.

{
 "canister": {
    "local": "mc6ru-gyaaa-aaaar-qaaaq-cai"
  },
}

but I either get

Error: Failed while trying to deploy canisters.
Caused by: Failed while trying to deploy canisters.
  Failed while trying to install all canisters.
    Failed to install wasm module to canister 'ckbtc'.
      Failed to install wasm in canister 'mc6ru-gyaaa-aaaar-qaaaq-cai'.
        Failed to install wasm.
          The replica returned an HTTP Error: Http Error: status 404 Not Found, content type "", content: Canister mc6ru-gyaaa-aaaar-qaaaq-cai not found

or it sets the canister id in .dfx/local/canister_ids.json to a completely new canister id

2 Likes

Not sure because I am no dfx expert and no direct answer but, canister ids deployed locally are predictable in the sense that if you deploy your canisters always in the same order, you will always get the same canister ids.

So that’s what I do. I either document the order in a README.md or create a script that deploys the canisters and that way I can use the same canister id in my code for local dev.

1 Like

appreciate the input, but yea I wanted it so that if I deploy a local icp ledger or local ckbtc ledger on a test server, if I move over to mainnet I didnt want to have to go to all my mo files and change the ids. This is useful to know though.

canister ids deployed locally are predictable in the sense that if you deploy your canisters always in the same order, you will always get the same canister ids.

Albeit, for some reason for the one of my canisters I was able to actually change the canister id in .dfx/local/canister_ids.json and it worked. so yea idk

1 Like

Hope someone with better dfx can answer here. For what it’s worth and if it can help too maybe, there is also a thread about “env files for Motoko” ENV variables for motoko builds - #8 by paulyoung

dfx.json has a feature for remote canister support: sdk/CHANGELOG.md at master · dfinity/sdk · GitHub

e.g. to specificy Ledger and II existing canister IDs on mainnet, I’ve got following in my dfx:

"internet_identity": {
			"type": "custom",
			"candid": "internet_identity.did",
			"wasm": "internet_identity.wasm",
			"build": "bash -c 'test -f internet_identity.wasm || curl -sSL https://github.com/dfinity/internet-identity/releases/latest/download/internet_identity_dev.wasm -o internet_identity.wasm; test -f internet_identity.did || curl -sSL https://raw.githubusercontent.com/dfinity/internet-identity/main/src/internet_identity/internet_identity.did -o internet_identity.did'",
			"remote": {
				"candid": "internet_identity.did",
				"id": {
					"ic": "rdmx6-jaaaa-aaaaa-aaadq-cai"
				}
			}
		},
		"ledger": {
			"type": "custom",
			"candid": "ledger.public.did",
			"wasm": "ledger.wasm",
			"remote": {
				"candid": "ledger.public.did",
				"id": {
					"ic": "ryjl3-tyaaa-aaaaa-aaaba-cai"
				}
			}
		}

@paulyoung and @peterparker but thats basicallly to specify an already existing canister id on the specific network. Not if I wanted to create a canister with a specific id

Because I’m doing something where I want to update some hashmaps that use the canister-id’s/canister-principals as keys, I want to make my local canisters have the canister-ids of the already existing network canisters. You get what I mean?

I do understand thats for the specifying the interface based on either local or ic network but its not for the principal.

I think you can pre-populate .dfx/local/canister_ids.json with any canister IDs you like and they will get used locally.

yea I tried that and I got this

Caused by: Failed while trying to deploy canisters.
  Failed while trying to install all canisters.
    Failed to install wasm module to canister 'ckbtc'.
      Failed to install wasm in canister 'mc6ru-gyaaa-aaaar-qaaaq-cai'.
        Failed to install wasm.
          The replica returned an HTTP Error: Http Error: status 404 Not Found, content type "", content: Canister mc6ru-gyaaa-aaaar-qaaaq-cai not found

If I swap one of the autogenerated ones with a custom canister it works. I.e. like if I created 5 canisters and the first one in the canister_ids.json was ryjl3-tyaaa-aaaaa-aaaba-cai and I set the fifth one to that id, it will work. But not a new fresh id.

1 Like

It would be very convenient to be able to specify an id for a local canister (for example, for ledger, token canisters, NFT canisters).

Using the root canister_ids.json seems like a good option.

2 Likes

Seconded, this feature would be really useful to have! Not just for convenience but other tooling I have in mind.

Best guess: The canister is not created yet, so there’s no way to install anything to it.

As for specifying specific IDs for local canisters: we’re pretty close to having this, now it’s a matter of prioritisation. EDIT: I misread what this PR does If someone wants to cut the line by making a PR: @mraszyk did all the hard work in this PR, now it’s just a matter of exposing the parameters in a sensible manner.
The interface to support is described in the spec.

2 Likes

@Severin the link that you provided seems to be broken. I’m also looking for a way to define a local canister id for testing of canisters in local development. Do you have an updated reference for this?

EDIT: just found that this is possible with the deploy command directly and setting the flag --specified-id <canister-id> (see reference description). Would still love to have an option to define it in dfx.json or canister_ids.json for the local deployment.

1 Like