fix a bunch of variable mismatches, add checks for env vars, remove -it from docker commands that don't need it

This commit is contained in:
Silas 2023-07-27 20:30:13 -04:00
parent cf9b13efdf
commit 8941c96ede
Signed by: silentsilas
GPG Key ID: 4199EFB7DAA34349
5 changed files with 78 additions and 28 deletions

View File

@ -12,8 +12,9 @@ Things to keep in mind:
* [Android Studio](https://developer.android.com/studio) installed with `platform-tools` in your path.
* _Note:_ This is likely configured if you've already developed for Android/RN on your machine
* [Docker Desktop ](https://docs.docker.com/desktop/install/mac-install/)
* [Docker Desktop ](https://docs.docker.com/desktop/install/mac-install/)
* [OrbStack](https://orbstack.dev/) _(great alternative but it will be out of its free beta soon and may not be stable)_
* If you're running Linux, you can also just use the docker CLI and docker service, installed through your package manager.
* A React Native project to dockerize!
## Installation
@ -67,7 +68,7 @@ docker-compose up
./dockdroid.sh run-android
```
And you're all set! The following are all of the helper commands for managing the container. Regular docker commands work as well. Refer to `dockdroid.sh` for examples.
And you're all set! The following are all of the helper commands for managing the container, created with the help of [GPT-4](https://chat.openai.com/share/648b4a38-9876-48d5-9e3b-a9c10bb25921). Regular docker commands work as well. Refer to `dockdroid.sh` for examples.
| Command | Description |
| ------------- | ------------- |

View File

@ -1,38 +1,63 @@
#!/bin/bash
if [[ -z "${RN_PROJECT_FOLDER_NAME}" ]]; then
echo "Please set the RN_PROJECT_FOLDER_NAME environment variable before running the script."
# Exit immediately if a command exits with a non-zero status.
set -e
# Source the environment variables
source $(dirname "${BASH_SOURCE[0]}")/.env
# Validate the container name
if [[ -z "${CONTAINER_NAME}" ]]; then
echo "Please set the CONTAINER_NAME environment variable before running the script."
exit 1
fi
# Define the project directory
PROJECT_DIR="/home/app"
# Docker command handling
case $1 in
stop-metro)
docker exec -it ${RN_PROJECT_FOLDER_NAME} supervisorctl stop npm
docker exec -it ${CONTAINER_NAME} supervisorctl stop npm
;;
start-metro)
docker exec -it ${RN_PROJECT_FOLDER_NAME} supervisorctl start npm -- --reset-cache
docker logs -f ${RN_PROJECT_FOLDER_NAME}
;;
metro-logs)
docker exec -it ${RN_PROJECT_FOLDER_NAME} cat /var/log/app/npm.log
;;
connect-android)
docker exec -ti ${RN_PROJECT_FOLDER_NAME} adb connect host.docker.internal
;;
run-android)
docker exec -ti ${RN_PROJECT_FOLDER_NAME} adb connect host.docker.internal
docker exec -ti ${RN_PROJECT_FOLDER_NAME} npx react-native run-android
;;
reinstall-node-modules)
docker exec -it ${RN_PROJECT_FOLDER_NAME} /bin/bash -c "cd /home/${RN_PROJECT_FOLDER_NAME} && rm -rf node_modules && npm install"
;;
shell)
if [[ -z "${RN_PROJECT_FOLDER_NAME}" ]]; then
echo "Please set the RN_PROJECT_FOLDER_NAME environment variable before running the script."
exit 1
fi
docker exec -it ${RN_PROJECT_FOLDER_NAME} /bin/bash -c "cd /home/${RN_PROJECT_FOLDER_NAME} && exec /bin/bash"
docker exec ${CONTAINER_NAME} /bin/bash -c "echo \"//github.com/:_authToken=$GH_TOKEN\" > ${PROJECT_DIR}/.npmrc"
docker exec ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && npm install" || { echo 'npm install failed' ; exit 1; }
docker exec ${CONTAINER_NAME} /bin/bash -c "rm -f ${PROJECT_DIR}/.npmrc"
docker exec ${CONTAINER_NAME} supervisorctl start npm -- --reset-cache
docker logs -f ${CONTAINER_NAME}
;;
metro-logs)
docker exec -it ${CONTAINER_NAME} cat /var/log/app/npm.log
;;
connect-android)
docker exec ${CONTAINER_NAME} adb connect host.docker.internal
;;
run-android)
docker exec ${CONTAINER_NAME} adb connect host.docker.internal
docker exec -ti ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && npx react-native run-android"
;;
reinstall-node-modules)
if [[ -z "${RN_PROJECT_FOLDER_NAME}" ]]; then
echo "Please set the RN_PROJECT_FOLDER_NAME environment variable before running the script."
exit 1
fi
docker exec ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && rm -rf node_modules && npm install" || { echo 'npm install failed' ; exit 1; }
;;
shell)
docker exec -it ${CONTAINER_NAME} /bin/bash -c "cd ${PROJECT_DIR} && exec /bin/bash"
;;
init-ssh)
if [[ -z "${SSH_KEY}" ]]; then
echo "Please set the SSH_KEY environment variable before running the script."
@ -47,6 +72,18 @@ case $1 in
echo "SSH key already exists at ${SSH_KEY}"
fi
# Check if Docker is running
if ! docker info >/dev/null 2>&1; then
echo "Docker is not running. Please start Docker and try again."
exit 1
fi
# Check if the specific container is running
if [ "$(docker inspect -f '{{.State.Running}}' ${CONTAINER_NAME})" != "true" ]; then
echo "The container ${CONTAINER_NAME} does not exist or is not running."
exit 1
fi
# get docker container's IP
CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${CONTAINER_NAME})
@ -60,17 +97,21 @@ case $1 in
echo "Docker IP added to SSH config."
fi
;;
clear-known-hosts)
echo "This will clear known hosts. Please run this command as root or use sudo."
if [ "$EUID" -ne 0 ]
then
echo "Please run this command as root or use sudo."
exit 1
fi
CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${CONTAINER_NAME})
ssh-keygen -R ${CONTAINER_IP}
;;
*)
echo "Invalid command"
echo "Usage: $0 {logs|connect|run-android|stop}"
echo "Usage: $0 {stop-metro|start-metro|metro-logs|connect-android|run-android|reinstall-node-modules|shell|init-ssh|clear-known-hosts}"
;;
esac

View File

@ -7,6 +7,8 @@ services:
dockerfile: Dockerfile
args:
- SSH_KEY=${SSH_KEY}
- RN_PROJECT_FOLDER_NAME=${RN_PROJECT_FOLDER_NAME}
- GH_TOKEN=${GH_TOKEN}
container_name: "${CONTAINER_NAME}"
volumes:
- type: bind

View File

@ -3,13 +3,13 @@
set -e
# Github auth for private npm packages
su -c "echo \"//github.com/:_authToken=$GH_TOKEN\" > /home/${RN_PROJECT_FOLDER_NAME}/.npmrc"
su -c "echo \"//github.com/:_authToken=$GH_TOKEN\" > /home/app/.npmrc"
# install npm dependencies
su -c "cd /home/${RN_PROJECT_FOLDER_NAME} && npm install"
su -c "cd /home/app && npm install"
# remove the token
su -c "rm -f /home/${RN_PROJECT_FOLDER_NAME}/.npmrc"
su -c "rm -f /home/app/.npmrc"
# run supervisord
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf

View File

@ -4,6 +4,12 @@ nodaemon=true
[unix_http_server]
file=/var/run/supervisor.sock
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock
[program:ssh]
command=/usr/sbin/sshd -D
user=root