Home > front end >  Docker: Cannot connect to other container
Docker: Cannot connect to other container

Time:04-13

![image|690x402](upload://nU943fLQhXqbw3DkuXC2qfP373c.png)

docker-compose.yml

version: '3'

volumes:
  wp-assets:

services:
  mariadb:
    build: ./requirements/mariadb
    environment:
      - MYSQL_ROOT_HOST=${MYSQL_ROOT_HOST}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}

    ports:
      - "127.0.0.1:3306:3306"
      - "127.0.0.1:9999:9999" # test

  wordpress:
    environment:
      - WORDPRESS_DB_HOST=${WORDPRESS_DB_HOST}
      - WORDPRESS_DB_USER=${WORDPRESS_DB_USER}
      - WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}
      - WORDPRESS_DB_NAME=${WORDPRESS_DB_NAME}
      - WORDPRESS_TABLE_PREFIX=${WORDPRESS_TABLE_PREFIX}
      - WORDPRESS_AUTH_KEY=${WORDPRESS_AUTH_KEY}
      - WORDPRESS_SECURE_AUTH_KEY=${WORDPRESS_SECURE_AUTH_KEY}
      - WORDPRESS_LOGGED_IN_KEY=${WORDPRESS_LOGGED_IN_KEY}
      - WORDPRESS_NONCE_KEY=${WORDPRESS_NONCE_KEY}
      - WORDPRESS_AUTH_SALT=${WORDPRESS_AUTH_SALT}
      - WORDPRESS_SECURE_AUTH_SALT=${WORDPRESS_SECURE_AUTH_SALT}
      - WORDPRESS_LOGGED_IN_SALT=${WORDPRESS_LOGGED_IN_SALT}
      - WORDPRESS_NONCE_SALT=${WORDPRESS_NONCE_SALT}
    volumes:
      - wp-assets:/var/wp-assets
    build: ./requirements/wordpress
    ports:
      # host_port == 127.0.0.1:9000, allow only localhost
      - "127.0.0.1:9000:9000"

  nginx:
    #    image: nginx:latest
    depends_on: 
      - wordpress
    volumes:
      - wp-assets:/var/wp-assets
    build: ./requirements/nginx
    ports:
      # host_port == 0.0.0.0:8080, allow all interfaces
      - "8080:80"

mariadb/Dockerfile

FROM debian:buster

# install mariadb-server
RUN apt update && apt install -y mariadb-server

# allow connection from wordpress (host name)
RUN sed -e 's/127.0.0.1/wordpress/' \
        -i '/etc/mysql/mariadb.conf.d/50-server.cnf'

# used for socket
RUN mkdir -p /var/run/mysqld && \
    chown -R mysql:mysql /var/lib/mysql /var/run/mysqld && \
    chmod 777 /var/run/mysqld && \ 
    touch /var/run/mysqld/mysqld.sock

# init db here

COPY docker-entrypoint.sh /
RUN chmod  x /docker-entrypoint.sh

#ENTRYPOINT ["/docker-entrypoint.sh"]
ENTRYPOINT ["tail", "-f"]

I was trying to connect mariadb with wordpress (mariadb-client), and got an error : Can't connect to MySQL server on 'mariadb'

So I tested ports are good. But while other ports like nginx:80 or wordpress:9000 can be accessed by the other containers, ports of mariadb refuses connections.

I couldn't figure out what is difference between mariadb container and the others. What's the problem?

CodePudding user response:

The ENTRYPOINT of your MariaDB Dockerfile is

ENTRYPOINT ["tail", "-f"]

so it doesn't actually run MariaDB.

You probably need to comment that out and comment back in

ENTRYPOINT ["/docker-entrypoint.sh"]

CodePudding user response:

I was confused about ports. I thought that each containers could communicate by just opening ports, but there was no LISTENing ports. Docker-compose ports: just binds ports and has nothing to do with LISTEN.

  • Related