• docker 已安装完成
  1. 拉取MySQL镜像

    1
    docker pull mysql:latest
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    latest: Pulling from library/mysql
    32c1bf40aba1: Pull complete
    3ac22f3a638d: Pull complete
    b1e7273ed05e: Pull complete
    20be45a0c6ab: Pull complete
    410a229693ff: Pull complete
    1ce71e3a9b88: Pull complete
    c93c823af05b: Pull complete
    c6752c4d09c7: Pull complete
    d7f2cfe3efcb: Pull complete
    916f32cb0394: Pull complete
    0d62a5f9a14f: Pull complete
    Digest: sha256:ce2ae3bd3e9f001435c4671cf073d1d5ae55d138b16927268474fc54ba09ed79
    Status: Downloaded newer image for mysql:latest
    docker.io/library/mysql:latest

    出现类似上述打印表示镜像拉取成功

  2. 查看本地镜像

    1
    docker images
    1
    2
    REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
    mysql latest 7b94cda7ffc7 11 days ago 446MB
  3. 运行容器

    1
    docker run -itd --name mysql-icity -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456a? mysql
  • -p 3306:3306 容器内的端口映射为容器外的端口
  • –name mysql-icity 自定义容器的名字
  • -e MYSQL_ROOT_PASSWORD=123456a? root用户的数据库密码
  • mysql为镜像名

如果打印类似5ae30c40e045ca2164e00e3c0fe04bc9c64451a00765728b30c4c0ca916a65bb内容说明运行成功
4. 查看运行结果

1
docker ps
1
2
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS                               NAMES
5ae30c40e045 mysql "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql-icity
  1. 进入容器
    1
    docker exec -it mysql-icity bash
  • mysql-icity为容器名
  • bash 进入容器的CLI模式,就是场景的进入到容器内
  1. 停止、重启容器
    1
    2
    docker stop mysql-icity
    docker restart mysql-icity
    docker kill是直接关闭容器,stop则默认10秒由容器自行保存状态后关闭