I've prepared a docker-compose file to deploy container with database:
services:
tmp-db:
image: microsoft/mssql-server-linux:latest
environment:
ACCEPT_EULA: Y
SA_PASSWORD: yourStrong(!)Password
ports:
- 1433:1433
It is okay. But now I need to create a database and build its structure. I need to execute some sql commands.
To check if i am able to do this I added command to the service:
services:
tmp-db:
image: microsoft/mssql-server-linux:latest
environment:
ACCEPT_EULA: Y
SA_PASSWORD: yourStrong(!)Password
command: /opt/mssql-tools/bin/sqlcmd -U sa -P yourStrong(!)Password -Q "SELECT [name] FROM sys.databases"
ports:
- 1433:1433
However I got the following errors:
tmp-db_1 | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired.
tmp-db_1 | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: Error code 0x2749.
tmp-db_1 | Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
I feel the command is executed before Sql Server instance is started. How can I fix it? How can I execute some sql after Sql Server is started?
The issue is that only one command is executed in a container. When you specify the command in docker-compose.yml it overrides the default command, which was supposed to start the container. So you have two options
Run the command manually
services:
tmp-db:
image: microsoft/mssql-server-linux:latest
environment:
ACCEPT_EULA: Y
SA_PASSWORD: yourStrong(!)Password
ports:
- 1433:1433
Then you can execute
docker-compose exec tmp-db /opt/mssql-tools/bin/sqlcmd -U sa -P yourStrong(!)Password -Q "SELECT [name] FROM sys.databases"
Have two services - one for server & one for data loading
services:
load-db:
image: microsoft/mssql-server-linux:latest
command: sh -c 'sleep 10 && /opt/mssql-tools/bin/sqlcmd -U sa -P yourStrong(!)Password -Q "SELECT [name] FROM sys.databases"'
network_mode: service:tmp-db
tmp-db:
image: microsoft/mssql-server-linux:latest
environment:
ACCEPT_EULA: Y
SA_PASSWORD: yourStrong(!)Password
ports:
- 1433:1433
In this approach we launch another container with the command to load our data, we run it on the network of our DB server container. This is done just to avoid the host name of the DB, if you prefer you can pass the host name also as tmp-db and remove the network_mode: service:tmp-db.
Related
I am working with a MacBook M1 that runs a SQL Server database using microsoft-azure-sql-edge image. On the same laptop is running a Win 11 VM using Parallels where SQL Server Management Studio is installed.
The issue is that I can't connect to the database from the Windows VM. I tried to give a hostname to the database container and connect it to the host network using this docker compose config:
version: "3.9"
services:
mssql:
image: mcr.microsoft.com/azure-sql-edge:latest
command: /opt/mssql/bin/sqlservr
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: ${SAPassword}
stdin_open: true
container_name: sqlserver
hostname: sqlserver
network_mode: host
ports:
- 1433:1433
I tried to set sqlserver as server name and enter the login for sa but I get an error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server
I know this has been covered multiple times, but I still cannot execute an SQL script on docker deployment.
docker-compose
version: '3.9'
services:
mssql:
build: ./docker/mssql
container_name: db
ports:
- 1433:1433
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=Welcome#toSQL2022
- MSSQL_PID=Developer
volumes:
- ./docker/mssql/db-data:/var/opt/mssql/data
- ./docker/mssql/scripts:/var/opt/mssql/scripts
networks:
- supernet
networks:
supernet: {}
Dockerfile
FROM mcr.microsoft.com/mssql/server:2019-CU15-ubuntu-20.04
COPY ./create-from-mdf.sql .
EXPOSE 1433
#tried this: CMD ["/opt/mssql-tools/bin/sqlcmd", "-i", "create-from-mdf.sql", "-S", "localhost", "-U", "SA", "-P" "Welcome#toSQL2022"]
#tried this: CMD sleep 30 && /opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U sa -P Welcome#toSQL2022 -i create-from-mdf.sql#
CMD /opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U sa -P Welcome#toSQL2022 -i create-from-mdf.sql
I get this error:
db | Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
db | Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
db | Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
db exited with code 1
However, if I connect to container via CLI the same command gets executed succefully:
/opt/mssql-tools/bin/sqlcmd -S localhost,1433 -U sa -P Welcome#toSQL2022 -i create-from-mdf.sql
This is my docker-compose.yml file:
version: "3.9"
services:
api:
# configuration of API here
db:
image: mcr.microsoft.com/mssql/server
restart: always
container_name: Database
volumes:
- ./Data:/var/opt/mssql
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=lksU2o412f7tBj58t07B
- MSSQL_PID=Express
ports:
- 1433:1433
command: >
sh -c
"
tail -f /dev/null
"
As you can see, I'm using the official docker image for MS SQL Server, mcr.microsoft.com/mssql/server.
My laptop is Ubuntu 20.04 LTS.
I run docker-compose up -d and then docker exec -it db sh to get interactive shell.
Then I write this command:
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'lksU2o412f7tBj58t07B'
And I get this response:
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
This is the official image and I have not changed anything.
Also in my api container, I can ping db or Database. But I can't telnet port 1433.
Also from my host machine I can telnet localhost 1433, but I get this response:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
This is a terrible experience in working with containers from Microsoft. Aren't containers supposed to work out of the box? Isn't container technology there only to solve these stupid problems?
What should I do?
Problem
$ go run cmd/syndicate/main.go
2021/01/25 16:37:25 error connecting to database: dial tcp: lookup db: no such host
Unable to connect to database when attempting to run:
$ go run cmd/syndicate/main.go
2021/01/25 16:37:25 error connecting to database: dial tcp: lookup db: no such host
&
$ migrate -source file://migrations -database postgres://postgres:secret#db:5432/syndicate?sslmode=disable up
error: dial tcp: lookup db on [2001:558:feed::1]:53: no such host
What do these two commands have in common?... Database URL. I am nearly certain my database URL is incorrect.
I have verified my postgres container is running:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e578bf646c7 adminer "entrypoint.sh docke…" 3 days ago Up 3 days 0.0.0.0:8080->8080/tcp syndicate_adminer_1
729fc179aa6f postgres "docker-entrypoint.s…" 3 days ago Up 3 days 5432/tcp syndicate_db_1
Here's where I might be overlooking something...
$ docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------
syndicate_adminer_1 entrypoint.sh docker-php-e ... Up 0.0.0.0:8080->8080/tcp
syndicate_db_1 docker-entrypoint.sh postgres Up 5432/tcp
5432/tcp???
I see that my adminer container is clearly mapped to my local port (0.0.0.0:8080->8080/tcp), however my postgres container is only showing 5432/tcp (and not 0.0.0.0:5432->5432/tcp)
I am new to docker.. Can anyone explain why my postgres port isn't associated with my local port?
Am I on the right track?
Here's my docker-compose.yml:
version: "3.8"
services:
db:
image: postgres
environment:
POSTGRES_DB: $POSTGRES_DB
POSTGRES_USER: $POSTGRES_USER
POSTGRES_PASSWORD: $POSTGRES_PASSWORD
migrate:
image: migrate/migrate
volumes:
- ./migrations:/migrations
depends_on:
- db
command: -source=file://migrations -database postgres://$POSTGRES_USER:$POSTGRES_PASSWORD#db:5432/$POSTGRES_DB?sslmode=disable up
adminer:
image: adminer
restart: always
ports:
- "8080:8080"
environment:
ADMINER_DEFAULT_SERVER: db
depends_on:
- db
PS. I tried adding port: "5432:5432" variable for db servicd
Browse my repository at this time in history
Thank you!
Connor
add to db service
ports:
- "5432:5432"
migrate -path D:/works/go/go-fiber-api-server/backend/platform/migrations -database "postgres://postgres:password#cgapp-postgres:5432/postgres?sslmode=disable" up
error: dial tcp: lookup cgapp-postgres: no such host
Then I have fixed this issue to change db-host name(cgapp-postgres) into "IP ADDRESS" or host.docker.internal.
migrate -path D:/works/go/go-fiber-api-server/backend/platform/migrations -database "postgres://postgres:password#100.100.100.100:5432/postgres?sslmode=disable" up
1/u create_init_tables (20.0987ms)
migrate -path D:/works/go/go-fiber-api-server/backend/platform/migrations -database "postgres://postgres:password#host.docker.internal:5432/postgres?sslmode=disable" up
1/u create_init_tables (20.0987ms)
"host.docker.internal" works.
I have simple docker-compose.yml which contains two services only, my-api and sql-server.
version: '3.0'
services:
sql-server:
image: mcr.microsoft.com/mssql/server:2019-latest
hostname: sql-server
container_name: sql-server
ports:
- "1433:1433"
environment:
- ACCEPT_EULA=Y
- SA_PASSWORD=MyPassword01*
- MSSQL_PID=Express
my-api:
ports:
- "8080:5000"
depends_on:
- sql-server
... ommited for clarity
When I docker-compose up --build the containers are ready (I can verify with docker ps)
a7a47b89a17a mcr.microsoft.com/mssql/server:2019-latest "/opt/mssql/bin/perm…" 12 minutes ago Up 11 minutes 0.0.0.0:1433->1433/tcp sql-server
but I cannot access my SQL Server using SSMS.
SSMS login window:
Server Name: localhost,1433
Authentication: SQL Server Authentication
Username: sa
Password: MyPassword01*
Error:
Cannot connect to localhost,1433.
Login failed for user 'sa'. (.Net SqlClient Data Provider)
PS: I also tried with
Server Name: sql-server,1433
but still cannot access
Execute the below code which will display the public ipaddress.
Instead of localhost use this ipaddress
docker inspect -f "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" mssqltrek-con1.
I followed the below link to achieve the same.
https://www.sqlshack.com/sql-server-with-a-docker-container-on-windows-server-2016/
I guess you have to debug further. The first thing I guess you can do is to open the bash inside the container and try to connect to your SQL database from the container.
docker exec -it sql-server "bash"
Once inside the container bash, then connect with sqlcmd,
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P "<YourNewStrong#Passw0rd>"
If you fail to connect inside the container, then I have to assume your SA password is somehow different when you set up the SQL server. But if this is not an issue, that means, you can connect to SQL server inside the container, then you can be rest assured it is something wrong with SSMS on port 1433 in your computer. Make sure the server name is correct.