I have two containers for a laravel project, one with mysql and the other with apache, when starting mysql it gives me an error.
The first thing I do is stop all the containers with the command " docker-compose down " and this is what I get on the screen
Stopping backend-forms_app_1 ... done
Removing backend-forms_app_1 ... done
Removing database ... done
Removing network backend-bringforms_default
And if I do a docker-compose
Creating network "backend-forms_default" with the default driver
Creating database ...
Creating database ... error
WARNING: Host is already in use by another container
Creating backend-forms_app_1 ... done
ERROR: for db Cannot start service db: driver failed programming external connectivity on endpoint database (ee9216527529596af152a5e210b6f8a90e46badd3e29bb8f4c9e024b0da1cc2e): Bind for 0.0.0.0:3307 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
docker compose file
version: '3.7'
services:
app:
build:
context: .
dockerfile: .docker/Dockerfile
image: laravelapp
command: php artisan serve --host 0.0.0.0 --port=8080
ports:
- 8080:8080
environment:
- SQL_HOST=db
volumes:
- ./:/var/www/html
db:
image: mysql:5.7
container_name: database
restart: always
ports:
- 3307:3306
environment:
- MYSQL_DATABASE=laraapp_db
- MYSQL_ALLOW_EMPTY_PASSWORD=1
- MYSQL_ROOT_PASSWORD=
- MYSQL_HOST=
volumes:
- mysql_data:/var/lib/mysql
volumes:
mysql_data:
The problem you have is that the image is being executed first
app
and then the database image; the solution for this is simple you must add to the construction of the imageapp
that depends on the execution of the database something likedepends:db
the complete example below: