2022-05-17

[Laravel Sail] How to handle an error "Cannot start service … address already in use" on `sail up` command execution

Description

When a command ./vendor/bin/sail up is executed, following error occurred.

  • MySQL image doesn't start up.

ERROR: for mysql Cannot start service mysql: driver failed programming external connectivity on endpoint example-app_mysql_1 (省略): Error starting userland proxy: listen tcp4 0.0.0.0:3306: bind: address already in use

  • Laravel image doesn't start up.

ERROR: for example-app_laravel.test_1 Cannot start service laravel.test: driver failed programming external connectivity on endpoint example-app_laravel.test_1 (省略): Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use

Cause and Solution

As shown in the error message, cause is that port 80 and port 3306 are already in use. In my development environment, Nginx and MySQL are locally hosted.

To solve the problem, there are 2 options.

  1. Release port 80 and port 3306.
  2. Assign an unused port.

In this article, we adopt the 2nd option. You just need to add following environment variables:

  • APP_PORT=8080
  • FORWARD_DB_PORT=33066

.env

APP_NAME=Laravel
APP_ENV=local
APP_KEY= // 省略
APP_DEBUG=true
APP_URL=http://example-app.test
APP_PORT=8080

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
FORWARD_DB_PORT=33066
DB_DATABASE=example_app
DB_USERNAME=sail
DB_PASSWORD=password

// omitted below

Then, restart Docker containers.

bash

./vendor/bin/sail down
./vendor/bin/sail up

Now, Laravel application and MySQL is listened on 8080 and 33066 respectively.

bash

docker ps
CONTAINER ID   IMAGE                         COMMAND                  CREATED         STATUS                   PORTS                                                                                  NAMES
4fabed6f1f90   sail-8.0/app                  "start-container"        2 minutes ago   Up 2 minutes             8000/tcp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp                                        example-app_laravel.test_1
2f2a8bca4fc5   mailhog/mailhog:latest        "MailHog"                2 minutes ago   Up 2 minutes             0.0.0.0:1025->1025/tcp, :::1025->1025/tcp, 0.0.0.0:8025->8025/tcp, :::8025->8025/tcp   example-app_mailhog_1
ff197fdd0936   mysql:8.0                     "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes (healthy)   33060/tcp, 0.0.0.0:33066->3306/tcp, :::33066->3306/tcp                                 example-app_mysql_1
cc2283b5f252   redis:alpine                  "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes (healthy)   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp                                              example-app_redis_1
7ded5834509c   getmeili/meilisearch:latest   "tini -- /bin/sh -c …"   2 minutes ago   Up 2 minutes (healthy)   0.0.0.0:7700->7700/tcp, :::7700->7700/tcp                                              example-app_meilisearch_1
19105ce35c45   selenium/standalone-chrome    "/opt/bin/entry_poin…"   2 minutes ago   Up 2 minutes             4444/tcp                                                                               example-app_selenium_1