Docker 创建MySQL容器:
1.运行“docker pull mysql”获取mysql镜像
[root@centos7 mnt]# docker pull mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
27833a3ba0a5: Pull complete
864c283b3c4b: Pull complete
cea281b2278b: Pull complete
8f856c14f5af: Pull complete
9c4f38c23b6f: Pull complete
1b810e1751b3: Pull complete
5479aaef3d30: Pull complete
ded8fa2e1614: Pull complete
636033ba4d2e: Pull complete
902e6010661d: Pull complete
dbe44d2bf055: Pull complete
e906385f419d: Pull complete
Digest: sha256:a7cf659a764732a27963429a87eccc8457e6d4af0ee9d5140a3b56e74986eed7
Status: Downloaded newer image for docker.io/mysql:latest
2.docker images查看本地的镜像:
IMAGE ID 为 7bb2586065cd 的即刚才pull的MySQL镜像
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/mysql latest 7bb2586065cd 3 hours ago 477 MB
centos v1.1 28967a5f4c72 14 hours ago 298 MB
3.在后台启动mysql容器(--name指定了容器的名称,方便之后进入容器的命令行,MYSQL_ROOT_PASSWORD=tianyan指定了mysql的root密码,-d表示在后台运行)
[root@centos7 mnt]# docker run --name=mysql -it -p 33061:3306 -e MYSQL_ROOT_PASSWORD=tianyan -d 7bb2586065cd
1c41541d81089e30edc39f550aeaeb74caa3f000bd95d521c2098ef0e95cd445
4.查看新启动的myslq容器
[root@centos7 mnt]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c41541d8108 7bb2586065cd "docker-entrypoint..." 4 seconds ago Up 3 seconds 33060/tcp, 0.0.0.0:33061->3306/tcp mysql
5.进入mysql容器,进入myslq
[root@centos7 mnt]# docker exec -ti mysql /bin/bash
root@1c41541d8108:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.15 MySQL Community Server - GPL
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>