L
L
Leonid Gorshkov2020-01-03 20:30:04
Docker
Leonid Gorshkov, 2020-01-03 20:30:04

How to use an already running container?

There is docker-compose.yml which runs 2 containers (mysql-5.7 and mysql-8.0)

docker-compose.yml (mysql)

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



And there are several projects, some of which will use mysql7 and others Mysql5.7.
Here is the docker-compose.yml that runs them:
docker-compose.yml (projects)

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



the server is configured by default (nginx + php7.1/5.6/7.0/7.4), just in case, I will throw off the Dockerfile (they are all plus or minus the same configured):
Dockerfile

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;"



So, I created mysql containers, and containers with projects.
I added them to the desired networks "mysql-5.7-network" for example.
When I write something like this in index.php:
$dbh = new PDO('mysql:host=mysql-5.7-network;dbname=test', 'roots', 'secret');

He swears that there is no host. Where is the mistake?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2020-01-03
@Synacs-U

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 question

Ask a Question

731 491 924 answers to any question