Compare commits

...

2 Commits

Author SHA1 Message Date
mrkmntal
426dd6e196 Add detached exec option to Docker manager 2025-11-02 08:32:31 -05:00
mrkmntal
a43de98578 Refactor Option 13: Streamline bash script to Docker image conversion workflow 2025-11-01 20:00:21 -04:00
4 changed files with 67 additions and 16 deletions

View File

@@ -11,8 +11,10 @@ It offers a clean, interactive menu for common Docker operations like pulling im
- 🔹 **Interactive container management** — start, stop, remove, or attach to containers with simple numbered menus. - 🔹 **Interactive container management** — start, stop, remove, or attach to containers with simple numbered menus.
- 🔹 **Port mapping made clear** — automatically prompts for and explains host ↔ container port bindings. - 🔹 **Port mapping made clear** — automatically prompts for and explains host ↔ container port bindings.
- 🔹 **Image operations** — pull, list, and delete Docker images. - 🔹 **Image operations** — pull, list, and delete Docker images.
- 🔹 **Script-to-image workflow** — turn a bash setup script into a Dockerfile and build the resulting image in one go.
- 🔹 **Quick MySQL setup** — spin up a MySQL container with version, password, and port configuration in seconds. - 🔹 **Quick MySQL setup** — spin up a MySQL container with version, password, and port configuration in seconds.
- 🔹 **Get container IP address** — cleanly retrieves and displays only the containers assigned IP. - 🔹 **Get container IP address** — cleanly retrieves and displays only the containers assigned IP.
- 🔹 **Run detached commands** — execute background jobs inside a container without attaching an interactive shell.
- 🔹 **Modern C++ design** — built with classes, minimal dependencies, and clear abstractions. - 🔹 **Modern C++ design** — built with classes, minimal dependencies, and clear abstractions.
--- ---
@@ -57,9 +59,11 @@ Tux-Dock: Docker Management Menu
8. Stop Container 8. Stop Container
9. Remove Container 9. Remove Container
10. Attach Shell to Running Container 10. Attach Shell to Running Container
11. Spin Up MySQL Container 11. Run Detached Command in Container
12. Get Container IP Address 12. Spin Up MySQL Container
13. Exit 13. Get Container IP Address
14. Create Dockerfile & Build Image from Bash Script
15. Exit
``` ```
Each action guides you through the required steps. Each action guides you through the required steps.
@@ -90,6 +94,7 @@ class DockerManager {
void runContainerInteractive(); void runContainerInteractive();
void listContainers() const; void listContainers() const;
void startInteractive(); void startInteractive();
void execDetachedCommand();
void stopContainer(); void stopContainer();
void showContainerIP(); void showContainerIP();
}; };

View File

@@ -6,6 +6,7 @@
#include <array> #include <array>
#include <fstream> #include <fstream>
#include <filesystem> #include <filesystem>
#include <limits>
using namespace std; using namespace std;
@@ -21,6 +22,7 @@ public:
void stopContainer(); void stopContainer();
void removeContainer(); void removeContainer();
void execShell(); void execShell();
void execDetachedCommand();
void createDockerfile(); void createDockerfile();
void spinUpMySQL(); void spinUpMySQL();
void showContainerIP(); void showContainerIP();
@@ -153,6 +155,35 @@ void DockerManager::execShell() {
if (!id.empty()) runCommand("docker exec -it " + id + " /bin/sh"); if (!id.empty()) runCommand("docker exec -it " + id + " /bin/sh");
} }
void DockerManager::execDetachedCommand() {
string id = selectContainer("Select container to run command in (detached)");
if (id.empty()) return;
// Flush any leftover newline before using getline
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string command;
cout << "Enter command to execute inside the container: ";
getline(cin, command);
if (command.empty()) {
cout << "No command entered. Aborting.\n";
return;
}
string escapedCommand;
escapedCommand.reserve(command.size() * 2);
for (char c : command) {
if (c == '"' || c == '\\')
escapedCommand += '\\';
escapedCommand += c;
}
cout << "Executing command in detached mode...\n";
runCommand("docker exec -d " + id + " /bin/sh -c \"" + escapedCommand + "\"");
cout << "Command dispatched.\n";
}
void DockerManager::spinUpMySQL() { void DockerManager::spinUpMySQL() {
string port, password, version; string port, password, version;
cout << "Enter port mapping (e.g., 3306:3306): "; cout << "Enter port mapping (e.g., 3306:3306): ";
@@ -197,7 +228,7 @@ void DockerManager::showContainerIP() {
} }
void DockerManager::createDockerfile() { void DockerManager::createDockerfile() {
string baseImage, bashScriptPath, outputFile; string baseImage, bashScriptPath, outputFile, imageName;
cout << "Enter base Docker image (e.g., ubuntu:22.04): "; cout << "Enter base Docker image (e.g., ubuntu:22.04): ";
cin >> baseImage; cin >> baseImage;
@@ -243,8 +274,17 @@ void DockerManager::createDockerfile() {
scriptFile.close(); scriptFile.close();
cout << "Dockerfile created successfully: " << outputFile << "\n"; cout << "Dockerfile created successfully: " << outputFile << "\n";
cout << "You can edit it or build it with:\n"; cout << "Enter image name to build from this Dockerfile (e.g., myimage): ";
cout << " docker build -t myimage -f " << outputFile << " .\n"; cin >> imageName;
if (imageName.empty()) {
cout << "No image name provided. Skipping build.\n";
return;
}
cout << "Building Docker image '" << imageName << "'...\n";
runCommand("docker build -t " + imageName + " -f " + outputFile + " .");
cout << "Docker build command executed.\n";
} }
// ---------------- Menu ---------------- // ---------------- Menu ----------------
@@ -266,10 +306,11 @@ int main() {
<< "8. Stop Container\n" << "8. Stop Container\n"
<< "9. Remove Container\n" << "9. Remove Container\n"
<< "10. Attach Shell to Running Container\n" << "10. Attach Shell to Running Container\n"
<< "11. Spin Up MySQL Container\n" << "11. Run Detached Command in Container\n"
<< "12. Get Container IP Address\n" << "12. Spin Up MySQL Container\n"
<< "13. Create Dockerfile from Bash Script\n" << "13. Get Container IP Address\n"
<< "14. Exit\n" << "14. Create Dockerfile & Build Image from Bash Script\n"
<< "15. Exit\n"
<< "----------------------------------\n" << "----------------------------------\n"
<< "Choose an option: "; << "Choose an option: ";
@@ -286,10 +327,11 @@ int main() {
case 8: docker.stopContainer(); break; case 8: docker.stopContainer(); break;
case 9: docker.removeContainer(); break; case 9: docker.removeContainer(); break;
case 10: docker.execShell(); break; case 10: docker.execShell(); break;
case 11: docker.spinUpMySQL(); break; case 11: docker.execDetachedCommand(); break;
case 12: docker.showContainerIP(); break; case 12: docker.spinUpMySQL(); break;
case 13: docker.createDockerfile(); break; case 13: docker.showContainerIP(); break;
case 14: case 14: docker.createDockerfile(); break;
case 15:
cout << "Exiting Tux-Dock.\n"; cout << "Exiting Tux-Dock.\n";
return 0; return 0;
default: default:
@@ -297,5 +339,3 @@ int main() {
} }
} }
} }

6
option13_script.sh Normal file
View File

@@ -0,0 +1,6 @@
#!/bin/bash
echo "Setting up application directory"
mkdir -p /app/bin
echo '#!/bin/bash' > /app/bin/run.sh
echo 'echo "Hello from Tux-Dock generated image"' >> /app/bin/run.sh
chmod +x /app/bin/run.sh

BIN
tux-dock Executable file

Binary file not shown.