Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a shared MySQL container

Tags:

Tl;Dr; Trying to get WordPress docker-compose container to talk to another docker-compose container.

On my Mac I have a WordPress & MySQL container which I have built and configured with a linked MySQL server. In production I plan to use a Google Cloud MySQL storage instance, so plan on removing the MySQL container from the docker-compose file (unlinking it) and then separate shared container I can use from multiple docker containers.

The issue I'm having is that I cant connect the WordPress container to the separate MySQL container. Would anyone be able to shed any light on how I might go about this?

I have tried unsuccessfully to create a network as well as tried creating a fixed IP that the local box has reference to via the /etc/hosts file (my preferred configuration as I can update the file according to ENV)

WP:

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
like image 715
gazzwi86 Avatar asked Jan 09 '17 05:01

gazzwi86


People also ask

Can two MySQL containers use the same Docker volume?

No,MySQL and MariaDB (that nginx_proxy_manager documents, are not shared storage databases, and only might detect each others appearance.

Can two containers talk to each other?

A Docker network lets your containers communicate with each other. If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other.


1 Answers

What you probably want to do is create a shared Docker network for the two containers to use, and point them both to it. 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 to it. When you're using docker-compose, you would do this at the bottom of your YAML file. This would go at the top level of the file, i.e. all the way to the left, like volumes:.

networks:
  default:
    external:
      name: sharednet

To do the same thing on a normal container (outside compose), you can pass the --network argument.

docker run --network sharednet [ ... ]
like image 135
Dan Lowe Avatar answered Sep 25 '22 10:09

Dan Lowe