Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The "WPTT_CONTAINER_NAME" variable is not set #10

Open
sanderdekroon opened this issue Jan 25, 2022 · 7 comments
Open

The "WPTT_CONTAINER_NAME" variable is not set #10

sanderdekroon opened this issue Jan 25, 2022 · 7 comments

Comments

@sanderdekroon
Copy link

sanderdekroon commented Jan 25, 2022

Whenever I run ./docker/run compose I get an error message telling me the WPTT_CONTAINER_NAME is not set.

WARN[0000] The "WPTT_CONTAINER_NAME" variable is not set. Defaulting to a blank string.

This is weird, as the .env file does contain it:

# Generated file. Do not edit. Edit .env.docker instead

# Assign unique name for the container
WPTT_CONTAINER_NAME=owcalgolia

# Where the testing installation will be made
WPTT_INSTALL_DIR=.wp-install

(I omitted the non-relevant lines).

Running the config command shows the same problem:

$ ./docker/run compose config

WARN[0000] The "WPTT_CONTAINER_NAME" variable is not set. Defaulting to a blank string.
WARN[0000] The "WPTT_CONTAINER_NAME" variable is not set. Defaulting to a blank string.
services:
  db:
    container_name: -db
    environment:
      DBGP_IDEKEY: wptt
      MYSQL_ROOT_PASSWORD: root
      WPTT_CONTAINER_NAME: owcalgolia
      WPTT_DB_HOST: db
      WPTT_DB_NAME: owcalgolia
      WPTT_DB_PASSWORD: root
      WPTT_DB_USER: root
      WPTT_INSTALL_DIR: .wp-install
      WPTT_SITE_ADMIN_EMAIL: [email protected]
      WPTT_SITE_ADMIN_PASSWORD: password
      WPTT_SITE_ADMIN_USERNAME: admin
      WPTT_SITE_HOST: localhost:8080
    env_file:
    - ../.env
    image: mariadb
    networks:
      default: null

Am I missing something here? According to the Docker docs this should work.

@noknokcody
Copy link

Can confirm I'm also getting the same error under the same conditions.

@esamattis
Copy link
Member

esamattis commented Jan 26, 2022

@sanderdekroon
Copy link
Author

It is :). Just to be clear:

in .env.docker

# Assign unique name for the container
WPTT_CONTAINER_NAME=owcalgolia

# Where the testing installation will be made
WPTT_INSTALL_DIR=.wp-install

in .env

# Generated file. Do not edit. Edit .env.docker instead

# Assign unique name for the container
WPTT_CONTAINER_NAME=owcalgolia

# Where the testing installation will be made
WPTT_INSTALL_DIR=.wp-install

@sanderdekroon
Copy link
Author

It also should be in .env.docker, as its contents get copied over to .env. Something, somewhere isn't reading or applying the environment variables and I don't know why.

@noknokcody
Copy link

noknokcody commented Jan 27, 2022

Okay, so I've done a little bit of testing.

When I run ./docker/run compose I get the following warning

$ ./docker/run compose
WARNING: The WPTT_CONTAINER_NAME variable is not set. Defaulting to a blank string.

This is run using the following dockerfile.

version: "3.7"
services:
  wp:
    init: true
    env_file:
      - ../.env
    container_name: "${WPTT_CONTAINER_NAME}-wp"
    ports:
      - "8080:8080"
    depends_on:
      - db
    build:
      context: ..
      dockerfile: docker/Dockerfile
    volumes:
      - ..:/app
  db:
    container_name: "${WPTT_CONTAINER_NAME}-db"
    image: mariadb
    env_file:
      - ../.env

Hardcoding these variables seems to fix the issue and remove the warning. Docker isn't loading these via env_file for some reason.

I found I can revert my hardcoded changes and resolve this error by manually copying ./.env into ./docker/.env.

I updated ./docker/run to write to./docker/.env automatically instead of ./.env. Then I updated docker-compose to use the new env_file location.

Things are now running smoothly (minus some minimum PHP version errors I now have to work out).

This may be very much a hotfix, but here are the changes I made if you want a quick solution.

./docker/run

#!/bin/sh

set -eu

cmd=docker/run
help() {
    echo "
    Docker shell wrapper for WP Testing Tools

    usage:


    $cmd compose [custom options]
        Start the Docker enviroment with docker-compose

    $cmd shell
        Enter the testing shell

    $cmd update
        Update these Docker scripts

    $cmd compose-no-init
        Start the container without installing anything. Usefull for
        debugging or developing install steps

    $cmd clean
        Remove the installation directory and containers

    "
}

if [ ! -f docker/run ]; then
    >&2 echo
    >&2 echo "Oops! The docker script is supposed to be run from the parent directory"
    >&2 echo "Ex. ./docker/run or more simply just docker/run"
    >&2 echo
    exit 1
fi

echo "# Generated file. Do not edit. Edit .env.docker instead" > ./docker/.env
echo "" >> ./docker/.env
cat .env.docker >> ./docker/.env
eval $(grep -v '^#' ./docker/.env)


shell() {
    command=$@

    if [ "$command" = "" ]; then
        command="bash -l"
        >&2 echo
        >&2 echo "Welcome to WordPress testing shell!"
        >&2 echo "Your plugin is mounted to /app and all composer dependencies are put to PATH."
        >&2 echo
        >&2 echo "Try: codecept run wpunit"
        >&2 echo " or: codecept run functional"
        >&2 echo
    fi

    exec docker exec -it "${WPTT_CONTAINER_NAME}-wp" $command
}

compose() {
    command=$@

    if [ "$command" = "" ]; then
        command="up --build --abort-on-container-exit"
    fi

    exec docker-compose -f docker/docker-compose.yml $command
}

update() {
    if [ -x ./vendor/bin/wptt-configure ]; then
        exec ./vendor/bin/wptt-configure --docker
    fi

    >&2 echo
    >&2 echo "valu/wp-testing-tools not installed with composer?"
    >&2 echo
    exit 5
}

clean() {
    rm -rf "$WPTT_INSTALL_DIR"
    compose rm
}

case "${1:-}" in
    -h|--help)
        help
        exit
    ;;
    "")
        >&2 help
        exit
    ;;
    compose)
        shift
        compose $@
    ;;
    compose-no-init)
        shift
        echo "WPTT_NO_INIT=1" >> ./docker/.env
        compose $@
    ;;
    shell)
        shift
        shell $@
    ;;
    update)
        shift
        update
    ;;
    clean)
        shift
        clean
    ;;
    *)
        >&2 echo "Bad action ${1:-}"
        exit 1
    ;;
esac

./docker/docker-compose

version: "3.7"
services:
  wp:
    init: true
    env_file:
      - .env
    container_name: "${WPTT_CONTAINER_NAME}-wp"
    ports:
      - "8080:8080"
    depends_on:
      - db
    build:
      context: ..
      dockerfile: docker/Dockerfile
    volumes:
      - ..:/app
  db:
    container_name: "${WPTT_CONTAINER_NAME}-db"
    image: mariadb
    env_file:
      - .env

@noknokcody
Copy link

Just realized that .env is used by wp-install. Could update script to write to both places.

@noknokcody
Copy link

Been a while since I visited this thread. So I'll give a small update to anyone struggling.

The issue is definitely related to docker-compose not respecting the relative path defined in docker/docker-compose.yml. This could be an intentional security feature to prevent reverse directory traversal or a bug on particular environments. Either way, this issue seems to go away when you pass the environment file path directly though command line arguments.

Updating your compose method in docker/run with the following resolves the issue.

compose() {
    command=$@

    if [ "$command" = "" ]; then
        command="--env-file .env up --build --abort-on-container-exit"
    fi

    exec docker-compose -f docker/docker-compose.yml $command
}

If I get some free time I'll test this thoroughly and PR it, but hopefully those struggling will see this issue and get some relief with this fix till then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants