Added support for converting bash scripts to Dockerfiles
This commit is contained in:
61
main.cpp
61
main.cpp
@@ -4,6 +4,8 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <fstream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -19,6 +21,7 @@ public:
|
|||||||
void stopContainer();
|
void stopContainer();
|
||||||
void removeContainer();
|
void removeContainer();
|
||||||
void execShell();
|
void execShell();
|
||||||
|
void createDockerfile();
|
||||||
void spinUpMySQL();
|
void spinUpMySQL();
|
||||||
void showContainerIP();
|
void showContainerIP();
|
||||||
|
|
||||||
@@ -193,6 +196,57 @@ void DockerManager::showContainerIP() {
|
|||||||
cout << "Container IP Address: " << ip;
|
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 ----------------
|
// ---------------- Menu ----------------
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@@ -214,7 +268,8 @@ int main() {
|
|||||||
<< "10. Attach Shell to Running Container\n"
|
<< "10. Attach Shell to Running Container\n"
|
||||||
<< "11. Spin Up MySQL Container\n"
|
<< "11. Spin Up MySQL Container\n"
|
||||||
<< "12. Get Container IP Address\n"
|
<< "12. Get Container IP Address\n"
|
||||||
<< "13. Exit\n"
|
<< "13. Create Dockerfile from Bash Script\n"
|
||||||
|
<< "14. Exit\n"
|
||||||
<< "----------------------------------\n"
|
<< "----------------------------------\n"
|
||||||
<< "Choose an option: ";
|
<< "Choose an option: ";
|
||||||
|
|
||||||
@@ -233,7 +288,8 @@ int main() {
|
|||||||
case 10: docker.execShell(); break;
|
case 10: docker.execShell(); break;
|
||||||
case 11: docker.spinUpMySQL(); break;
|
case 11: docker.spinUpMySQL(); break;
|
||||||
case 12: docker.showContainerIP(); break;
|
case 12: docker.showContainerIP(); break;
|
||||||
case 13:
|
case 13: docker.createDockerfile(); break;
|
||||||
|
case 14:
|
||||||
cout << "Exiting Tux-Dock.\n";
|
cout << "Exiting Tux-Dock.\n";
|
||||||
return 0;
|
return 0;
|
||||||
default:
|
default:
|
||||||
@@ -242,3 +298,4 @@ int main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user