Are you sure the host is mssql for DB_HOST=mssql? Or should it be localhost?
Cannot connect to mssql (laradock)
hi, im currently installing a laravel docker from laradock https://laradock.io/
I have already installed nginx, mssql, php-fpm on the docker using docker-compose up mssql nginx php-fpm but when trying to migrate dataabse using php artisan migrate, there is an error:
Illuminate\Database\QueryException
SQLSTATE[HYT00]: [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (SQL: select * from sysobjects where type = 'U' and name = migrations)
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671 667▕ // If an exception occurs when attempting to run a query, we'll format the error 668▕ // message to include the bindings with SQL, which will make this exception a 669▕ // lot more helpful to the developer instead of just the database's errors. 670▕ catch (Exception $e) { ➜ 671▕ throw new QueryException( 672▕ $query, $this->prepareBindings($bindings), $e 673▕ ); 674▕ } 675▕
my database laravel env
DB_CONNECTION=sqlsrv DB_HOST=mssql DB_PORT=1433 DB_DATABASE=laravelsql DB_USERNAME=sa DB_PASSWORD=P@ssw0rds
i can connect to mssql docker using windows management studio and comand line sqlcmd with that username and password, but cannot do php artisan migrate
what is missing here
i have tried both DB_HOST=mssql or DB_HOST=localhost but still have same error
What is the host setting in your Windows Manager Studio?
Run php artisan config:clear before trying again.
Try checking if it is running.
docker ps --all
Now find it in the list. Is it running or restarting?
this is my docker ps list
laradock nginx php-fpm mssql all still running
Ok. Try inspecting it and post the output (replace laradock_mssql with the proper name or the container id, as I cannot see the container name on the image)
docker inspect laradock_mssql
this is my mssql inspect result
C:\Users\andre>docker inspect a5f1
[
{
"Id": "a5f146781bc61e44c897f103a0d46062e796df4f6007bb8399e7a10ed30cda11",
"Created": "2020-09-18T02:59:58.504451374Z",
"Path": "/opt/mssql/bin/nonroot_msg.sh",
"Args": [
"/opt/mssql/bin/sqlservr"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 1825,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-09-21T02:59:01.780369866Z",
"FinishedAt": "2020-09-19T01:28:50.847099887Z"
},
"Image": "sha256:32949a4015fcad0b4dc7418430fc6a492aa506bff1f8ded662d00dc95d0a1164",
"ResolvConfPath": "/var/lib/docker/containers/a5f146781bc61e44c897f103a0d46062e796df4f6007bb8399e7a10ed30cda11/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/a5f146781bc61e44c897f103a0d46062e796df4f6007bb8399e7a10ed30cda11/hostname",
"HostsPath": "/var/lib/docker/containers/a5f146781bc61e44c897f103a0d46062e796df4f6007bb8399e7a10ed30cda11/hosts",
"LogPath": "/var/lib/docker/containers/a5f146781bc61e44c897f103a0d46062e796df4f6007bb8399e7a10ed30cda11/a5f146781bc61e44c897f103a0d46062e796df4f6007bb8399e7a10ed30cda11-json.log",
"Name": "/laradock_mssql_1",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/host_mnt/c/Users/andre/.laradock/data/mssql:/var/opt/mssql/data:rw"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "laradock_backend",
"PortBindings": {
"1433/tcp": [
{
"HostIp": "",
"HostPort": "1433"
}
]
},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": [],
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": true,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": [
"label=disable"
],
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": null,
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/f1cba758def8b04f71017b5c9520c4f294d67e1a52abff4525322380afa32b99-init/diff:/var/lib/docker/overlay2/3f046f8ad1941852b02d38adb90dd92edc6c43553c6808cbaed0efdab5da12df/diff:/var/lib/docker/overlay2/1f9d67e9e61fb7d8c1eb5c1c432e199f7b9ce50cf54cf83007b5763790ece744/diff:/var/lib/docker/overlay2/fec5dc22ed6d97ee64bebc14bf9cd1731f83a9c0bd190f82d50de26e2021188d/diff:/var/lib/docker/overlay2/2fb053ba75fc5c1c752002913f5a4adf560c2ac41be9db785ecabcb9b5f9b026/diff:/var/lib/docker/overlay2/76b5438fb5f2b38feca4b08446217514925a0ec7267a34e3e6656c764e8f265a/diff:/var/lib/docker/overlay2/b82323ca0caacb435881e2753074deb5a3c5fca8e2fc35e2ef81ee60714ea300/diff:/var/lib/docker/overlay2/31df928d714adf63750e47fe2f71bfc01f88ffaebc37a2ccb23a01c5bad15d6c/diff:/var/lib/docker/overlay2/594abcff77b9345dec74ad1416a195ef4010b62377b76f9057d4f4b02fc3f6e0/diff:/var/lib/docker/overlay2/cb29ed25ffefd5b33eb798bfbe23e75fb88c539b1b8a064852f769b7cd5e100e/diff",
"MergedDir": "/var/lib/docker/overlay2/f1cba758def8b04f71017b5c9520c4f294d67e1a52abff4525322380afa32b99/merged",
"UpperDir": "/var/lib/docker/overlay2/f1cba758def8b04f71017b5c9520c4f294d67e1a52abff4525322380afa32b99/diff",
"WorkDir": "/var/lib/docker/overlay2/f1cba758def8b04f71017b5c9520c4f294d67e1a52abff4525322380afa32b99/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/host_mnt/c/Users/andre/.laradock/data/mssql",
"Destination": "/var/opt/mssql/data",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "746b2518cf09",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"1433/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"MSSQL_PID=Express",
"MSSQL_DATABASE=laravelsql",
"MSSQL_SA_PASSWORD=P@ssw0rds",
"ACCEPT_EULA=Y",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"SA_PASSWORD=P@ssw0rds"
],
"Cmd": [
"/opt/mssql/bin/sqlservr"
],
"Image": "laradock_mssql",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/opt/mssql/bin/nonroot_msg.sh"
],
"OnBuild": null,
"Labels": {
"com.docker.compose.config-hash": "b8058e09f6b5d48fe2599ba7cfbc6a3e8c09924081c2fbd04a276c786d7cf43a",
"com.docker.compose.container-number": "1",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "laradock",
"com.docker.compose.project.config_files": "docker-compose.yml",
"com.docker.compose.project.working_dir": "C:\Andre\Docker\laravelDragonRunner\dragonRunnerLaravel\laradock",
"com.docker.compose.service": "mssql",
"com.docker.compose.version": "1.25.4",
"com.microsoft.product": "Microsoft SQL Server",
"com.microsoft.version": "14.0.3356.20",
"vendor": "Microsoft"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "e0beb7f4a1f37d1b8d57c497820c45d62fbee9069b98228799240f55f65f6f63",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"1433/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "1433"
}
]
},
"SandboxKey": "/var/run/docker/netns/e0beb7f4a1f3",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "80ae9def5c57bc16f68d8a32710cd6e3c70bc564d5bea67a02c39ae7cbd22960",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "c5607311adeaa55d297a6a6df2038f233b5e80b40479d5f46f4dc2f5e4cb6a8f",
"EndpointID": "80ae9def5c57bc16f68d8a32710cd6e3c70bc564d5bea67a02c39ae7cbd22960",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
},
"laradock_backend": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"a5f146781bc6"
],
"NetworkID": "17f647ba515eb4d0fe6b20d28c8eabb8700c29b70c74476987cf82c473098cf5",
"EndpointID": "6c74ebcab9e874c31d7760ac9d3980700c4d2a334a6c5c132dbba58b068d3281",
"Gateway": "172.19.0.1",
"IPAddress": "172.19.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:13:00:02",
"DriverOpts": null
}
}
}
}
]
Have you cleared the cache php artisan config:clear? Do a dd(config("database.connections.sqlsrv.host")); before executing the SQL to see the host value being used.
i did clear the cache using php artisan config:clear
i don't understand dd(config("database.connections.sqlsrv.host")); run that on command line?
It does not seem to have gotten a network alias (well it has a randomly generated one on the backend)
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
//and
"laradock_backend": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"a5f146781bc6"
],
Try connecting to it on laradock_mssql_1 from laravel
i did put the connection into the network on all installed laradock (nginx, mssql, php-fpm etc )

I'm using kitematic to check docker network
Yes it is on those networks, but it does not have an alias. Can you perhaps show your docker-compose.yml ?
Here is how it looks on my computer (mysql but it works the same way)
"Networks": {
"backend": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"c48295964166",
"mysql" //This is what I can connect to
],
my docker-compose.yml mssql part
### MSSQL ################################################
mssql:
build:
context: ./mssql
environment:
- MSSQL_PID=Express
- MSSQL_DATABASE=laravelsql
- MSSQL_SA_PASSWORD=P@ssw0rds
- ACCEPT_EULA=Y
volumes:
- ${DATA_PATH_HOST}/mssql:/var/opt/mssql/data
ports:
- "${MSSQL_PORT}:1433"
networks:
- backend
Try giving it an alias in networks
### MSSQL ################################################
mssql:
build:
context: ./mssql
environment:
- MSSQL_PID=Express
- MSSQL_DATABASE=laravelsql
- MSSQL_SA_PASSWORD=P@ssw0rds
- ACCEPT_EULA=Y
volumes:
- ${DATA_PATH_HOST}/mssql:/var/opt/mssql/data
ports:
- "${MSSQL_PORT}:1433"
networks:
backend:
aliases:
- mssql
No restart your containers and try again
docker-compose restart
rebuilt with aliases but still has same error when trying php artisan migrate
root@fc46ab3785a5:/var/www# php artisan migrate
Illuminate\Database\QueryException
SQLSTATE[HYT00]: [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (SQL: select * from sysobjects where type = 'U' and name = migrations)
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
667▕ // If an exception occurs when attempting to run a query, we'll format the error
668▕ // message to include the bindings with SQL, which will make this exception a
669▕ // lot more helpful to the developer instead of just the database's errors.
670▕ catch (Exception $e) {
➜ 671▕ throw new QueryException(
672▕ $query, $this->prepareBindings($bindings), $e
673▕ );
674▕ }
675▕
+37 vendor frames
38 artisan:37
new latest mssql inspect network
"NetworkSettings": {
"Bridge": "",
"SandboxID": "ff7fcb64cd82e43dd74d468228ae6b0bbaee55d682ad51487d900b94a157badb",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"1433/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "1433"
}
]
},
"SandboxKey": "/var/run/docker/netns/ff7fcb64cd82",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"laradock_backend": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"mssql",
"86ffda17f6d8"
],
"NetworkID": "17f647ba515eb4d0fe6b20d28c8eabb8700c29b70c74476987cf82c473098cf5",
"EndpointID": "50781183da7a9ab5844696296a510550e35012f77998e3a177c9667980757554",
"Gateway": "172.19.0.1",
"IPAddress": "172.19.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:13:00:03",
"DriverOpts": null
}
}
And how is your mssql config in laravel now?
config/database.php
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
env file
DB_CONNECTION=sqlsrv
DB_HOST=localhost
DB_PORT=1433
DB_DATABASE=laravelsql
DB_USERNAME=sa
DB_PASSWORD=P@ssw0rds
laradock env file
### MSSQL #################################################
MSSQL_DATABASE=laravelsql
MSSQL_PASSWORD=P@ssw0rds
MSSQL_PORT=1433
mssql dockerfile:
FROM mcr.microsoft.com/mssql/server:2017-latest-ubuntu
ENV MSSQL_PID=Express
ENV MSSQL_DATABASE=laravelsql
ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=P@ssw0rds
EXPOSE 1433
Change this
DB_CONNECTION=sqlsrv
DB_HOST=mssql //this was changed
DB_PORT=1433
DB_DATABASE=laravelsql
DB_USERNAME=sa
DB_PASSWORD=P@ssw0rds
finally
root@fc46ab3785a5:/var/www# php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table (11.55ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table (3.60ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated: 2019_08_19_000000_create_failed_jobs_table (4.95ms)
root@fc46ab3785a5:/var/www#
thanks man!!
so the problem there was no alliases name on the network
Apparantly no. Normally docker-compose will create them from the key (name) of the service, but that was not done here it would seem. Not sure why. But you can always make it manually :)
Please or to participate in this conversation.