The first login
Every other chapter of this guide can be done at leisure. This one cannot. A fresh Portainer has no users at all, and its first administrator is created by whoever completes the setup first — so the setup belongs in the same sitting as the install, and Portainer guards it accordingly. By the end of this chapter the console has an administrator (you), the setup door is closed behind you, and you have signed in through the ordinary door and been given the token the API runs on.
Check: GET /api/users/admin/check
answers 404 before setup and 204 —
an empty "yes, an admin exists" —
after it. The lab's transcript
(portainer-lab/transcripts/t02-admin-init-and-auth.txt)
shows both.
The setup token
Older Portainer versions guarded the first login with a clock: finish setup within a few minutes of starting, or restart the server and finish within the new window. Current versions guard it with a setup token instead, which is a better lock. At startup, with no administrator in its database, Portainer prints a long random token into its own log:
docker logs portainer
look for the lines that offer a
setup_token=... and tell you to
paste it into the setup screen, or
send it in the X-Setup-Token header.
Whoever holds that token may create
the first administrator; nobody else
may. Reading the log requires access
to the machine or its engine — which
is the point: the right to claim the
console is proven by already having
the keys to the house.
The lab proved the lock works before using it. Its first attempt to create the administrator without the token was refused, in so many words (transcript t02):
{"message":"Invalid or missing setup token. Provide
the X-Setup-Token header with the token printed in
the server logs at startup.","details":"Invalid or
missing setup token"}
HTTP 403
And with the token, the same request
created the user and returned its
record — Id: 1, Role: 1 (an
administrator), and a password field
holding only a hash, a one-way
scramble; Portainer never keeps the
password itself.
Doing it in the browser
Open https://<the machine>:9443
(accept the certificate warning you
were promised in chapter 3), and the
setup screen asks for three things: a
username, a password of at least
twelve characters, and the setup
token. Choose the password like the
privileged door it is (chapter 1): a
phrase you do not reuse anywhere.
When you submit, two things happen at
once — your administrator exists, and
the setup route stops working for
everyone, token or not. The lab
watched admin/check flip from 404 to
204 at exactly that moment.
There is no "later" trick, and no back door kept for convenience: if the token is lost before setup, restart the container and read the fresh log; if an administrator already exists and their password is lost, that is a different rescue, and the troubleshooting chapter covers it.
Signing in, and the token you get
After setup, the ordinary door is a
username and password. Signing in
(POST /api/auth, transcript t02)
returns a JWT — a signed, expiring
pass, a few hundred characters — which
the browser keeps for you and the API
takes as Authorization: Bearer <jwt>.
The lab kept its token in a file,
never printed it whole, and let it
expire with the session; do the same
with yours. Everything the lab did in
later chapters was done holding that
token as admin — which is worth
remembering when chapter 11 makes a
second account that holds almost
nothing, and signs in to prove it.