Docker Commands

Prefix with sudo if the current user is not root (admin) ~ applicable to Linux environment.

General

1
2
3
4
5
6
7
8
9
docker version ~ display version
docker run hello-world ~ run hello-world test container
docker logs [CONTAINER NAME] ~ works even if the container is stopped
docker images
docker ps –all ~ list containers
docker network ls ~ list networks

docker rm $(docker ps -a -q) ~ deletes all excited containers
​ first run sudo -i ~ switch to root

Stats

Along with the linux memory usage command ~ sudo free -m you can also check each containers stats:

1
2
3
sudo docker stats [CONTAINER NAME]

(CTRL C to quit)

Copy

Copy from the logs dir to the host

1
2
sudo docker cp [CONTAINER NAME]:/app/logs/ /home/carl/obfuscation/
you can also specify the current dir with a period .

Copy to container

1
sudo docker cp /path/on/host [CONTAINER NAME]:/path/on/container

EXEC session to container

1
sudo docker exec -it 279 sh (where 279 is the start of the container ID)
1
2
sudo docker exec compose-angular-web cat /etc/nginx/nginx.conf
sudo docker exec compose-angular-web ls /usr/share/nginx/html

Interactive processes (like a shell)

This was helpful when debugging pgDash when the container would not start and we needed to run in interactive mode, run from bash their command to start the app and then cat out the logs.

1
2
3
docker run -it –entrypoint /bin/bash pgdash_image    (note the – – before entrypoint)
/usr/sbin/pgdashd
cat /var/log/pgdash/pgdash.log

<* https://docs.docker.com/engine/reference/run/#operator-exclusive-options>

Network

1
2
3
sudo docker network connect golden-gate [CONTAINER NAME]
sudo docker network connect bridge [CONTAINER NAME]
sudo docker network disconnect golden-gate [CONTAINER NAME]

Inspect

1
2
3
4
docker network inspect [NETWORK NAME]
docker container inspect [CONTAINER NAME]
docker inspect -f “{{ .NetworkSettings.Networks.nat.IPAddress }}” [CONTAINER NAME]
~ shows its IP to get around the WinNAT plow

Volumes

1
2
3
docker volume ls
docker volume inspect my-vol
sudo docker volume prune ~ deletes volumes not in use

Setup Kubernetes Local-machine

Minikube is the recommended method for creating a local, single-node Kubernetes cluster for development and testing. Setup is completely automated and doesn’t require a cloud provider account.

Windows 10

  1. Enable Hyper V in your BIOS and turn on the windows feature ‘Hyper-V’
    1. Also see: Install Hyper-V on Windows 10
  2. Install chocolatey package manager
  3. Install kubectl
1
2
3
4
5
6
choco install kubernetes-cli
kubectl version
cd C:\users\Carl (where Carl is your home dir)
mkdir .kube
cd .kube
New-Item config -type file
  1. Install
    1. minikube
  2. At the time of writing this the latest version was v0.28.0

TBC \ :D /

References

PostgreSQL on a Linux VM

You will need to install Ubuntu on your virtual host, I used VMware Workstation Player and ubuntu-17.10.1-server-amd64.iso

I called my instance ‘Ubuntu PSQL’ and used carl/carl username and password for authentication. Once the VM is running you can install postgresql:

1
2
3
4
5
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install postgresql postgresql-contrib
sudo apt-get install openssh-server
ip addr show

You can then putty to the server, this is easier as you can paste commands.

To check the PostgreSQL server is running you can query it with psql –version

All of my examples below were on version 9.6

1
2
3
4
5
6
7
8
9
10
11
12
#Check the PSQL version
psql --version

#Check status
service --status-all
sudo pg_lsclusters

#Other service commands
sudo service postgresql status
sudo service postgresql restart
sudo service postgresql stop
sudo service postgresql start

Configuration updates

The following configuration updates will be needed, I used nano to edit the files but you can use any other editor such as vi

pg_hba.conf

Set the METHOD to ‘trust’ under ‘# Database administrative login by Unix domain socket’

1
2
3
4
sudo nano /etc/postgresql/9.6/main/pg_hba.conf

# Database administrative login by Unix domain socket
local all postgres trust

You can also restrict access by IP or IP range:

Example:

1
2
192.168.0.254/32 = ip address of 192.168.0.254
192.168.0.1/24 = range of ip's from 192.168.0.1 to 192.168.0.1.255

Also see Subnet mask

Allow all connections:

1
2
3
4
5
-- allow the lot
host all all all md5

-- allow by username
host all USERNAME 0.0.0.0/0 md5

postgresql.conf

Set the the following:

1
2
3
4
5
6
sudo nano /etc/postgresql/9.6/main/postgresql.conf

listen_addresses = '*'
log_statement = 'all'
log_hostname = on
log_line_prefix = '%m [%p] [%r] %q%u@%d '

Update password

You can update the root password

1
2
3
4
5
6
7
8
9
10
#set password
sudo -u postgres psql postgres
\password postgres
[newpassword]
\q
#test user/pass
(Requires pg_hba.conf change to trust)

psql -U postgres -W
[newpassword]

Tail logs

1
2
tail -f /var/log/postgresql/postgresql-9.6-main.log
CRTL C ~ to quit

Backup & Restore

See putty if you need to copy a dump file to the server from your windows PC.

1
2
3
4
5
6
7
8
//Create the dump file
sudo -Su postgres PGCLUSTER="9.6/main" pg_dump -v -Fc productiondbname -f /tmp/production20180503.dump

//Create a new database
sudo -u postgres psql -c "CREATE DATABASE production20180503 OWNER = postgres TABLESPACE pg_default;"

//Restore
sudo -u postgres pg_restore --dbname "production20180503" --verbose /tmp/production20180503.dump

References

NLog

Creates and manages instances of Logger objects.

Works well with .net core. Install from nuget NLog 4.5.11

Configuration

Set the config file NLog.config

1
2
3
4
5
6
7
8
9
10
11
12
13
<targets>
<target name="logfile"
xsi:type="File"
archiveNumbering="Date"
archiveEvery="Day"
maxArchiveFiles="50"
bufferSize="102400"
fileName="appname.log" />
</targets>

<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>

Example use

1
2
3
4
5
6
7
var logger = NLog.LogManager.GetCurrentClassLogger();

logger.Debug("Some debug");
logger.Debug("Some other debug");
logger.Info("Some info.");

~ this will then create `appname.log` in your executing bin directory.

References