Linux & Docker Commands Reference & Troubleshooting

We’ve published these commands for our developers for reference purposes. They are publicly visible as it might be helpful for other developers as well:

Docker

#stop all containers:
docker stop $(docker ps -a -q)

# Restart docker service
sudo systemctl restart docker.service

Recreate all container
docker-compose up --build --force-recreate --no-deps

#stop all containers by force
docker kill $(docker ps -q)

#remove all containers
docker rm $(docker ps -a -q)

#remove all docker images
docker rmi $(docker images -q)

#purge the rest
docker system prune --all --force --volumes

#Find IP of a container
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' [container_name]

#Show docker logs
docker-compose logs

#Show docker process
docker-compose ps

Execute mysqldump in container

docker exec [container_name] sh -c 'exec mysqldump --all-databases -uroot -p[password]' > /home/user/all-databases.sql

Error response: Could not kill running container runc did not terminate successfully: container_linux.go:392: init process caused “permission denied”

This can happen if apparmor prevents stopping the docker container.
One solution is to uninstall apparmor and restarting the docker service:

sudo apt-get purge --auto-remove apparmor
sudo service docker restart
docker system prune --all --volumes

sudo systemctl restart docker
sudo systemctl daemon-reload

Error starting userland proxy: listen tcp 172.200.0.1:443: bind: address already in use

Find PID and kill it:

sudo netstat -nlpt |grep 3306

sudo kill [pid]

 

Network

Number of open connections per ip
netstat -ntu | awk ‘{print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n

Show all connected ip addresses to a certain port
netstat -an |grep 443
netstat -an |grep 80

Count connections per IP
netstat -an | grep 80 | wc -l
netstat -an | grep 443 | wc -l

Firewall iptables

Firewall Block IP Adress
sudo iptables -A INPUT -s [IP ADDRESS] -j DROP

Allow access from a specific IP address
sudo iptables -A INPUT -s 91.52.78.249 -p tcp –destination-port 3306 -j ACCEPT

Unban IP addresses blocked by fail2ban

iptables -D [chain] [line] z.B.
iptables -D f2b-wordpress-hard 1

Firewall Rules
See: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-with-ufw-on-ubuntu-18-04

  • sudo ufw default deny incoming
  • sudo ufw default allow outgoing
  • sudo ufw allow ssh
  • sudo ufw allow 1000
  • sudo ufw allow http
  • sudo ufw allow 443
  • sudo ufw allow from 91.52.78.249 to any port 3306 (Local IP address)
  • sudo ufw allow from 52.45.200.92 to any port 3306 (Amazon DBS)
  • sudo ufw deny from 157.245.247.135

MySQL

Change MySQL password authentication module tomysql_native_password.

Needed for upgrading from 7.X to mysql 8.x

ALTER USER ‘user’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password1’;

Debug xDebug:-)

If your code editor does not connect to xDebug create a file `test.php` in your app folder with some code and add a breakpoint to it. Then run the command below to test it:

PHP_IDE_CONFIG="serverName=tests" php -dxdebug.remote_autostart=1 -dxdebug.remote_enable=1 -dxdebug.default_enable=1 -dxdebug.remote_connect_back=1 -dxdebug.remote_port=9001 -dxdebug.idekey=PHPSTORM -dxdebug.remote_host=172.200.0.1 test.php

Login reloads the login prompt

If you try to login to KDE and the login form reloads after entering correct credentials try the commands below:

systemctl stop gdm.service
systemctl restart gdm.service
sudo dpkg-reconfigure gdm3
reboot

Author: Rene Hermenau

I'm René Hermenau, founder of WP STAGING. I've been building WordPress infrastructure software since 2013 and writing code on GitHub since 2011. My repos live at github.com/rene-hermenau. WP STAGING started as a small developer project solving the same problem I kept hitting on client work: there was no fast, safe way to clone a WordPress site for staging or migration without breaking serialized data, file paths, or media references. Today we are a team of more than 10 people. The free plugin runs on hundreds of thousands of WordPress installations, and the Pro version powers backup, migration, and staging workflows for agencies, hosting platforms, and ecommerce stores. I'm still hands-on with the codebase and technical architecture. Our releases are built as a team, but many of the core architectural decisions are ones I helped design, test, and evolve over the years: how we handle large database exports, how we keep memory usage flat on multi-GB sites, and how we make migrations atomic against partially written tables. "When you touch code, leave it 10% better than before and write a test." If you're stuck on a WP STAGING question, the docs are at wp-staging.com/docs. If you hit a bug, file it on GitHub at github.com/wp-staging. Our team reads everything that lands there.