Centos7系统Docker环境下Mysql部署
1.拉取mysql镜像
[root@localhost ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
27833a3ba0a5: Already exists
864c283b3c4b: Pull complete
cea281b2278b: Pull complete
8f856c14f5af: Pull complete
9c4f38c23b6f: Pull complete
1b810e1751b3: Pull complete
5479aaef3d30: Pull complete
9667974ee097: Pull complete
4ebb5e7ad6ac: Pull complete
021bd5074e22: Pull complete
cce70737c123: Pull complete
544ff12e028f: Pull complete
Digest: sha256:2e436df90deb54e4b76b58507a7e11d880e2308506294834403146ef59aa9847
Status: Downloaded newer image for mysql:latest
[root@localhost ~]#
2.测试一下mysql
[root@localhost ~]# docker run --name test.mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
73ade7686685bf27fc70c713b27ec27a7d08ad7654134c6a5fc91672de2901cd
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
73ade7686685 mysql "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 3306/tcp, 33060/tcp test.mysql
[root@localhost ~]# docker exec -it test.mysql \
> mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.16 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> exit;
Bye
[root@localhost ~]#
3.挂载数据卷
#创建数据卷
[root@localhost ~]# docker volume create --name test.db
test.db
#查看数据卷信息
[root@localhost ~]# docker volume inspect test.db
[
{
"CreatedAt": "2019-04-26T18:14:25+08:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/test.db/_data",
"Name": "test.db",
"Options": {},
"Scope": "local"
}
]
#挂载数据卷启动MySql实例,需要把之前mysql容器删了
[root@localhost ~]# docker run --name test.mysql -v test.db:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql
49eac84b374a17cd1d8f0e604c858e195c51953eaf1a15b1687bfbde934bf66e
[root@localhost ~]#
4.安装Git并Clone示例项目
#安装Git
[root@localhost ~]# yum install git
#查看Git版本
[root@localhost ~]# git --version
git version 1.8.3.1
[root@localhost ~]# cd Documents
#拉取示例项目
[root@localhost Documents]# git clone https://github.com/yanshengjie/Docker.NetCore.MySql.git
Cloning into 'Docker.NetCore.MySql'...
remote: Enumerating objects: 182, done.
remote: Total 182 (delta 0), reused 0 (delta 0), pack-reused 182
Receiving objects: 100% (182/182), 539.58 KiB | 254.00 KiB/s, done.
Resolving deltas: 100% (60/60), done.
[root@localhost Documents]# ll
total 8
drwxr-xr-x. 9 root root 4096 Apr 26 21:38 Docker.NetCore.MySql
drwxr-xr-x. 3 root root 4096 Apr 25 22:49 DockerWeb
drwxr-xr-x. 9 root root 243 Apr 23 04:45 testapp
[root@localhost Documents]# cd Docker.NetCore.MySql/
#查看目录,可以看到已经存在Dcokerfile文件了
[root@localhost Docker.NetCore.MySql]# ll
total 48
-rw-r--r--. 1 root root 168 Apr 26 21:38 appsettings.Development.json
-rw-r--r--. 1 root root 202 Apr 26 21:38 appsettings.json
-rw-r--r--. 1 root root 604 Apr 26 21:38 bundleconfig.json
drwxr-xr-x. 2 root root 60 Apr 26 21:38 Controllers
drwxr-xr-x. 2 root root 55 Apr 26 21:38 Data
-rw-r--r--. 1 root root 463 Apr 26 21:38 docker-compose.yml
-rw-r--r--. 1 root root 148 Apr 26 21:38 Dockerfile
-rw-r--r--. 1 root root 566 Apr 26 21:38 Docker.NetCore.MySql.csproj
-rw-r--r--. 1 root root 1063 Apr 26 21:38 LICENSE
drwxr-xr-x. 2 root root 49 Apr 26 21:38 Models
-rw-r--r--. 1 root root 802 Apr 26 21:38 Program.cs
-rw-r--r--. 1 root root 86 Apr 26 21:38 proxy.conf
-rw-r--r--. 1 root root 845 Apr 26 21:38 README.md
-rw-r--r--. 1 root root 1148 Apr 26 21:38 ScaffoldingReadMe.txt
-rw-r--r--. 1 root root 1513 Apr 26 21:38 Startup.cs
drwxr-xr-x. 4 root root 84 Apr 26 21:38 Views
drwxr-xr-x. 6 root root 71 Apr 26 21:38 wwwroot
#创建镜像
[root@localhost Docker.NetCore.MySql]# docker build -t docker.netcore.mysql .
Sending build context to Docker daemon 3.051MB
Step 1/7 : FROM microsoft/dotnet:latest
---> e268893be733
Step 2/7 : WORKDIR /app
---> Using cache
---> 636e51b35e60
Step 3/7 : COPY . /app
---> 30e232246309
Step 4/7 : RUN dotnet restore
---> Running in 5171b89ab157
Restore completed in 1.3 min for /app/Docker.NetCore.MySql.csproj.
Restore completed in 8.62 sec for /app/Docker.NetCore.MySql.csproj.
Removing intermediate container 5171b89ab157
---> 5dd5560c1f48
Step 5/7 : EXPOSE 5000
---> Running in 63894ab4403d
Removing intermediate container 63894ab4403d
---> 128497af43a1
Step 6/7 : ENV ASPNETCORE_URLS http://*:5000
---> Running in 58ba200cf816
Removing intermediate container 58ba200cf816
---> ecc95d23f64d
Step 7/7 : ENTRYPOINT ["dotnet","run"]
---> Running in ddafe6aab0c0
Removing intermediate container ddafe6aab0c0
---> 6fd390a6de79
Successfully built 6fd390a6de79
Successfully tagged docker.netcore.mysql:latest
#创建容器,将容器命名为docker.netcore.mysql,并使用--link参数与之前创建的test.mysql容器建立连接
[root@localhost Docker.NetCore.MySql]# docker run --name docker.netcore.mysql --link test.mysql:db -d -p 5000:5000 docker.netcore.mysql
d6f1174a3c18c9ceb49bcdb3ba68f9089dbf18a2532ffac824600eaaa2807476
#测试是否成功
[root@localhost Docker.NetCore.MySql]# curl http://localhost:5000/api/products
[{"productId":1,"name":"iphone 6","price":5000,"stockQty":10},{"productId":2,"name":"iphone 7","price":6000,"stockQty":10},{"productId":3,"name":"iphone 7 plus","price":7000,"stockQty":10},{"productId":4,"name":"iphone x","price":8000,"stockQty":10}]
[root@localhost Docker.NetCore.MySql]#
5.使用docker-compose多容器部署示例项目
上述的示例代码目录里面已经存在docker-compose.yml和proxy.conf,可以使用cat指令查看下。
#我改了一些容器名称,所以跟刚下下来的示例代码不太一样。
[root@localhost Docker.NetCore.MySql]# cat docker-compose.yml
version: '2'
services:
db:
container_name: test.db
image: mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
- ./mysql:/var/lib/mysql
web:
container_name: docker.web
build: .
depends_on:
- db
links:
- db
reverse-proxy:
container_name: docker.proxy
image: nginx
depends_on:
- web
ports:
- "9090:8080"
volumes:
- ./proxy.conf:/etc/nginx/conf.d/default.conf
[root@localhost Docker.NetCore.MySql]# cat proxy.conf
server {
listen 8080;
location / {
proxy_pass http://web:5000;
}
}
两个配置文件已存在,所以我们可以直接Compose,在此之前先清空一些之前测试的容器以免冲突。
#停止所有容器
[root@localhost Docker.NetCore.MySql]# docker stop $(docker ps -qa)
d6f1174a3c18
49eac84b374a
#删除所有容器
[root@localhost Docker.NetCore.MySql]# docker rm $(docker ps -qa)
d6f1174a3c18
49eac84b374a
#Compose
[root@localhost Docker.NetCore.MySql]# docker-compose up -d
Creating test.db ... done
Creating docker.web ... done
Creating docker.proxy ... done
#查看创建的容器
[root@localhost Docker.NetCore.MySql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b719e4ce90ba nginx "nginx -g 'daemon of…" 18 seconds ago Up 15 seconds 80/tcp, 0.0.0.0:9090->8080/tcp docker.proxy
7e4fc2220d86 dockernetcoremysql_web "dotnet run" 23 seconds ago Up 18 seconds 5000/tcp docker.web
5dbe9b8cefc0 mysql "docker-entrypoint.s…" 23 seconds ago Up 22 seconds 3306/tcp, 33060/tcp test.db
[root@localhost Docker.NetCore.MySql]# curl http://localhost:9090/api/products
[{"productId":1,"name":"iphone 6","price":5000.0000000000000000000000000,"stockQty":10},{"productId":2,"name":"iphone 7","price":6000.0000000000000000000000000,"stockQty":10},{"productId":3,"name":"iphone 7 plus","price":7000.0000000000000000000000000,"stockQty":10},{"productId":4,"name":"iphone x","price":8000.000000000000000000000000,"stockQty":10}]
[root@localhost Docker.NetCore.MySql]#
最后我们可以验证一下数据库是否存在
[root@localhost Docker.NetCore.MySql]# docker exec -it test.db mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.16 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> show databases;
+--------------------+
| Database |
+--------------------+
| MySqlDbContext |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.04 sec)
mysql>