zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
29.37k stars 3.97k forks source link

fix: ensure Dockerfile includes etc directory and correct CMD based on config #4343

Open JackGod001 opened 2 months ago

JackGod001 commented 2 months ago

fix: ensure Dockerfile includes etc directory and correct CMD based on config

Bug Fix: Include etc directory in Dockerfile and set correct CMD based on config

Description

This PR fixes two related issues:

  1. The etc directory was not being included in the generated Dockerfile when running goctl docker from a directory different from where the Go file is located.
  2. Due to the missing etc directory, the Dockerfile's CMD was not being set correctly based on the configuration file.

Problems

  1. When users ran goctl docker from a directory that was not the same as the Go file's location, the generated Dockerfile did not include the etc directory. This caused issues for projects that rely on configuration files stored in the etc directory.
  2. As a consequence of the missing etc directory, the Dockerfile was not able to locate and use the configuration file to set the correct CMD instruction. This led to incorrect startup commands in the resulting Docker containers.

Solution

The fix involves modifying the dockerCommand function to:

  1. Correctly check for the existence of the etc directory relative to the Go file's location, rather than the current working directory.
  2. Ensure that when the etc directory exists, it's properly included in the Dockerfile, and the CMD is set based on the configuration file.

Changes made:

  1. Updated the logic to check for the etc directory using the Go file's path as a reference.
  2. Used filepath.Join for better cross-platform compatibility.
  3. Ensured that the etc directory is included in the Dockerfile if it exists in the same directory as the Go file.
  4. Fixed the logic to properly set the CMD in the Dockerfile based on the configuration file when the etc directory is found.

Testing

Tested the changes by:

  1. Running goctl docker from various directory locations relative to the Go file, confirming that the etc directory is correctly included in all cases where it exists.
  2. Verifying that the generated Dockerfile contains the correct CMD instruction based on the configuration file when the etc directory is present.
  3. Building and running containers using the generated Dockerfiles to ensure they start up correctly with the proper configuration.

Additional Notes

This fix improves the reliability and consistency of the goctl docker command, especially for users working with more complex project structures or running the command from different locations within their project. It ensures that not only is the etc directory properly included, but also that the resulting Docker containers are configured correctly based on the project's configuration files.

JackGod001 commented 2 months ago

This PR addresses a potential issue with path duplication when generating Dockerfiles. The change uses filepath.Base() to extract just the filename from goFile, preventing situations where the project path might be inadvertently duplicated.

Changes made:

This change ensures that the path used in the Dockerfile is always relative to the project root, improving consistency and preventing potential build issues due to incorrect file paths.

Please merge the two submissions together, otherwise it will be incomplete

codecov[bot] commented 2 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 95.01%. Comparing base (8690859) to head (2153f14). Report is 137 commits behind head on master.

Additional details and impacted files [see 255 files with indirect coverage changes](https://app.codecov.io/gh/zeromicro/go-zero/pull/4343/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro)