List of all databases contained in a docker container

I have multiple databases in multiple containers. It is starting to get confusing to know which containers hold which database.

So my question is very simple, is there a way to get the list of all the databases contained in a docker container?

docker ps -a gives the list of all containers created, but how do I know which container contains a specific database?

There must be a command line that would do just that, give me the list of all my mssql databases contained in each containers, as of now I have to guess which container hold which database in order to access the database.

I'm hosting the docker containers on a mac.

1 answer

  • answered 2019-02-10 13:40 alroc

    Docker has no knowledge of what's running inside it, so you have to ask SQL Server directly.

    select [name] from sys.databases where database_id > 4 will list all user databases on a given instance (or, in your case, container).

    Connect to each instance/container and run that query. If you use Central Management Server, this is much easier as you can execute the same query against a number of servers at once.

    If you don't have a CMS, you can do it by passing the list of instances to the dbatools PowerShell module function Invoke-DbaQuery and get similar results.

    Invoke-DbaQuery -serverinstance InstanceOne,InstanceTwo -query "select [name],@@SERVERNAME from sys.databases where database_id > 4;";

    Another option (again using dbatools) is to use Get-DbaDatabase. This will return more data about each database than the name.

    Get-DbaDatabase -serverinstance InstanceOne,InstanceTwo