Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run apps inside Docker containers #379

Merged
merged 22 commits into from
Sep 6, 2020
Merged

Conversation

ggwadera
Copy link
Collaborator

@ggwadera ggwadera commented Sep 4, 2020

Closes #377
Closes #281

This PR will change the apps process management from pm2 to docker, running the apps inside containers to achieve better isolation, this way protecting the main system from malicious apps.

Now docker is a requirement to run myProxy.

There's been a lot of changes, so read and review this PR carefully. I'm always open to your suggestions or questions.

PR Summary

  • Create a new container when a new domain is created.
    • Bind the correct port for the domain to the container.
    • Pass the app port as a environment variable to the container.
    • Bind the domain directory to a folder inside the container.
    • Set the container default command to start the app.
  • Start the apps using a Docker container instead of pm2 when the user pushes a new commit.
  • Get the container logs (stdout and stderr).
  • Get existing containers list and status (running or stopped).
  • Stop and delete the container when domain is deleted.
  • Start/Restart/Stop containers through the client

Changed

  • setup.sh script

    • checks if docker is installed before executing, exiting if it's not
    • added more checks so the script does not stop halfway and does not executes unnecessary commands
    • pulls the necessary node docker image (node:alpine (Alpine is a lightweight linux distro, the image is based on that))
  • post-receive script

    • starts the app container by sending a request through curl to the docker socket on the machine
  • api/mappings.ts

    • replaced every use of pm2 to use docker instead, using it's API instead of exec commands
    • when a domain is created, a new container is created with all the needed configurations like port and directory bindings
    • the domain id is set to be the same as the container id
    • on a git push the container is started through the post-receivescript
    • the delete feature was changed to also remove the container
    • the apps are started by running node . in the app directory. For this to work, package.json needs to be configured correctly by setting the app entry point file in the main property (for example "main": "index.js").
  • api/logs.ts

    • merged the stdout and stderr endpoints into a single one
    • now returns the logs from docker
  • public/client.ts

    • changed the logs buttons to work with the new parameters
    • changed the domain status check to the docker equivalent

Added

  • new package dependency: dockerode, used to communicate with docker through it's API

  • new docker helper functions in helpers/docker.ts

  • new query params for the logs api

    • follow: boolean: works like heroku logs tail where the log keeps updating with new changes. Does not work in the browser. Useful in the terminal when used with curl.
    • tail: number: returns the last n lines from the log.
  • new start and stop buttons in the client webpage, with the respective endpoints in the mappings api

    • start/restart: /api/mappings/{id}/start
    • stop: /api/mappings/{id}/stop

image

Removed

  • clear logs feature

    • docker doesn't provide an easy way to clear it's logs, so I though better to remove this
    • as an alternative, we can get the log's tail (last n lines) by a query param in the url: /api/logs/{stream}/{domain}?tail={numLines}
  • deploy.config.js is not needed anymore, all the domain configs are set when the container is created

Possible features to be developed in the future

  1. Add tests for the docker integration (would this require the dev/test env to have docker installed in their system?)
  2. Integrate the c0d3-cli tool to deploy domains through the api

scripts/setup.sh Outdated Show resolved Hide resolved
scripts/setup.sh Show resolved Hide resolved
@songz
Copy link
Collaborator

songz commented Sep 6, 2020

approved - to onboard new user, did not review thoroughly.

Nice comments in the code, at a high level everything makes sense.

@songz songz merged commit bbdabe7 into garageScript:master Sep 6, 2020
@ggwadera ggwadera deleted the docker branch September 7, 2020 12:52
@ggwadera ggwadera linked an issue Sep 7, 2020 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing file on home installation Run apps inside Docker containers Ability to turn app on and off
3 participants