4

When spinning up a new SQL Server 2022 (or 2019) Docker container using -h or --hostname

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=dev1234#" -p 1633:1433 --name sql22new --hostname sql22new -d mcr.microsoft.com/mssql/server:2022-latest

the value for @@SERVERNAME is (as expected) sql22new.

When running from docker compose, then the value for @@SERVERNAME is buildkitsandbox.

Did anyone else came across this and solved it and would like to help?

I need to have the value of @@SERVERNAME show the hostname set correctly by compose.

If we look inside a container, in /etc/hosts the values are ok and also in /etc/hostname.

Details

Dockerfile - image is called sqlha

FROM mcr.microsoft.com/mssql/server:2022-latest

ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=dev1234#
ENV MSSQL_PID=Developer
ENV MSSQL_TCP_PORT=1433
ENV MSSQL_AGENT_ENABLED=True
ENV MSSQL_ENABLE_HADR=1

WORKDIR /src

RUN mkdir /var/opt/mssql/backup/
RUN mkdir /tmp/certificates/
RUN mkdir /tmp/scripts/

COPY ./cert/* /tmp/certificates/
COPY *.sql /tmp/scripts/

RUN (/opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" &&  /opt/mssql-tools/bin/sqlcmd -S127.0.0.1 -Usa -Pdev1234# -i /tmp/scripts/setup.sql

docker-compose.yml

version: '3.9'

networks:
    db-server-network:
      ipam:
        driver: default
        config:
          - subnet: "172.16.238.0/24"

services:
  db1:
    container_name: sqlNode1
    image: sqlha
    hostname: sqlNode1
    ports:
    - "1501:1433"
    extra_hosts:
      - "sqlNode2:172.16.238.12"
      - "sqlNode3:172.16.238.13"
    networks:
      db-server-network:
        ipv4_address: 172.16.238.11
        aliases: 
        - sqlNode1

  db2:
    container_name: sqlNode2
    image: sqlha
    hostname: sqlNode2
    ports:
    - "1502:1433"
    extra_hosts:
      - "sqlNode1:172.16.238.11"
      - "sqlNode3:172.16.238.13"
    networks:
      db-server-network:
        ipv4_address: 172.16.238.12
        aliases: 
        - sqlNode2

  db3:
    container_name: sqlNode3
    image: sqlha
    hostname: sqlNode3
    ports:
    - "1503:1433"
    extra_hosts:
      - "sqlNode1:172.16.238.11"
      - "sqlNode2:172.16.238.12"
    networks:
      db-server-network:
        ipv4_address: 172.16.238.13
        aliases: 
        - sqlNode3

When mounting directly:

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=dev1234#" -p 1633:1433 --name sql22new --hostname sql22new -d mcr.microsoft.com/mssql/server:2022-latest

When mounting directly

When mounting with compose:

When mounting with compose

sqlNode1 container inspection:

docker exec -u 0 -it sqlNode1 bash

then

apt-get update && apt-get install nano -y
nano /etc/hosts

hosts file contents

nano /etc/hostname

/etc/hostname

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • I am not sure which service you would assighn with mentioned server name. You have 3 database nodes in your compose file. However in `docker run` example you start only a single node. – Alexey R. Jan 31 '23 at 16:51
  • Seems to be something you're doing in your `Dockerfile` or related scripts. If you launch `image: mcr.microsoft.com/mssql/server:2022-latest` directly from your `docker-compose.yml` file (along with the `ACCEPT_EULA` and `SA_PASSWORD` environment variables) then the containers' `HOSTNAME` environment variables are correct and `SELECT @@SERVERNAME` returns the correct name. – AlwaysLearning Feb 01 '23 at 02:26

0 Answers0