Skip to content

Instantly share code, notes, and snippets.

@deviantony
Created March 8, 2018 01:24
Show Gist options
  • Save deviantony/62c009b41bde5e078b1a7de9f11f5e55 to your computer and use it in GitHub Desktop.
Save deviantony/62c009b41bde5e078b1a7de9f11f5e55 to your computer and use it in GitHub Desktop.
Portainer admin password in a docker-compose environment

Portainer compose deployment with admin password preset

This file aims to explain how to deploy Portainer inside a compose file with the admin password already set.

Generate the admin password

For this example, we'll use the password superpassword.

Use the following command to generate a hash for the password:

docker run --rm httpd:2.4-alpine htpasswd -nbB admin 'superpassword' | cut -d ":" -f 2

The output of that command is the hashed password, it should be something similar to $2y$05$w5wsvlEDXxPjh2GGfkoe9.At0zj8r7DeafAkXXeubs0JnmxLjyw/a.

Define the password in the compose file

If you try to use the hashed password in this form directly in your Compose file, the following error will be raised:

ERROR: Invalid interpolation format for "command" option in service "portainer": "--admin-password '$2y$05$ZBq/6oanDzs3iwkhQCxF2uKoJsGXA0SI4jdu1PkFrnsKfpCH5Ae4G'"

You need to escape each $ character inside the hashed password with another $:

$$2y$$05$$ZBq/6oanDzs3iwkhQCxF2uKoJsGXA0SI4jdu1PkFrnsKfpCH5Ae4G

Example of valid Compose file:

version: '2'

services:
  portainer:
    image: portainer/portainer:latest
    ports:
      - "9000:9000"
    command: --admin-password '$$2y$$05$$ZBq/6oanDzs3iwkhQCxF2uKoJsGXA0SI4jdu1PkFrnsKfpCH5Ae4G'
    networks:
      - local
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - portainer-data:/data

networks:
  local:
    driver: bridge

volumes:
  portainer-data:

Output of docker-compose up:

docker-compose up                                                                                           !10023
Creating network "porcomp_local" with driver "bridge"
Creating volume "porcomp_portainer-data" with default driver
Creating porcomp_portainer_1 ... 
Creating porcomp_portainer_1 ... done
Attaching to porcomp_portainer_1
portainer_1  | 2018/03/08 01:18:37 Creating admin user with password hash $2y$05$ZBq/6oanDzs3iwkhQCxF2uKoJsGXA0SI4jdu1PkFrnsKfpCH5Ae4G
portainer_1  | 2018/03/08 01:18:37 Starting Portainer 1.16.3 on :9000

Now you can login to the Portainer instance using the credentials admin / superpassword.

@chris-cadev
Copy link

chris-cadev commented Mar 15, 2022

Did I do something wrong?

I notice my error, in the encrypted for each $ we need to escape them as $$

@LostOnTheLine
Copy link

Thanks man, but how to change the user admin to something else?

I am having the same issue.

@alfchao
Copy link

alfchao commented May 26, 2023

Thanks man, but how to change the user admin to something else?谢谢伙计,但是如何将用户 admin 更改为其他内容?

I am having the same issue.我遇到了同样的问题。

y, me 2.

@kaltokri
Copy link

I use this and it works fine:

docker run --rm httpd:2.4-alpine htpasswd -nbB admin YourPassword | cut -d ":" -f 2 | sed 's/\$/\$\$/g'

Thank you for sharing the information!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment