Skip to content

Instantly share code, notes, and snippets.

@Mihai-P
Last active July 22, 2021 19:55
Show Gist options
  • Select an option

  • Save Mihai-P/3fc131a8a8e78a406ec45836643d2b85 to your computer and use it in GitHub Desktop.

Select an option

Save Mihai-P/3fc131a8a8e78a406ec45836643d2b85 to your computer and use it in GitHub Desktop.
Interesting operations

Docker

Connecting to a docker image based on the name of the image

docker exec -i -t $(docker ps -aqf "name=my-image") /bin/bash

Copying something from a docker container based on the image name

docker cp $(docker ps -aqf "name=my-image"):/some-file.txt .

Postgres

Given the following environmental variables

DB_HOST=localhost
DB_NAME=greatdb
$DB_DETAILS={\"username\":\"greatuser\",\"password\":\"greatpassword\"}

Connecting to a DB from the command line, the user and password are copied from a JSON env variable

export PGPASSWORD=$(echo $DB_DETAILS | jq .password); psql -h $DB_HOST -U $(echo $DB_DETAILS | jq .password) $DB_NAME

Dumping a DB to a file

export PGPASSWORD=$(echo $DB_DETAILS | jq .password); pg_dump -h $DB_HOST -d $DB_NAME -U $(echo $DB_DETAILS | jq .password) > some-file.sql

Importing into a DB

export PGPASSWORD=$(echo $DB_DETAILS | jq .password); psql -h $DB_HOST -d $DB_NAME -U $(echo $DB_DETAILS | jq .password) -f /some-file.sql

ssh

Copying a file using a certificate

scp -i ~/.ssh/my-certificate.pem ./from-file.txt user@host:/tmp/to-file.txt

set up a certificate in .ssh/config

Host myGreatHost
  Hostname IPorHostname
  User username
  ForwardAgent yes
  IdentityFile ~/.ssh/myKey.pem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment