Skip to content

Docker Commands

Login into docker container

docker exec -it container_id /bin/bash

Clearing Space

Probably better to use the docker commands rather than using piping in these commands {.is-warning}

https://lebkowski.name/docker-volumes/

# run as root

#!/bin/bash

# remove exited containers:
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v

# remove unused images:
docker images --no-trunc | grep '<none>' | awk '{ print $3 }' | xargs -r docker rmi

# remove unused volumes:
find '/var/lib/docker/volumes/' -mindepth 1 -maxdepth 1 -type d | grep -vFf <(
  docker ps -aq | xargs docker inspect | jq -r '.[] | .Mounts | .[] | .Name | select(.)'
) | xargs -r rm -fr

Docker Compose Update images

https://stackoverflow.com/questions/49316462/how-to-update-existing-images-with-docker-compose

docker-compose up --force-recreate --build -d
docker image prune -f

or

docker-compose pull
docker-compose restart

Watchtower -- automated Docker image updates

https://containrrr.dev/watchtower/

This will check for new docker images. If found, it will restart the container with the new image.

Check https://git.chriswong.org/clw/watchtower_docker

Lazydocker - terminal based docker viewer

https://github.com/jesseduffield/lazydocker

Linux

curl https://raw.githubusercontent.com/jesseduffield/lazydocker/master/scripts/install_update_linux.sh | bash

Go

go get github.com/jesseduffield/lazydocker

Portainer

https://www.portainer.io/

Portainer is a universal container management tool. It works with Kubernetes, Docker, Docker Swarm and Azure ACI and allows you to manage containers without needing to know platform-specific code.

Docker network (for use with Nginx proxy manager)

This creates the docker network so the services can use the same network

docker network create npm

On the containers, we need to set the network to this external network npm we just created.

For each docker-compose.yml, add

networks:
  default:
    external:
      name: npm

Conflicting ports

If we run services that use the same port, we need to keep those containers on different networks.

In docker-compose.yml, we can define a network like so

networks:
  default:
    name: statping_chriswong

On another docker-compose.yml, we can define

networks:
  default:
    name: statping

On the Nginx reverse proxy, we need to add both of these networks so they can be accessed

version: '3'
services:
  app:
    image: 'jc21/nginx-proxy-manager:latest'
    networks:
      - statping
      - statping_chriswong
      - default
    ports:
      - '80:80'
      - '81:81'
      - '443:443'
    environment:
      DB_MYSQL_HOST: "db"
      DB_MYSQL_PORT: 3306
      DB_MYSQL_USER: "npm"
      DB_MYSQL_PASSWORD: "npm"
      DB_MYSQL_NAME: "npm"
    volumes:
      - ./data:/data
      - ./letsencrypt:/etc/letsencrypt
  db:
    image: 'jc21/mariadb-aria:latest'
    environment:
      MYSQL_ROOT_PASSWORD: 'npm'
      MYSQL_DATABASE: 'npm'
      MYSQL_USER: 'npm'
      MYSQL_PASSWORD: 'npm'
    volumes:
      - ./data/mysql:/var/lib/mysql

networks:
  default:
    external:
      name: npm
  statping:
    external:
      name: statping
  statping_chriswong:
    external:
      name: statping_chriswong

Get bash

docker exec -it <container-name> bash