Error response from daemon: ports are not available: exposing port tcp 0.0.0.0:3306 -> 0.0.0.0:0: listen tcp 0.0.0.0:3306: bind: address already in use

I’m sure we’ve encountered this error plenty of times, the one that looks like this.

ERROR: for postgres Cannot start service postgres: driver failed programming external connectivity on endpoint test_postgres_1 Bind for 0.0.0.0:5432 failed: port is already allocated

In this example, we have another container or an application listening on the port 5432 on our machine. This prevents our service from allocating that port.

Now, how can we fix this?

I was using docker-compose to start and stop my applications, so naturally, I used docker-compose ps to list all running containers and docker-compose down to stop those containers.

However, docker-compose commands only execute on containers related to images in our docker-compose.yml files. We also need to check containers started without docker-compose.

This is the process I would use to stop the correct containers.

First, I would stop all containers started by docker-compose [or just that one container].

Then, I would list all running containers.

CONTAINER ID IMAGE ... PORTS NAMES 45eec0e12d63 postgres ... 0.0.0.0:5432->5432/tcp test_postgres_1

Let’s grab the CONTAINER ID and stop/remove the container.

docker stop 45eec0e12d63 docker rm 45eec0e12d63

  • Article
  • 03/31/2022
  • 5 minutes to read

Applies to:

SQL Server [all supported versions] - Linux

This article talks about common errors seen when deploying and using SQL Server Docker containers, and provide troubleshooting techniques to help resolve the issue.

Docker command errors

If you get errors for any docker commands, make sure that the docker service is running, and try to run with elevated permissions.

For example, on Linux, you might get the following error when running docker commands:

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

If you get this error on Linux, try running the same commands prefaced with sudo. If that fails, verify the docker service is running, and start it if necessary.

sudo systemctl status docker sudo systemctl start docker

On Windows, verify that you're launching PowerShell or your command-prompt as an Administrator.

SQL Server container startup errors

If the SQL Server container fails to run, try the following tests:

  • If you get an error such as failed to create endpoint CONTAINER_NAME on network bridge. Error starting proxy: listen tcp 0.0.0.0:1433 bind: address already in use., you're attempting to map the container port 1433 to a port that is already in use. This can happen if you're running SQL Server locally on the host machine. It can also happen if you start two SQL Server containers and try to map them both to the same host port. If this happens, use the -p parameter to map the container port 1433 to a different host port. For example:

    docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=' -p 1400:1433 -d mcr.microsoft.com/mssql/server:2017-latest`.

    docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -p 1400:1433 -d mcr.microsoft.com/mssql/server:2017-latest`.

    docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -p 1400:1433 -d mcr.microsoft.com/mssql/server:2017-latest`.

    docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=' -p 1400:1433 -d mcr.microsoft.com/mssql/server:2019-latest`.

    docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -p 1400:1433 -d mcr.microsoft.com/mssql/server:2019-latest`.

    docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -p 1400:1433 -d mcr.microsoft.com/mssql/server:2019-latest`.

  • If you get an error such as Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get //%2Fvar%2Frun%2Fdocker.sock/v1.30tdout=1&tail=all: dial unix /var/run/docker.sock: connect: permission denied when trying to start a container, then add your user to the docker group in Ubuntu. Then logout and login again as this change will affect new sessions.

    usermod -aG docker $USER
  • Check to see if there are any error messages from container.

    docker logs e69e056c702d
  • Make sure that you meet the minimum memory and disk requirements specified in the prerequisites section of the quickstart article.

  • If you're using any container management software, make sure it supports container processes running as root. The sqlservr process in the container runs as root.

  • If your SQL Server Docker container exits immediately after starting, check your docker logs. If you're using PowerShell on Windows with the docker run command, use double quotes instead of single quotes. With PowerShell Core, use single quotes.

  • Review the SQL Server setup and error logs.

Enable dump captures

If the SQL Server process is failing inside the container, you should create a new container with SYS_PTRACE enabled. This adds the Linux capability to trace a process, which is necessary for creating a dump file on an exception. The dump file can be used by support to help troubleshoot the problem. The following docker run command enables this capability.

docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=' -e 'MSSQL_PID=Developer' --cap-add SYS_PTRACE -p 1401:1433 -d mcr.microsoft.com/mssql/server:2017-latest

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -e "MSSQL_PID=Developer" --cap-add SYS_PTRACE -p 1401:1433 -d mcr.microsoft.com/mssql/server:2017-latest

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -e "MSSQL_PID=Developer" --cap-add SYS_PTRACE -p 1401:1433 -d mcr.microsoft.com/mssql/server:2017-latest

docker run -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=' -e 'MSSQL_PID=Developer' --cap-add SYS_PTRACE -p 1401:1433 -d mcr.microsoft.com/mssql/server:2019-latest

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -e "MSSQL_PID=Developer" --cap-add SYS_PTRACE -p 1401:1433 -d mcr.microsoft.com/mssql/server:2019-latest

docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=" -e "MSSQL_PID=Developer" --cap-add SYS_PTRACE -p 1401:1433 -d mcr.microsoft.com/mssql/server:2019-latest

SQL Server connection failures

If you can't connect to the SQL Server instance running in your container, try the following tests:

  • Make sure that your SQL Server container is running by looking at the STATUS column of the docker ps -a output. If not, use docker start to start it.

  • If you mapped to a non-default host port [not 1433], make sure you're specifying the port in your connection string. You can see your port mapping in the PORTS column of the docker ps -a output. For example, the following command connects sqlcmd to a container listening on port 1401:

    sqlcmd -S 10.3.2.4,1401 -U SA -P ''

    sqlcmd -S 10.3.2.4,1401 -U SA -P ""

    sqlcmd -S 10.3.2.4,1401 -U SA -P ""

  • If you used docker run with an existing mapped data volume or data volume container, SQL Server ignores the value of SA_PASSWORD. Instead, the pre-configured SA user password is used from the SQL Server data in the data volume or data volume container. Verify that you're using the SA password associated with the data you're attaching to.

  • Review the SQL Server setup and error logs.

SQL Server Availability Groups

If you're using Docker with SQL Server Availability Groups, there are two additional requirements.

  • Map the port that is used for replica communication [default 5022]. For example, specify -p 5022:5022 as part of your docker run command.

  • Explicitly set the container host name with the -h YOURHOSTNAME parameter of the docker run command. This host name is used when you configure your Availability Group. If you don't specify it with -h, it defaults to the container ID.

SQL Server setup and error logs

You can look at the SQL Server setup and error logs in /var/opt/mssql/log. If the container isn't running, first start the container. Then use an interactive command-prompt to inspect the logs. You can get the container ID by running the command docker ps.

docker start docker exec -it "bash"

From the bash session inside your container, run the following commands:

cd /var/opt/mssql/log cat setup*.log cat errorlog

Tip

If you mounted a host directory to /var/opt/mssql when you created your container, you can instead look in the log subdirectory on the mapped path on the host.

Execute commands in a container

If you have a running container, you can execute commands within the container from a host terminal.

To get the container ID run:

docker ps -a

To start a bash terminal in the container run:

docker exec -it /bin/bash

Now you can run commands as though you're running them at the terminal inside the container. When finished, type exit. This exits in the interactive command session, but your container continues to run.

Next steps

  • Get started with SQL Server 2017 container images on Docker by going through the quickstart.

  • Get started with SQL Server 2019 container images on Docker by going through the quickstart.

Video liên quan

Chủ Đề