简体   繁体   中英

how can I configure the default admin user:password with docker if i am launching docker-compose up of a docker(portainer in my case)?

I am trying to create a docker-compose which sets up a huge environment of dockers with portainer as a manager.

The problem is that the first time the user use "docker-compose up" and the portainer start running, he has to navigate to portainer web interface (localhost:9000) and set-up the admin user and password.

How can I automate this step and create a portainer with a default user that I define so when the user navigate to portainer on the first time, the admin user is already created.

Here is my docker-compose.yml

version: '3.3'

services:

  portainer:
    image: portainer/portainer
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./portainer/portainer_data:/data
    ports:
      - "9000:9000"

Portainer allows you to specify an encrypted password from the command line for the admin account. You need to generate the hash value for password.

For example, this is the hash value of password - $$2y$$05$$arC5e4UbRPxfR68jaFnAAe1aL7C1U03pqfyQh49/9lB9lqFxLfBqS

In your docker-compose file make following modification

version: '3.3'
 services:
   portainer:
    image: portainer/portainer
    volumes:
     - /var/run/docker.sock:/var/run/docker.sock
     - ./portainer/portainer_data:/data
    command: --admin-password "$$2y$$05$$arC5e4UbRPxfR68jaFnAAe1aL7C1U03pqfyQh49/9lB9lqFxLfBqS"
    ports:
     - "9000:9000"

--admin-password This flag is used to specify an encrypted password in Portainer.

More information can be found in documentation - Portainer

Hope this will help you.

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

$2y$05$ZBq/6oanDzs3iwkhQCxF2uKoJsGXA0SI4jdu1PkFrnsKfpCH5Ae4G

To

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

You can set admin password ONLY for the first run of the container. Use this repository :

Following Mohsen's remark, you must run docker-compose down , each time you need to restart and initialise a new admin password, with the option -v if you want also to delete the volumes.

https://github.com/portainer/portainer/issues/1506#issuecomment-352273682

Using version: '3.3' in the docker-compose.yml file is mandatory in this later case.

I set the admin password directly on Dockerfile!

  1. Create a script to generate your password:
$ cat <<EOF > portainer-pass.sh
#!/usr/bin/env bash
    
if [ -z "\$1" ]; then
    echo -e "\\nPlease call '\$0 <password>' to run this command!\\n"
    exit 1
fi
    
htpasswd -nb -B admin \$1 | cut -d ":" -f 2
EOF
  1. Give your permission:
chmod u+x portainer-pass.sh
  1. generate your password:
./portainer-pass.sh c7e694055489cb2051195a2be1740992

Output: $2y$05$bGljp9ThZkfNaZuKvDUB3uKpXecI5SDZ6s6Xga8azv4JQUDXmHV82

  1. Put in "CMD" of the Dockerfile:
# Set fixed portainer image
FROM portainer/portainer-ce:latest

# Set default admin password at startup
CMD ["--admin-password", "$2y$05$bGljp9ThZkfNaZuKvDUB3uKpXecI5SDZ6s6Xga8azv4JQUDXmHV82"]

# Default portainer web port
EXPOSE 9443

Here you NOT need to be replace $ to $$ !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM