1. 程式人生 > >docker+mysql(8.0.15)+node.js(hapi.js)構建容器(命令行)

docker+mysql(8.0.15)+node.js(hapi.js)構建容器(命令行)

net ... https pass 互聯網 -- github container ont

安裝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

docker+mysql(8.0.15)+node.js(hapi.js)構建容器(命令行)