I want to create a simply LAMP with docker. The code is
Dockerfile
FROM php:8.0.0-apache
ARG DEBIAN_FRONTEND=noninteractive
RUN docker-php-ext-install mysqli
RUN apt-get update \
&& apt-get install -y libzip-dev \
&& apt-get install -y zlib1g-dev \
&& rm -rf /var/lib/apt/lists/* \
&& docker-php-ext-install zip
RUN a2enmod rewrite
Docker Compose
version: "3.8"
services:
www:
build: .
container_name: 'www'
hostname: 'laravelvue.com'
restart: 'always'
ports:
- "80:80"
networks:
- default
volumes:
- ./www:/var/www/html
database:
image: mariadb:10.7.1
container_name: 'mariadb'
command: --default-authentication-plugin=mysql_native_password
restart: 'always'
ports:
- "3306:3306"
volumes:
- persistent:/var/lib/mysql
environment:
MARIADB_DATABASE: laravelvue
MARIADB_USER: root
MARIADB_PASSWORD: test
MARIADB_ROOT_PASSWORD: test
networks:
- default
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: 'phpmyadmin'
links:
- database
environment:
MYSQL_USER: root
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
ports:
- 8000:80
volumes:
persistent:
When i launch docker compose up -d
works, but when i try to enter in database (localhost:8000) with username root and password test returns
mysqli::real_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
I´m new with docker so i don´t know exactly what´s is wrong ¿Anybody see what´s wrong?
CodePudding user response:
Docker networking works with the name of your service as hostname. So in your database
service you can connect with your phpmyadmin via http://phpmyadmin:8000
. The same is for the other services. In your phpadmin service you connect with your database via http://database:3306
.
And remove the networks: default
and links: database
in your docker-compose.yml
it is not needed, as you use version 3.8
in docker-compose.yml
. And make sure your phpadmin
service depends_on
your database
service.
Correct docker-compose.yml
:
version: "3.8"
services:
www:
build: .
container_name: 'www'
restart: 'always'
ports:
- "80:80"
volumes:
- ./www:/var/www/html
database:
image: mariadb:10.7.1
container_name: 'mariadb'
command: --default-authentication-plugin=mysql_native_password
restart: 'always'
ports:
- "3306:3306"
volumes:
- persistent:/var/lib/mysql
environment:
MARIADB_DATABASE: laravelvue
MARIADB_USER: root
MARIADB_PASSWORD: test
MARIADB_ROOT_PASSWORD: test
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
depends_on:
- database
environment:
MYSQL_USER: root
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: test
ports:
- 8000:80