[Docker] Add a SQLite Console Shortcut with the Dockerfile

发布时间 2023-05-04 14:13:59作者: Zhentiw

With a long-running node server and a database, sometimes it's useful to ssh into the virtual machine to explore the file system, and look at the database.

In Dockerfile, add:

RUN echo '#!/bin/sh\nset -xe\nsqlite3 \$DATABASE_URL' > /usr/local/bin/database_cli && chmod +x /usr/local/bin/database_cli

This command will create a new file inside user/local/bin/database_cli. This file will contain a script that runs SQLite 3 on the database URL, and makes sure the script is executable.

 

After deploying the changes with fly deploy, you can use fly ssh console -C database_cli to jump straight into the SQLite CLI.

 

  1. RUN: This is a Docker command that runs a command in a new layer on top of the current image and commits the result. This is typically used for installing packages, configuring the environment, or other similar tasks.

  2. echo '#!/bin/sh\nset -xe\nsqlite3 \$DATABASE_URL': The echo command is used to print the given string to the standard output. In this case, the string is a shell script that consists of three lines:

    • #!/bin/sh: This is a shebang (or hashbang) line that specifies the interpreter to be used for executing the script, in this case, /bin/sh (a standard Unix shell).
    • set -xe: This line sets two options for the shell:
      • -x: This option tells the shell to print each command before it's executed, which is useful for debugging.
      • -e: This option tells the shell to exit immediately if any command exits with a non-zero status, which is useful for detecting errors early.
    • sqlite3 \$DATABASE_URL: This line runs the sqlite3 command-line tool with the $DATABASE_URL environment variable as its argument. The backslash before the $ is used to escape it, ensuring that the variable is not expanded when the echo command is executed but rather when the script is run.
  3. > /usr/local/bin/database_cli: This part of the command redirects the output of the echo command to a file named database_cli in the /usr/local/bin/ directory. This is a standard location for executable files on Unix-like systems.

  4. &&: This is a shell control operator that allows you to chain multiple commands together. The second command will only be executed if the first command succeeds (returns a zero exit status).

  5. chmod +x /usr/local/bin/database_cli: This command changes the permissions of the database_cli file to make it executable. The +x option adds the execute permission for the owner, group, and others.