-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-shell-helper.sh
executable file
·72 lines (64 loc) · 2.37 KB
/
docker-shell-helper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh
#####################################
# Docker Helper Script Usage #
#####################################
#
# This script provides a menu-based interface to manage Docker containers
# for Ruby development. It allows you to:
#
# 1. Start a new container with Ruby 3.1.3 and your project files mounted
# 2. Commit changes made in the container to a new image or replace the current image
# 3. Start a new container from an existing image with your project files mounted
#
# To use this script:
#
# 1. Make sure you have Docker installed and configured correctly on your system.
# 2. Save this script with a `.sh` extension (e.g., `docker-shell-helper.sh`).
# 3. Make the script executable with the command: `chmod +x docker-shell-helper.sh`
# 4. Run the script with: `./docker-shell-helper.sh`
# 5. Follow the menu prompts to perform the desired actions.
#
# Note: Inside the container, you can run commands like `bundle install`,
# `bundle exec rspec`, and `bundle exec rails db:setup RAILS_ENV=test` as needed.
#
# Function to start a new container with the necessary Ruby version
start_container() {
docker run -it --rm --name my-ruby-container -v "${PWD}:/app" -w /app ruby:3.1.3 bash
}
# Function to commit changes to a new image
commit_changes() {
current_image=$(docker ps --latest --quiet --no-trunc)
read -p "Enter a name for the new image (leave blank to replace the current image): " image_name
if [ -z "$image_name" ]; then
docker commit my-ruby-container "$current_image"
echo "Current image replaced: $current_image"
else
docker commit my-ruby-container "$image_name"
echo "New image created: $image_name"
fi
}
# Function to start a container from an existing image
start_from_image() {
read -p "Enter the name of the image: " image_name
docker run -it --rm -v "${PWD}:/app" -w /app "$image_name" bash
}
# Function to display the available options
show_menu() {
echo "Select an option:"
echo "1. Start a new container"
echo "2. Commit changes to a new image"
echo "3. Start a container from an existing image"
echo "4. Exit"
}
# Main loop
while true; do
show_menu
read -p "Enter your choice: " choice
case $choice in
1) start_container ;;
2) commit_changes ;;
3) start_from_image ;;
4) exit ;;
*) echo "Invalid choice. Please try again." ;;
esac
done