預習:
使用到的docker命令:
docker images 顯示本地有的鏡像
docker pull +鏡像名稱 從docker hub上面拉取鏡像
docker run
--name 定義容器的名稱
-d 讓docker容器在后台運行到
-a 查看已經創建的容器
-s 查看啟動的容器
docker start docker_name 啟動名稱為docker_name的容器
docker stop docker_name 關閉名稱為docker_name的容器
docker rm docker_name 刪除名稱為docker_name的容器
docker rmi docker_name 刪除名稱為docker_name的鏡像
docker rename old_name new_name 給容器重命名
操作:
[root@localhost ~]# docker pull mysql 拉取鏡像
[root@localhost ~]# docker images|grep mysql 查看鏡像
docker.io/mysql latest 6a834f03bd02 2 weeks ago 484 MB
[root@localhost ~]# docker run -e MYSQL_ROOT_PASSWORD=123456 --name mysql -it mysql 啟動一個容器
[root@localhost ~]# docker ps -s 查看正在運行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
dc1c39266b16 mysql "docker-entrypoint..." 3 days ago Up 2 hours 3306/tcp, 33060/tcp mysql 119 B (virtual 484 MB)
連接MySQL數據庫
[root@localhost ~]# docker exec -it dc1c39266b16 bash
root@dc1c39266b16:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.12 MySQL Community Server - GPL
Copyright (c) 2000, 2018, 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>
已經連接上數據庫了。
