Tag Archives: Docker

My Docker Cheat Sheet

Run a new container

docker run <container id or name>

Start a stopped container

docker start <container id or name>

Stop a container

docker stop <container id or name>

Kill a container

docker kill <container id or name>

Restart a container

docker restart <container id or name>

List all running containers

docker ps

List all containers

docker ps -a

List with formatted output (here only show name and status of containers)

docker ps --format '{{.Names}} {{.Status}}'

Inspect a container

docker inspect <container id or name>

List all ports a container uses

docker container port <container id or name>

Open Bash in container (‘exit’ to close)

docker exec -it <container id or name> bash

Watch STDOUT from container (‘-f’ for continued logging)

docker logs <container id or name>

Search containers in registry (example: “MySQL” gives all available MYSQL-containers)

docker search <search word>

Pull container from registry

docker pull <name>

MySQL Docker helper script

Here is a little script I made to help me start/stop and remove a MySQL docker instance. The script also points to an init file (docker-entrypoint-initdb.d) for the database where you can put all the creations of init objects needed, eg. CREATE TABLE…, INSERT INTO…, and so on

Usage:

>my-docker-db-script start # Starts MySQL docker instance. Creates it if it does not exist
>my-docker-db-script stop # Stops the MySQL docker instance
>my-docker-db-script remove # Removes the MySQL instance

Bash script:

#!/bin/bash

name="my-mysql-db"
command="$1"

function start() {
 state=$(2>/dev/null docker inspect $name --format '{{ .State.Status }}' || echo "")

 if [[ "$state" == "running" ]]; then
  echo "Databse already running!"
  exit 1
 fi
 
 if [[ "$state" == "exited" ]]; then
  echo "Database already exists. Starting..."
  docker start $name && echo "  ... database started"
 else
  echo "No database exists. Creating..."
  docker run -d -p 3306:3306 --name $name \
   -e MYSQL_ROOT_PASSWORD=root \
   -e MYSQL_DATABASE=my_db \
   -e MYSQL_USERNAME=myusername \
   -e MYSQL_PASSWORD=myuserpassword \
   -v $(pwd)/my-mysql-db/mysql-docker-init:/docker-entrypoint-initdb.d \
   -d mysql:5.6.51 && echo "  ... database created"
 fi

}

function stop() {
 state=$(2>/dev/null docker inspect $name --format '{{ .State.Status }}' || echo "")

 if [[ "$state" == "" ]]; then
  echo "Database does not exist!"
  exit 1
 fi

 echo "Stopping database ..."
 docker stop $name && echo "  ... database stopped"
}

function remove() {
 state=$(2>/dev/null docker inspect $name --format '{{ .State.Status }}' || echo "")

 if [[ "$state" == "" ]]; then
  echo "Database does not exist!"
  exit 1
 fi

 echo "Removing database"
 if [[ "$state" == "running" ]]; then
  docker stop $name && echo "  ...database stopped"
 fi

 docker rm $name && echo "   ...database removed"
}

case "$command" in
 start)
  start
  ;;
 stop)
  stop
  ;;
 remove)
  remove
  ;;
 *)
  echo "Invalid command: ${command}. Usage $0 (start|stop|remove)"
  exit 1
esac 

Tested on MySQL v5.6.51, OSX 10.15.7 and Docker Desktop 4.3.2