Docker常用命令大全🔥🔥🔥

532 阅读8分钟

Docker 是一个用于开发、发布和运行应用程序的平台。在这篇文章中,我们将讨论我们项目中经常使用的一些重要 Docker 命令。

docker pull

此命令有助于从 docker hub 拉取镜像。如果我们不指定要拉取的tag,则会拉取最新的docker镜像。

docker 拉取镜像:tag

docker pull mysql:8.0.33
8.0.33: Pulling from library/mysql
e2c03c89dcad: Pull complete 
68eb43837bf8: Pull complete 
796892ddf5ac: Pull complete 
6bca45eb31e1: Pull complete 
ebb53bc0dcca: Pull complete 
2e2c6bdc7a40: Pull complete 
6f27b5c76970: Pull complete 
438533a24810: Pull complete 
e5bdf19985e0: Pull complete 
667fa148337b: Pull complete 
5baa702110e4: Pull complete 
Digest: sha256:232936eb036d444045da2b87a90d48241c60b68b376caf509051cb6cffea6fdc
Status: Downloaded newer image for mysql:8.0.33
docker.io/library/mysql:8.0.33

docker images

显示下载到本地机器的docker镜像。

docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        8.0.33    041315a16183   11 days ago   565MB

docker run

从 docker 镜像运行容器。

docker run -p6000:3306 --name some-mysql-new -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:8.0.33
0dacb1101ad336475f7ebcbc5e21cc9fb894f2bfa4c0a805f65d3ef1f39bf7e4

-p(主机端口):(容器端口) - 主机端口绑定到容器端口,以便可以访问。
-d :以分离模式运行容器。

docker ps

显示正在运行的容器列表。

docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS                               NAMES
0dacb1101ad3   mysql:8.0.33   "docker-entrypoint.s…"   About a minute ago   Up About a minute   33060/tcp, 0.0.0.0:6000->3306/tcp   some-mysql-new

docker stop

停止正在运行的 docker 容器。需要传递容器id。

docker stop 0dacb1101ad3

docker start

启动 docker 容器。需要传递容器id。

docker start 0dacb1101ad3

docker 日志

显示 Docker 容器的日志。需要传递容器id。

docker logs 0dacb1101ad3
2023-07-16 14:02:16+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
2023-07-16 14:02:16+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2023-07-16 14:02:16+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
2023-07-16 14:02:16+00:00 [Note] [Entrypoint]: Initializing database files
2023-07-16T14:02:16.555166Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-07-16T14:02:16.555434Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.33) initializing of server in progress as process 81
2023-07-16T14:02:16.562164Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-07-16T14:02:17.077951Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-07-16T14:02:18.844888Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2023-07-16 14:02:23+00:00 [Note] [Entrypoint]: Database files initialized
2023-07-16 14:02:23+00:00 [Note] [Entrypoint]: Starting temporary server
2023-07-16T14:02:23.967805Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-07-16T14:02:23.969395Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.33) starting as process 123
2023-07-16T14:02:23.984575Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-07-16T14:02:24.258224Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-07-16T14:02:24.669973Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-07-16T14:02:24.670204Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-07-16T14:02:24.673431Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-07-16T14:02:24.732237Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
2023-07-16T14:02:24.733339Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.33'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
2023-07-16 14:02:24+00:00 [Note] [Entrypoint]: Temporary server started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.

2023-07-16 14:02:29+00:00 [Note] [Entrypoint]: Stopping temporary server
2023-07-16T14:02:29.418853Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.33).
2023-07-16T14:02:31.320144Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.33)  MySQL Community Server - GPL.
2023-07-16 14:02:31+00:00 [Note] [Entrypoint]: Temporary server stopped

2023-07-16 14:02:31+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.

2023-07-16T14:02:31.685058Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-07-16T14:02:31.686585Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.33) starting as process 1
2023-07-16T14:02:31.694428Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-07-16T14:02:31.882615Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-07-16T14:02:32.331478Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-07-16T14:02:32.331559Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-07-16T14:02:32.334663Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-07-16T14:02:32.367684Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2023-07-16T14:02:32.367707Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.33'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
2023-07-16T14:07:21.221136Z 0 [System] [MY-013172] [Server] Received SHUTDOWN from user <via user signal>. Shutting down mysqld (Version: 8.0.33).
2023-07-16T14:07:22.110930Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.33)  MySQL Community Server - GPL.
2023-07-16 14:07:32+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
2023-07-16 14:07:32+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2023-07-16 14:07:32+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
2023-07-16T14:07:33.176975Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2023-07-16T14:07:33.178454Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.33) starting as process 1
2023-07-16T14:07:33.185757Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-07-16T14:07:33.376968Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-07-16T14:07:33.689001Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2023-07-16T14:07:33.689518Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2023-07-16T14:07:33.693007Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2023-07-16T14:07:33.743641Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2023-07-16T14:07:33.744479Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.33'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

docker exec

进入容器的终端。

docker exec -it 0dacb1101ad3 /bin/bash
bash-4.4# ls
bin  boot  dev  docker-entrypoint-initdb.d  entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
bash-4.4# exit
exit

docker image rm

删除镜像。命令中应提及图像名称和标签。

docker image rm mysql:8.0.33 -f    
Untagged: mysql:8.0.33
Untagged: mysql@sha256:232936eb036d444045da2b87a90d48241c60b68b376caf509051cb6cffea6fdc
Deleted: sha256:041315a161837f8bb87361e13390abda7159b98aeedee5e6152a0bb7a9b45f27