Unable to access Wordpress site created as a Docker Stack

I am trying to create a Wordpress site inside a Docker stack composed of the following services:

  • wordpress - the Wordpress site itself
  • db - the MySQL container

I have the following stack.yml file:

version: '3.1'
networks:
  abtehnic:
services:
  db:
    image: mysql:latest
    restart: always
    networks:
      - abtehnic
    environment:
      MYSQL_ROOT_PASSWORD: XXXXX
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: barbu123
  wordpress:
    image: abtehnic-wordpress
    depends_on:
      - db
    restart: always
    ports:
      - 80
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_PASSWORD: barbu123
    networks:
      - abtehnic
    volumes:
      - ./source:/var/www/html

I deploy the stack with the following command:

$ docker stack deploy -c stack.yml abtehnic

The docker ps command outputs the following:

CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
256e65fe2c4c        mysql:latest        "docker-entrypoint..."   About a minute ago   Up About a minute   3306/tcp            abtehnic_db.1.mo0xp17adt2jocu9yivkzg19g
26481d8bab95        wordpress:4.8       "docker-entrypoint..."   About a minute ago   Up About a minute   80/tcp              abtehnic_wordpress.1.tiikrjwm1kcmxjg7v74vrcquw

I am trying to find out the mapped port via:

$ docker port <container_id for wordpress>

and its output is empty. Also when I am running docker logs <container_id_for_wordpress> I get the following:

Warning: mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 22

Warning: mysqli::mysqli(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in - on line 22

MySQL Connection Error: (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known

Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 22

MySQL Connection Error: (2002) Connection refused

Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 22

MySQL Connection Error: (2002) Connection refused

MySQL Connection Error: (2002) Connection refused

Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 22
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.0.1.3. Set the 'ServerName' directive globally to suppress this message
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.0.1.3. Set the 'ServerName' directive globally to suppress this message
[Sat Oct 28 16:05:32.189285 2017] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/5.6.31 configured -- resuming normal operations
[Sat Oct 28 16:05:32.189321 2017] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

Here are my questions:

  1. Why can't the wordpress container access the db container?
  2. Why isn't there any port that I can use to access the Wordpress site from my host?
depends_on:
  - db

That just makes sure the database container is fully loaded before the wordpress container. You need to tell docker to link the db container from the wordpress container to reference it by name.

What docker-compose does under the hood is take the ip docker gives the db container and add a /etc/hosts entry to the wordpress container so you can reference it by name.

So try adding this to the wordpress section

links:
  - db

I'll assume you're doing something like docker port 26481d8bab95, as per your question you defined port 80, so your answer is port 80:

# docker run -d -p 80:80 wordpress
dc9e4aab1015eb5d82cf489e2943d76084faceaf3093bc3238964d6cfa81ab1c
# docker port dc9e4aab1015eb5d82cf489e2943d76084faceaf3093bc3238964d6cfa81ab1c
80/tcp -> 0.0.0.0:80
# 

stack or not, you should still be able to use docker port

  1. mysqld takes longer to start comparing to httpd (wordpress), that's why you're seeing errors
  2. docker wise, it's configured correctly and the actual reason why you can't access it could be many, firewall and whatnot.

Check the logs for the db service to make sure it started: docker logs abtehnic_db.1.mo0xp17adt2jocu9yivkzg19g