记录一些 Docker 常用命令。

环境信息

docker -v
docker info

镜像相关命令

docker search <keyword>
docker pull <image-name>[:<tag>]
docker images
docker rmi <image-name>
docker inspect [--type image] <image-name>

容器相关命令

docker run                              \
    [--rm]                              \
    [-d|-it|-itd]                       \
    [--name <container-name>]           \
    [-p <host-port>:<container-port>]   \
    [-v <host-dir>:<container-dir>]     \
    [-e <env-variable>[=<value>]]       \
    <image-name>[:<tag>]                \
    [<cmd>] [<args>]
docker ps [-a]
docker stop <container-name>
docker start <container-name>
docker exec -it <container-name> /bin/sh
docker logs <container-name>
docker inspect [--type container] <container-name>
docker rm <container-name>

常用镜像部署

MySQL

https://hub.docker.com/_/mysql

docker run -d --name <container-name> -e MYSQL_ROOT_PASSWORD=<password> mysql:8.0
docker run -d --name <container-name> -e MYSQL_ALLOW_EMPTY_PASSWORD=1 mysql:8.0

docker exec -it <container-name> mysql

PostgreSQL

https://hub.docker.com/_/postgres

docker run -d --name <container-name> -e POSTGRES_PASSWORD=<password> postgres:17

docker exec -it <container-name> psql -U postgres