Using a shared MySQL container

T; R; Trying to get a WordPress dock container container to talk to another docker storage container.

On my Mac, I have a WordPress and MySQL container that I created and configured with the associated MySQL server. In production, I plan to use an instance of Google Cloud MySQL storage, so I plan to remove the MySQL container from the docker layout file (unchecking it), and then split the shared container that I can use from several docker containers.

The problem I am facing is that I cannot connect a WordPress container to a separate MySQL container. Can anyone shed some light on how I can do this?

I tried unsuccessfully to create a network, and also tried to create a fixed IP address that the local field refers to through the / etc / hosts file (my preferred configuration, since I can update the file according to ENV)

WG:

version: '2' services: wordpress: container_name: spmfrontend hostname: spmfrontend domainname: spmfrontend.local image: wordpress:latest restart: always ports: - 8080:80 # creates an entry in /etc/hosts extra_hosts: - "ic-mysql.local:172.20.0.1" # Sets up the env, passwords etc environment: WORDPRESS_DB_HOST: ic-mysql.local:9306 WORDPRESS_DB_USER: root WORDPRESS_DB_PASSWORD: root WORDPRESS_DB_NAME: wordpress WORDPRESS_TABLE_PREFIX: spm # sets the working directory working_dir: /var/www/html # creates a link to the volume local to the file volumes: - ./wp-content:/var/www/html/wp-content # Any networks the container should be associated with networks: default: external: name: ic-network 

MySQL:

 version: '2' services: mysql: container_name: ic-mysql hostname: ic-mysql domainname: ic-mysql.local restart: always image: mysql:5.7 ports: - 9306:3306 # Create a static IP for the container networks: ipv4_address: 172.20.0.1 # Sets up the env, passwords etc environment: MYSQL_ROOT_PASSWORD: root # TODO: Change this MYSQL_USER: root MYSQL_PASS: root MYSQL_DATABASE: wordpress # saves /var/lib/mysql to persistant volume volumes: - perstvol:/var/lib/mysql - backups:/backups # creates a volume to persist data volumes: perstvol: backups: # Any networks the container should be associated with networks: default: external: name: ic-network 
+5
source share
1 answer

What you probably want to do is create a common Docker network for the two containers used and point to both of them. You can create a network using docker network create <name> . I will use sharednet as an example below, but you can use any name you like.

Once the network is there, you can point both containers at it. When you use docker-compose, you would do this at the bottom of your YAML file. This will go to the top level of the file, i.e. All to the left, for example volumes:

 networks: default: external: name: sharednet 

To do the same in a regular container (out of layout), you can pass the --network argument.

 docker run --network sharednet [ ... ] 
+3
source

Source: https://habr.com/ru/post/1262507/


All Articles