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

Spin deploy is not using the correct environment file #146

Closed
schnetzi opened this issue Dec 25, 2024 · 5 comments
Closed

Spin deploy is not using the correct environment file #146

schnetzi opened this issue Dec 25, 2024 · 5 comments

Comments

@schnetzi
Copy link

schnetzi commented Dec 25, 2024

Current Behavior

I currently have in my wordpress project (spin template) a .env file for my local environment and a .env.production file for my production deploy.

When I run spin deploy production I noticed that my database-user from the .env-file is used to try to connect to my database container on my server. Although I get access denied because my database-container used the .env.production-file information for the setup.

After replacing the values in the .env-file with the values from the .env.production and running a spin deploy production again the connection to the dabase works.

Expected Behavior

The .env.production-values are used for connecting to the container on the server.

Steps To Reproduce

  1. Create a new spin project (preferrable with the wordpress template: spin new schnetzi/spin-template-wordpress wordpress-problem)
  2. Provision a server for the deploy
  3. Create a .env-file for a local setup and a .env.production-file with different values for the production deploy
  4. Run spin deploy production
  5. Database connection should fail

Environment

Spin Version: 
v3.0.1 [beta] (User Installed)

Operating System Version: 
ProductName:            macOS
ProductVersion:         15.2
BuildVersion:           24C101

Docker Info: 
WARNING: daemon is not using the default seccomp profile
Client:
 Version:    27.3.1
 Context:    desktop-linux
 Debug Mode: false
 Plugins:
  ai: Ask Gordon - Docker Agent (Docker Inc.)
    Version:  v0.1.0
    Path:     /Users/ps/.docker/cli-plugins/docker-ai
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.18.0-desktop.2
    Path:     /Users/ps/.docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.30.3-desktop.1
    Path:     /Users/ps/.docker/cli-plugins/docker-compose
  debug: Get a shell into any image or container (Docker Inc.)
    Version:  0.0.37
    Path:     /Users/ps/.docker/cli-plugins/docker-debug
  desktop: Docker Desktop commands (Alpha) (Docker Inc.)
    Version:  v0.0.15
    Path:     /Users/ps/.docker/cli-plugins/docker-desktop
  dev: Docker Dev Environments (Docker Inc.)
    Version:  v0.1.2
    Path:     /Users/ps/.docker/cli-plugins/docker-dev
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.27
    Path:     /Users/ps/.docker/cli-plugins/docker-extension
  feedback: Provide feedback, right in your terminal! (Docker Inc.)
    Version:  v1.0.5
    Path:     /Users/ps/.docker/cli-plugins/docker-feedback
  init: Creates Docker-related starter files for your project (Docker Inc.)
    Version:  v1.4.0
    Path:     /Users/ps/.docker/cli-plugins/docker-init
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc.)
    Version:  0.6.0
    Path:     /Users/ps/.docker/cli-plugins/docker-sbom
  scout: Docker Scout (Docker Inc.)
    Version:  v1.15.0
    Path:     /Users/ps/.docker/cli-plugins/docker-scout

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 33
 Server Version: 27.3.1
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 472731909fa34bd7bc9c087e4c27943f9835f111
 runc version: v1.1.13-0-g58aa920
 init version: de40ad0
 Security Options:
  seccomp
   Profile: unconfined
  cgroupns
 Kernel Version: 6.10.14-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 10
 Total Memory: 12.65GiB
 Name: docker-desktop
 ID: dc78c172-9f79-4bf3-8427-e0311c830d18
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Labels:
  com.docker.desktop.address=unix:///Users/ps/Library/Containers/com.docker.docker/Data/docker-cli.sock
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5555
  127.0.0.0/8
 Live Restore Enabled: false

Anything else?

I am currently using a Google Cloud server.
I also tried to start over with the server but it still didin't work. Moving the env-variables to the .env file solved it though.

@jaydrogers
Copy link
Member

This could be a limitation in WordPress. With Laravel, we set the APP_ENV which then tells Laravel to call the correct environment file: https://laravel.com/docs/11.x/configuration#additional-environment-files

This is something with WordPress that would take some logic to ensure it loads the correct environment file.

@schnetzi
Copy link
Author

schnetzi commented Jan 9, 2025

Thanks for your reply. Okay I see. So it will always put all environment files into the container. I thought the spin deploy production is using the .env.production-file and deploying it as the .env-file.
Will need to think about that. Maybe I can manage it with the dotenv package I am already using (https://github.com/vlucas/phpdotenv)

@schnetzi
Copy link
Author

schnetzi commented Jan 12, 2025

@jaydrogers To understand it correctly, when is the APP_ENV set to production? Would it be enough to add an APP_ENV into my environment files and rely on it being updated to production when I run spin deploy production?
So then adding a logic to my config file where I load the base .env and look at the APP_ENV and then optionally load the .env.[environment] depending on the APP_ENV.

@jaydrogers
Copy link
Member

I think we may have discussed this in Discord. Just in case we didn't, Laravel is the one who listens to APP_ENV.

Then Laravel looks for the file of .env.<environment> if it exists. If it doesn't it falls back to .env.

This logic would have to be developed on your own or incorporated into WordPress somehow. Closing this issue since it's not an issue with Spin, but feel free to ask more questions if you need anything else

@jaydrogers jaydrogers closed this as not planned Won't fix, can't repro, duplicate, stale Jan 15, 2025
@schnetzi
Copy link
Author

Thanks for your response. My question kind of was how APP_ENV is set to production in the production container. But I actually only needed to know that it somehow gets set during the deploy.

My logic for using the correct .env file is now added commit. Thanks for your help

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

2 participants