Answer the question
In order to leave comments, you need to log in
How to use an already running container?
There is docker-compose.yml which runs 2 containers (mysql-5.7 and mysql-8.0)
version: '3'
services:
mysql-5.7:
image: mysql:5.7
volumes:
- ./database/5.7:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
networks:
- mysql-5.7-network
ports:
- 3307:3306
mysql-8.0:
image: mysql:8.0
volumes:
- ./database/8.0:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: secret
networks:
- mysql-8.0-network
ports:
- 3308:3306
networks:
mysql-5.7-network:
name: mysql-5.7-network
driver: bridge
mysql-8.0-network:
name: mysql-8.0-network
driver: bridge
version: '3'
services:
main-project1:
container_name: main-project1
build:
./main-project1
volumes:
- ./main-project1/src:/var/www/html
- ./main-project1/nginx/log:/var/www/log
ports:
- 8001:80
networks:
- main-mysql-5.7
main-project2:
container_name: main-project2
build:
./main-project2
volumes:
- ./main-project2/src:/var/www/html
- ./main-project2/nginx/log:/var/www/log
ports:
- 8002:80
networks:
- main-mysql-5.7
networks:
main-mysql-5.7:
external:
name: mysql-5.7-network
main-mysql-8.0:
external:
name: mysql-8.0-network
FROM ubuntu:20.04
RUN apt-get update && apt-get upgrade -y
RUN apt install software-properties-common -y
RUN add-apt-repository ppa:ondrej/php
RUN apt-get update
RUN apt-get install php7.1-fpm php7.1-mbstring php7.1-curl php7.1-xml php7.1-xdebug php7.1-gd php7.1-mysql php7.1-intl php7.1-xsl php7.1-zip php7.1-memcache php7.1-memcached -y
RUN apt-get install nginx -y
RUN mkdir /var/www/log
COPY ./nginx/config /etc/nginx/sites-available
CMD service php7.1-fpm start && nginx -g "daemon off;"
$dbh = new PDO('mysql:host=mysql-5.7-network;dbname=test', 'roots', 'secret');
Answer the question
In order to leave comments, you need to log in
in the host, not the network name is expected, but the name of the service in it. Try
$dbh = new PDO('mysql:host=mysql-5.7;dbname=test', 'roots', 'secret');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question