HA fails to communicate with external API

I have HA and an express API running on different images under one Docker container on my ubuntu server. I attempted to configure a switch using the rest platform, but it failed to show up as a switch on my HA dashboard. So, I changes the platform to command_line, using curl to make the requests- this made the switch show up on my dashboard.

I’ve confirmed that the curl commands work when I ssh into the server, and the endpoints on my API are all working as expected. The switch, however, does nothing, fails to log to the Logbook, and the home-assistant.log file is filled with errors and warnings about timeouts.
home-assistant.log:

"home-assistant.log" [readonly] 3L, 617B                                                                                                    1,1           All
2023-06-21 21:44:37.320 ERROR (SyncWorker_0) [homeassistant.components.command_line.utils] Timeout for command: curl -X POST -H "Content-Type: application/json" -d "{\"state\":\"dark\"}" http://192.168.0.41:8081/roku/state
2023-06-21 21:44:37.324 ERROR (MainThread) [homeassistant.components.command_line] Command failed: curl -X POST -H "Content-Type: application/json" -d "{\"state\":\"dark\"}" http://192.168.0.41:8081/roku/state
2023-06-21 21:44:37.326 WARNING (MainThread) [homeassistant.components.command_line] Updating Command Line Switch roku_dark_mode took longer than the scheduled update interval 0:00:30

configuration.yaml:

switch:
  - platform: command_line
    switches:
      roku_dark_mode:
        command_on: 'curl -X POST -H "Content-Type: application/json" -d "{\"state\":\"dark\"}" http://192.168.0.41:8081/roku/state'
        command_off: 'curl -X POST -H "Content-Type: application/json" -d "{\"state\":\"light\"}" http://192.168.0.41:8081/roku/state'
        command_state: 'curl http://192.168.0.41:8081/roku/state | jq -r ".state"'
        value_template: '{{ value == "dark" }}'
# Below is a test switch to confirm that switches were bring picked up.
  - platform: rest
    name: "jsonplaceholder_test"
    resource: https://jsonplaceholder.typicode.com/posts
    body_on: '{"title":"foo","body":"bar","userId":1}'
    body_off: '{"title":"baz","body":"qux","userId":1}'
    headers:
      Content-Type: "application/json; charset=utf-8"

Previous attempt at a rest platform:

- platform: rest
  name: "roku_dark_mode"
  resource: http://192.168.0.41:8081/roku/state
  body_on: '{"state":"dark"}'
  body_off: '{"state":"light"}'
  is_on_template: "{{value_json.state == 'dark'}}"
  headers:
    Content-Type: application/json

Heh? I don’t understand that sentence and what it has to do with the rest of your explanations…

What server?

What is 192.168.0.41?

Have you exposed port 8081 outside your container? Ie

docker container run -d -p 8081:8081/tcp

I found the issue when I switched back the the rest platform for the switch. While the switch still failed to show up on my HA dashboard, an error message in my home-assistant.log explained that the switch was ‘not ready’ and no API endpoint was available.
I realized that there was an issue in communication between the docker images. Homeassistant couldn’t reach 192.168.0.41:8081 (my api).
I initiated an internal network in my docker-compose file and updated the api endpoints to homeserver:8081 which enabled communication between the docker images.
initialize the internal network:

$ docker network create mynetwork

update docker-compose.yaml

"docker-compose.yaml" 68L, 1519B                                                                                                            58,7          All
version: "3.0"
services:

  portainer:
    container_name: portainer
    image: portainer/portainer
    restart: always
    stdin_open: true
    tty: true
    ports:
      - "9000:9000/tcp"
    environment:
      - TZ=America/Denver
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /opt/portainer:/data
    networks:
      - mynetwork

  homeassistant:
    container_name: homeassistant
    image: "ghcr.io/home-assistant/home-assistant:stable"
    ports:
      - "8123:8123"
    volumes:
      - /opt/homeassistant/config:/config
      - /etc/localtime:/etc/localtime:ro
    restart: unless-stopped
    privileged: true
    #network_mode: host
    networks:
      - mynetwork

  plex:
    image: lscr.io/linuxserver/plex:latest
    container_name: plex
      #network_mode: host
    ports:
      - "32400:32400"
    volumes:
      - /opt/plex/config:/config
      - /opt/plex/tv:/tv
      - /opt/plex/movies:/movies
    restart: unless-stopped
    networks:
      - mynetwork

  homeserver:
    container_name: homeserver
    image: ctheil/homeserver:latest
    ports:
      - "8081:8081"
    volumes:
      - ./data:/app/data # map local data directory to /app/data in container
    restart: unless-stopped
    networks:
      - mynetwork

networks:
  mynetwork:

Adding networks: -mynetwork to each server, and initializing the mynetwork at the bottom enables each server to be reachable via the name, so I can now reach homeserver via homeassistant via the address and port: http://homeserver:8081/api-endpoint

(homeassistant) configuration.yaml

switch:
  - platform: rest
    name: "roku_dark_mode"
    resource: http://homeserver:8081/roku/state
    body_on: '{"state":"dark"}'
    body_off: '{"state":"light"}'
    is_on_template: "{{value_json.state == 'dark'}}"
    headers:
      Content-Type: application/json