安装docker, 准备一个node.js项目,项目中包含数据库配置。
一、将node.js项目创建为image
在项目中创建.dockerignore文件和dockerfile文件(https://github.com/hanxiaoer1992/docker_nodejs_cmd)
cd 项目文件夹下(cd ....../docker_nodejs_cmd)
docker image build -t dockertest:0.0.1 .
二、拉取mysql
docker pull mysql
三、构建容器互联网络
docker network create -d bridge network-name
四、运行image: mysql, dockertest:0.0.1
docker run --rm --name mysql --network network-name -e MYSQL_ROOT_PASSWORD=mysqlPassword -d -it -p 3307:3306 mysql
(容器停止后自动删除,不会保留mysql数据)
进入mysql命令行:docker exec -it mysql /bin/bash
mysql -uroot -pmysqlPassword
因‘root‘@‘%‘已在数据库中,alter user ‘root‘@‘%‘ identified with mysql_native_password by ‘mysqlPassword‘;
(如不更改会报Client does not support authentication protocol requested by server; consider upgrading MySQL client, 因mysql升级后密码机制改变)
grant all privileges on *.* to ‘root‘@‘%‘ with grant option;
flush privileges;
创建数据库:
create database dockerTest;
use dockerTest;
create table test(id int(11) auto_increment, name varchar(40), primary key (id) )default charset=utf8;
insert into test (name) values (‘docker-test‘);
exit退出,或另外打开一个命令行
docker container run --rm --name testplay -it -p 1000:3000 --network network-name --link mysql:mysql -e DATABASE_HOST=mysql -e DB_USER=root dockertest:0.0.1 /bin/bash
npm start
原文地址:https://www.cnblogs.com/hanxiaoer/p/10526366.html