1

I am fooling around with some docker containers and I wonder how could I fetch password generated on container creation from logs.

So I run container with

$> docker container run --name mysql -p 3306:3306 -e MYSQL_RANDOM_ROOT_PASSWORD=yes -d mysql

This command starts a new mysql container with randomly generated password that I can get from logs. To be exact this command gives among others pass:

$> docker container logs mysql
...
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
GENERATED ROOT PASSWORD: Poow3eet2aat6aePhae6leipeecagife

2019-04-07T08:00:18.945238Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.15)  MySQL Community Server - GPL.

That's just a background. Naturally, the important part is the line with GENERATED ROOT PASSWORD and you could assume I got this text file from any other source in fact. I'd like to store it in some variable with something like

export ROOT_PASS=$(expression)

To do so I need to come up with expression that would return only the random generated string. Of course, alternatively I could just assign matching substring of regexp pattern to variable.

I thought of

docker container logs mysql | grep -e "PASSWORD: .*"

but this results with entire line and highlights PASSWORD: Poow3eet2aat6aePhae6leipeecagife in fact. I also thought of something with sed:

docker container logs mysql | sed -e 's/PASSWORD: .*/$1/g'

... but this stripped the interesting part and returned everything else in fact.

Any suggestions?

Inian
  • 80,270
  • 14
  • 142
  • 161
Tomasz Kapłoński
  • 1,320
  • 4
  • 24
  • 49

1 Answers1

2

Append with GNU grep:

| grep -Po 'GENERATED ROOT PASSWORD: \K.*'

or GNU sed:

| sed -n 's/^GENERATED ROOT PASSWORD: //p'

Output:

Poow3eet2aat6aePhae6leipeecagife

See: What does '\K' mean in this regex?

Cyrus
  • 84,225
  • 14
  • 89
  • 153