Added support for converting bash scripts to Dockerfiles

This commit is contained in:
2025-10-28 17:09:37 -04:00
parent 67e73a1aa2
commit e056c1d6a1

View File

@@ -4,6 +4,8 @@
#include <cstdlib>
#include <sstream>
#include <array>
#include <fstream>
#include <filesystem>
using namespace std;
@@ -19,6 +21,7 @@ public:
void stopContainer();
void removeContainer();
void execShell();
void createDockerfile();
void spinUpMySQL();
void showContainerIP();
@@ -193,6 +196,57 @@ void DockerManager::showContainerIP() {
cout << "Container IP Address: " << ip;
}
void DockerManager::createDockerfile() {
string baseImage, bashScriptPath, outputFile;
cout << "Enter base Docker image (e.g., ubuntu:22.04): ";
cin >> baseImage;
cout << "Enter path to bash script to convert (e.g., setup.sh): ";
cin >> bashScriptPath;
if (!filesystem::exists(bashScriptPath)) {
cout << "Error: Bash script not found.\n";
return;
}
cout << "Enter output Dockerfile name (e.g., Dockerfile or Dockerfile_app): ";
cin >> outputFile;
if (outputFile.empty()) outputFile = "Dockerfile";
ifstream scriptFile(bashScriptPath);
ofstream dockerfile(outputFile);
if (!scriptFile.is_open() || !dockerfile.is_open()) {
cout << "Error: Unable to open file(s).\n";
return;
}
// Write Dockerfile header
dockerfile << "FROM " << baseImage << "\n";
dockerfile << "WORKDIR /app\n\n";
dockerfile << "# Auto-generated by Tux-Dock\n";
string line;
while (getline(scriptFile, line)) {
if (line.empty()) continue;
// Skip comments or shebang
if (line.rfind("#", 0) == 0 || line.rfind("#!", 0) == 0)
continue;
dockerfile << "RUN " << line << "\n";
}
dockerfile << "\nCMD [\"/bin/bash\"]\n";
dockerfile.close();
scriptFile.close();
cout << "Dockerfile created successfully: " << outputFile << "\n";
cout << "You can edit it or build it with:\n";
cout << " docker build -t myimage -f " << outputFile << " .\n";
}
// ---------------- Menu ----------------
int main() {
@@ -214,7 +268,8 @@ int main() {
<< "10. Attach Shell to Running Container\n"
<< "11. Spin Up MySQL Container\n"
<< "12. Get Container IP Address\n"
<< "13. Exit\n"
<< "13. Create Dockerfile from Bash Script\n"
<< "14. Exit\n"
<< "----------------------------------\n"
<< "Choose an option: ";
@@ -233,7 +288,8 @@ int main() {
case 10: docker.execShell(); break;
case 11: docker.spinUpMySQL(); break;
case 12: docker.showContainerIP(); break;
case 13:
case 13: docker.createDockerfile(); break;
case 14:
cout << "Exiting Tux-Dock.\n";
return 0;
default:
@@ -242,3 +298,4 @@ int main() {
}
}