A Tour of Docker CLI Commands — Part I

Photo by Steve Johnson on Unsplash

Introduction

Terminology

  • Docker
  • Docker Hub
  • images
  • containers

Setup

docker versionClient:
Cloud integration: v1.0.24
Version: 20.10.17
API version: 1.41
[...]

Pulling Images and Creating Containers

docker pull postgres
docker imagesREPOSITORY   TAG       IMAGE ID       CREATED       SIZE
postgres latest 1133a9cdc367 2 weeks ago 376MB
docker images -q1133a9cdc367
docker create postgres3fa3bc56a56f7a0bf4bb9a34ad94941f2066643cc476d3a3e2c3223784333ee0
docker psCONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
docker ps -aCONTAINER ID   IMAGE      COMMAND                  CREATED              STATUS    PORTS     NAMES3fa3bc56a56f   postgres   "docker-entrypoint.s…"   About a minute ago   Created             strange_easleydocker ps -aq3fa3bc56a56f
docker pull mysql
docker imagesREPOSITORY   TAG       IMAGE ID       CREATED        SIZE
mysql latest 38643ad93215 17 hours ago 446MB
postgres latest 1133a9cdc367 2 weeks ago 376MB
docker create --name mysql-container mysql
2e5afd84fd493a35a00d6e83a848415e0eac52382e1f088af6725198ab09e3cb
docker ps -aCONTAINER ID   IMAGE      COMMAND                  CREATED              STATUS    PORTS     NAMES2e5afd84fd49   mysql      "docker-entrypoint.s…"   About a minute ago   Created             mysql-container3fa3bc56a56f   postgres   "docker-entrypoint.s…"   15 minutes ago       Created             strange_easley

Searching for Containers

docker ps -a -f name=myCONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS    PORTS     NAMES2e5afd84fd49   mysql     "docker-entrypoint.s…"   4 minutes ago   Created             mysql-container
docker ps -a -f status=runningCONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Running Containers

docker start mysql-containermysql-containerdocker psCONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
docker container logs mysql_container2022-07-27 16:32:47+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specifiedYou need to specify one of the following:- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql23aa7eb5bb5d0e6266f649c5d3cb9f92bcb09bcd5dcd8f8d3c9aa4286ab664dd
docker psCONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                 NAMES23aa7eb5bb5d   mysql     "docker-entrypoint.s…"   28 seconds ago   Up 27 seconds   3306/tcp, 33060/tcp   some-mysql
docker run --name some-mysql [...]

Pausing, Unpausing, Restarting and Stopping Containers

docker pause some-mysqlsome-mysqldocker psCONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                   PORTS                 NAMES23aa7eb5bb5d   mysql     "docker-entrypoint.s…"   17 minutes ago   Up 21 seconds (Paused)   3306/tcp, 33060/tcp   some-mysql
docker unpause some-mysqlsome-mysqldocker psCONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS              PORTS                 NAMES
23aa7eb5bb5d mysql "docker-entrypoint.s…" 18 minutes ago Up About a minute 3306/tcp, 33060/tcp some-mysql
docker container top some-mysqlUID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD999                 3503                3478                2                   16:55               ?                   00:00:07            mysqld
docker restart some-mysqlsome-mysqldocker stop some-mysqlsome-mysqldocker psCONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
docker logs some-mysql 2>&1 | grep "SHUTDOWN"2022-07-27T16:38:44.799824Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.30).2022-07-27T16:43:34.052929Z 0 [System] [MY-013172] [Server] Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.0.30).

Removing Containers

docker container pruneWARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
2e5afd84fd493a35a00d6e83a848415e0eac52382e1f088af6725198ab09e3cb
3fa3bc56a56f7a0bf4bb9a34ad94941f2066643cc476d3a3e2c3223784333ee0
docker ps -aCONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                 NAMES
23aa7eb5bb5d mysql "docker-entrypoint.s…" 25 minutes ago Up 8 minutes 3306/tcp, 33060/tcp some-mysql
docker rm -f $(docker ps -a -q)
23aa7eb5bb5d
docker rm some-mysqlError response from daemon: You cannot remove a running container 23aa7eb5bb5d0e6266f649c5d3cb9f92bcb09bcd5dcd8f8d3c9aa4286ab664dd. Stop the container before attempting removal or force remove
  • stop the container then remove it, or
  • remove it using the force flag.

Removing Images

docker rmi mysql
docker rmi postgres
docker rmi -f mysql

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store