Brandon Tom
Brandon Tom

Reputation: 969

WordPress 4.8 on Docker REST API Not Accessible

So I recently installed WordPress using Docker, which is a straightforward fill-in-the-blank compose file from Docker docs (https://docs.docker.com/compose/wordpress/).

version: '3'

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 
      MYSQL_DATABASE: 
      MYSQL_USER: 
      MYSQL_PASSWORD: 

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: 
      WORDPRESS_DB_PASSWORD: 
volumes:
    db_data:

It's up-and-running. That part is fine, but when I head to http://siteurl/wp-json I get a 404. Site works fine, but the REST API isn't accessible. I have another site running on WAMP and when I got to that address it pops out:

{
  "name": "localhost:8090",
  "description": "Just another WordPress site",
  "url": "http://localhost:8090/wordpress",
  "home": "http://localhost:8090/wordpress",
  "gmt_offset": "0",
  "timezone_string": "",
  "namespaces": [
    "oembed/1.0",
    "wp/v2"
  ],
  ...

Both sites are running 4.8. How do I access the REST API when running WordPress on Docker? I usually develop locally using Docker and don't recall this being a problem.

(As a side note, I spun up an WordPress container that Bitnami publishes and had no problem getting the proper response. So this is an issue with the... official WordPress image? Maybe the underlying stack for the image?? I can use it, but I'd really, really, really like to know what the problem is because I've been seeing a similar issue crop up for my fellow devs)

Upvotes: 17

Views: 7744

Answers (2)

M_Badrah
M_Badrah

Reputation: 485

I run on Docker and faced the same problem the solution was : in the AdminPanal => Settings -> Permalinks -> change from 'default' to 'Post name'

Upvotes: 12

Brandon Tom
Brandon Tom

Reputation: 969

As it turns out this has to do with the permalink setting for your site. The /wp-json/wp/v2 end point is available when you set your site is set up to use the custom permalink setting. If I use the /%post%/ permalink structure it works. There is an alternate route for sites that use other permalink structures:

On sites without pretty permalinks, the route is instead added to the URL as the rest_route parameter. For the above example, the full URL would then be http://example.com/?rest_route=/wp/v2/posts/123

Source: https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/

In fact the ?rest_route=/wp/v2/posts appears to always work, making it the more reliable option.

Upvotes: 44

Related Questions