一.部署nodejs項目,映射端口,掛載數(shù)據(jù)卷
可以到https://hub.docker.com/去搜索node鏡像,然后下載,也可以直接通過docker pull node下載鏡像,然后用這個node鏡像啟動容器node,這樣系統(tǒng)就集成了node服務(wù)了,在這里掛載www/node目錄到容器中,并指定端口映射,運行nodejs程序,安裝npm,以及對應(yīng)的依賴,啟動node目錄下對應(yīng)的項目,然后通過瀏覽器訪問,看看是否成功
1.安裝 nodejs
#下載node鏡像
[root@localhost www]# docker pull node
Using default tag: latest
latest: Pulling from library/node
0a9573503463: Pull complete
a360c7bc21d1: Pull complete
1dc4b09d340f: Pull complete
d0daf3eb1098: Pull complete
Digest: sha256:1f937398bb207138bd26777f76d8b31b44f22d8baf6058705ad7433225c6f1aa
Status: Downloaded newer image for node:latest
docker.io/library/node:latest
#查看是否存在鏡像node
[root@localhost www]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
node latest 51bf29046591 44 hours ago 1.1GB
2.啟動node容器,映射端口,掛載數(shù)據(jù)卷
[root@localhost node]# docker run -it -d --name mynode -p 3000:3000 -v /var/www/node/:/var/www/node/ 51bf29046591 /bin/bash
#查看啟動的容器
[root@localhost node]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c28cc83c4ba3 51bf29046591 "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:3000->3000/tcp, :::3000->3000/tcp mynode
3.進入容器,查看node版本,以及安裝cnpm,安裝依賴
#進入容器
[root@localhost node]# docker exec -it c28cc83c4ba3 /bin/bash
#查看node版本:發(fā)現(xiàn)存在,說明node安裝完成,因為是使用docker安裝nodejs,所以容器中就會自動安裝node
root@c28cc83c4ba3:/var/www# node -v
v21.1.0
#查看項目文件
root@c28cc83c4ba3:/var/www# ls
app.js package.json
#查看項目需要的依賴
root@c28cc83c4ba3:/var/www# cat package.json
{
"dependencies": {
"ejs": "^2.5.6",
"express": "^4.15.3",
"socket.io": "^2.0.3",
"body-parser": "~1.17.1"
}
}
#需要安裝依賴:這里可以通過npm來下載,不過npm可能會失敗,所以使用cnpm來安裝
#要使用cnpm,就需要安裝: npm install cnpm -g --registry=https://registry.nlark.com
root@c28cc83c4ba3:/var/www# npm install cnpm -g --registry=https://registry.nlark.com
added 40 packages in 21s
28 packages are looking for funding
run `npm fund` for details
npm notice
npm notice New patch version of npm available! 10.2.0 -> 10.2.1
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.2.1
npm notice Run npm install -g npm@10.2.1 to update!
npm notice
#查看npm版本
root@c28cc83c4ba3:/var/www# npm -v
10.2.0
#查看cnpm版本
root@c28cc83c4ba3:/var/www# cnpm -v
cnpm@9.2.0 (/usr/local/lib/node_modules/cnpm/lib/parse_argv.js)
npm@9.9.0 (/usr/local/lib/node_modules/cnpm/node_modules/npm/index.js)
node@21.1.0 (/usr/local/bin/node)
npminstall@7.11.1 (/usr/local/lib/node_modules/cnpm/node_modules/npminstall/lib/index.js)
prefix=/usr/local
linux x64 4.18.0-348.el8.x86_64
registry=https://registry.npmmirror.com
#通過cnpm i 安裝依賴
root@c28cc83c4ba3:/var/www# cnpm i
? Linked 88 latest versions fallback to /var/www/node_modules/.store/node_modules
deprecate socket.io@2.5.0 ? debug@~4.1.0 Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (https://github.com/visionmedia/debug/issues/797)
Recently updated (since 2023-10-20): 3 packages (detail see file /var/www/node_modules/.recently_updates.txt)
? Run 1 script(s) in 131ms.
? Installed 4 packages on /var/www
? All packages installed (106 packages installed from npm registry, used 3s(network 3s), speed 747.79KB/s, json 88(572.5KB), tarball 1.56MB, manifests cache hit 0, etag hit 0 / miss 0)
?4.后臺運行程序,瀏覽器訪問
#后臺運行程序: nohup node app.js &, 當(dāng)然,也可以node app.js,這個命令會卡死在這兒,不會后臺運行
root@c28cc83c4ba3:/var/www# nohup node app.js &
[1] 209
root@c28cc83c4ba3:/var/www# nohup: ignoring input and appending output to 'nohup.out'
#容器內(nèi)訪問,看看是否成功,發(fā)現(xiàn)成功
root@c28cc83c4ba3:/var/www# curl 127.0.0.1:3000
首頁root@c28cc83c4ba3:/var/www#
#退出容器
root@c28cc83c4ba3:/var/www# exit
exit
#查看ip,然后通過ip在瀏覽器中訪問
[root@localhost node]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:c3:3d:27 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.6/24 brd 192.168.0.255 scope global dynamic noprefixroute ens33
valid_lft 160539sec preferred_lft 160539sec
5.修改app.js,重啟node,查看修改是否成功
因為修改node項目后,需要重啟node,故修改項目中的app.js代碼后,需要進入容器,重新啟動node才能看見修改變化
app.js修改
var express = require('express');
var app=express();
app.get('/',function(req,res){
res.send('首頁update');
})
app.get('/news',function(req,res){
res.send('首頁');
})
//docker做端口映射的時候不要指定ip
app.listen(3000);
重啟node
#進入node容器
[root@localhost node]# docker exec -it c28cc83c4ba3 /bin/bash
#查看運行的node
root@c28cc83c4ba3:/# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 4188 132 pts/0 Ss+ 14:17 0:00 /bin/bash
root 209 0.0 2.6 995844 21348 ? Sl 14:41 0:00 node app.js
root 217 0.2 0.4 4188 3508 pts/1 Ss 14:55 0:00 /bin/bash
root 224 0.0 0.5 8100 4116 pts/1 R+ 14:55 0:00 ps -aux
#停止node程序
root@c28cc83c4ba3:/# kill -9 209
#進入/var/www node項目,重新運行node程序,并后臺運行
root@c28cc83c4ba3:/# cd /var/www/
root@c28cc83c4ba3:/var/www# nohup node app.js &
[1] 242
root@c28cc83c4ba3:/var/www# nohup: ignoring input and appending output to 'nohup.out'
二.部署Mysql,遠程連接Mysql,Mysql數(shù)據(jù)持久化
可以到https://hub.docker.com/或者Docker去搜索mysql鏡像,然后下載,也可以直接通過docker pull mysql下載鏡像,然后用這個mysql鏡像啟動容器mysql,這樣系統(tǒng)就集成了mysql服務(wù)了
1.下載 mysql
#下載mysql
[root@localhost zph]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
8e0176adc18c: Pull complete
2d2c52718f65: Pull complete
d88d03ce139b: Pull complete
4a7d7f11aa1e: Pull complete
ce5949193e4c: Pull complete
f7f024dfb329: Pull complete
5fc3c840facc: Pull complete
509068e49488: Pull complete
cbc847bab598: Pull complete
942bef62a146: Pull complete
Digest: sha256:1773f3c7aa9522f0014d0ad2bbdaf597ea3b1643c64c8ccc2123c64afd8b82b1
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
[root@localhost zph]#
[root@localhost zph]#
[root@localhost zph]#
[root@localhost zph]#
#查看是否下載好了mysql鏡像:發(fā)現(xiàn)下載了mysql鏡像
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
node latest 51bf29046591 2 days ago 1.1GB
nginx latest 593aee2afb64 3 days ago 187MB
mysql latest a3b6608898d6 3 days ago 596MB
2.啟動mysql容器
#通過mysql鏡像,啟動一個mysql容器,這里需要映射端口,輸入密碼
[root@localhost zph]# docker run -it -d --name mymysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 a3b6608898d6
0f51776fd2edce3d6479a9296a25c8b9b84a35742c9b21171a0860ec4b564e2a
[root@localhost zph]#
#查看是否啟動mysql容器
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f51776fd2ed a3b6608898d6 "docker-entrypoint.s…" 14 seconds ago Up 10 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mymysql
?3.進入容器
#進入mysql容器
[root@localhost zph]# docker exec -it 0f51776fd2ed /bin/bash
#進入mysql,并輸入密碼
bash-4.4# 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.2.0 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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.
#輸入123456密碼后,能夠進入mysql數(shù)據(jù)庫,說明容器ok,然后exit退出
mysql> exit
Bye
bash-4.4# exit
exit
4.外部連接mysql
通過外部的Navicat訪問mysql容器
(1).查看電腦的ip
通過ip addr 查看ip
[root@localhost zph]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:c3:3d:27 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.6/24 brd 192.168.0.255 scope global dynamic noprefixroute ens33
valid_lft 168896sec preferred_lft 168896sec
inet6 fe80::20c:29ff:fec3:3d27/64 scope link noprefixroute
valid_lft forever preferred_lft forever
(2).通過Navicat連接mysql容器
(3).在容器中查看是否創(chuàng)建了數(shù)據(jù)庫
$#進入mysql容器
[root@localhost zph]# docker exec -it 0f51776fd2ed /bin/bash
#輸入密碼,進入mysql數(shù)據(jù)庫
bash-4.4# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.2.0 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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數(shù)據(jù)庫:發(fā)現(xiàn)存在通過Navicat創(chuàng)建的test數(shù)據(jù)庫
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.06 sec)
#進入test數(shù)據(jù)庫
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
#查看test數(shù)據(jù)庫中數(shù)據(jù)表:發(fā)現(xiàn)存在user數(shù)據(jù)表
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user |
+----------------+
1 row in set (0.01 sec)
#查看user表里面的的數(shù)據(jù):發(fā)現(xiàn)和Navicat創(chuàng)建的數(shù)據(jù)一致,說明了mysql容器沒有問題
mysql> select * from user;
+----+------+
| id | name |
+----+------+
| 1 | test |
+----+------+
1 row in set (0.00 sec)
mysql>
?5.映射端口 , 掛載配置文件目錄,掛載數(shù)據(jù)文件目錄,啟動mysql
默認數(shù)據(jù)庫的數(shù)據(jù)是放在容器里面的,這樣的話當(dāng)容器刪除會導(dǎo)致數(shù)據(jù)丟失,如果要實現(xiàn) 當(dāng)刪除容器的時候不刪除容器里面的mysql數(shù)據(jù) ,這個時候啟動容器的時候就可以把 mysql 數(shù)據(jù)掛載到外部
(1).映射端口,掛載配置文件,數(shù)據(jù)文件目錄,啟動mysql容器?
這里,需要把mysql的配置文件,以及數(shù)據(jù)文件映射到容器中的,在linux /var/www/mysql下創(chuàng)建conf.d,data文件夾,用來保存配置文件以及數(shù)據(jù)文件,這樣,當(dāng)不小心刪除了容器,再次啟動一個容器時,就可以把配置以及數(shù)據(jù)映射到容器中,避免數(shù)據(jù)的丟失?
#查看mysql
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f51776fd2ed a3b6608898d6 "docker-entrypoint.s…" 34 minutes ago Up 34 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mymysql
#刪除這個mysql容器
[root@localhost zph]# docker rm -f 0f51776fd2ed
0f51776fd2ed
#發(fā)現(xiàn)已經(jīng)刪除了容器
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#進入/var/www/,創(chuàng)建mysql/conf.d,mysql/data配置文件以及數(shù)據(jù)文件
[root@localhost zph]# cd /var/www/
[root@localhost www]# mkdir mysql
[root@localhost www]# cd mysql/
[root@localhost mysql]# ll
總用量 0
[root@localhost mysql]# mkdir conf.d
[root@localhost mysql]# mkdir data
[root@localhost mysql]# ll
總用量 0
drwxr-xr-x 2 root root 6 10月 28 07:15 conf.d
drwxr-xr-x 2 root root 6 10月 28 07:15 data
[root@localhost mysql]# pwd
/var/www/mysql
#通過鏡像啟動mysql容器,起一個名字,以及映射端口,傳遞參數(shù),并映射數(shù)據(jù)卷(映射配置文件,數(shù)據(jù)文件)
[root@localhost mysql]# docker run -it -d --name mymysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /var/www/mysql/conf.d:/etc/mysql/conf.d -v /var/www/mysql/data/:/var/lib/mysql a3b6608898d6
7b133357d04a7c457225fad99d6322b8e1226fa80884596e0a36284abca73e16
#查看是否啟動容器:發(fā)現(xiàn)啟動了
[root@localhost mysql]#
[root@localhost mysql]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7b133357d04a a3b6608898d6 "docker-entrypoint.s…" 6 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mymysql
(2).通過外網(wǎng)連接mysql,并創(chuàng)建數(shù)據(jù)庫數(shù)據(jù)表,寫入數(shù)據(jù)?
?
?(3).進入容器,查看是否存在數(shù)據(jù)庫以及數(shù)據(jù)數(shù)據(jù)
#進入mysql容器
[root@localhost mysql]# docker exec -it 7b133357d04a /bin/bash
#進入mysql數(shù)據(jù)庫
bash-4.4# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.2.0 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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 |
+--------------------+
| backend |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.18 sec)
mysql> use backend;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------+
| Tables_in_backend |
+-------------------+
| article |
| user |
+-------------------+
2 rows in set (0.00 sec)
mysql> select * from article;
+----+-------+---------+
| id | title | content |
+----+-------+---------+
| 1 | ?? | ???? |
+----+-------+---------+
1 row in set (0.01 sec)
mysql> exit
Bye
bash-4.4# exit
exit
[root@localhost mysql]# ll
總用量 4
drwxr-xr-x 2 root root 6 10月 28 07:15 conf.d
drwxr-xr-x 8 systemd-coredump root 4096 10月 28 07:18 data
[root@localhost mysql]# cd data/
#查看data中是否存在數(shù)據(jù):發(fā)現(xiàn)存在數(shù)據(jù),說明數(shù)據(jù)映射沒問題
[root@localhost data]# ll
總用量 100708
-rw-r----- 1 systemd-coredump input 1737 10月 28 07:16 7b133357d04a.err
-rw-r----- 1 systemd-coredump input 56 10月 28 07:16 auto.cnf
drwxr-x--- 2 systemd-coredump input 41 10月 28 07:19 backend
-rw-r----- 1 systemd-coredump input 3039831 10月 28 07:16 binlog.000001
-rw-r----- 1 systemd-coredump input 1643 10月 28 07:19 binlog.000002
-rw-r----- 1 systemd-coredump input 32 10月 28 07:16 binlog.index
-rw------- 1 systemd-coredump input 1680 10月 28 07:16 ca-key.pem
-rw-r--r-- 1 systemd-coredump input 1108 10月 28 07:16 ca.pem
-rw-r--r-- 1 systemd-coredump input 1108 10月 28 07:16 client-cert.pem
-rw------- 1 systemd-coredump input 1680 10月 28 07:16 client-key.pem
-rw-r----- 1 systemd-coredump input 196608 10月 28 07:20 '#ib_16384_0.dblwr'
-rw-r----- 1 systemd-coredump input 8585216 10月 28 07:16 '#ib_16384_1.dblwr'
-rw-r----- 1 systemd-coredump input 5460 10月 28 07:16 ib_buffer_pool
-rw-r----- 1 systemd-coredump input 12582912 10月 28 07:19 ibdata1
-rw-r----- 1 systemd-coredump input 12582912 10月 28 07:16 ibtmp1
drwxr-x--- 2 systemd-coredump input 4096 10月 28 07:16 '#innodb_redo'
drwxr-x--- 2 systemd-coredump input 187 10月 28 07:16 '#innodb_temp'
drwxr-x--- 2 systemd-coredump input 143 10月 28 07:16 mysql
-rw-r----- 1 systemd-coredump input 32505856 10月 28 07:19 mysql.ibd
lrwxrwxrwx 1 systemd-coredump input 27 10月 28 07:16 mysql.sock -> /var/run/mysqld/mysqld.sock
drwxr-x--- 2 systemd-coredump input 8192 10月 28 07:16 performance_schema
-rw------- 1 systemd-coredump input 1676 10月 28 07:16 private_key.pem
-rw-r--r-- 1 systemd-coredump input 452 10月 28 07:16 public_key.pem
-rw-r--r-- 1 systemd-coredump input 1108 10月 28 07:16 server-cert.pem
-rw------- 1 systemd-coredump input 1676 10月 28 07:16 server-key.pem
drwxr-x--- 2 systemd-coredump input 28 10月 28 07:16 sys
-rw-r----- 1 systemd-coredump input 16777216 10月 28 07:19 undo_001
-rw-r----- 1 systemd-coredump input 16777216 10月 28 07:20 undo_002
(4).刪除容器,重啟啟動一個容器,查看數(shù)據(jù)是否保存
$查看當(dāng)前運行中的容器
[root@localhost data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7b133357d04a a3b6608898d6 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mymysql
#刪除mysql容器:這時外網(wǎng)連接不上mysql容器了,數(shù)據(jù)保存在/var/www/mysql/中
[root@localhost data]# docker rm -f 7b133357d04a
7b133357d04a
#查看刪除是否成功
[root@localhost data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#重新創(chuàng)建一個mysql容器
[root@localhost data]# docker run -it -d --name mymysql_bak -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /var/www/mysql/conf.d:/etc/mysql/conf.d -v /var/www/mysql/data/:/var/lib/mysql a3b6608898d6
a54a9c659b12c8c2a329a22f4a59994e2ccbf9f72470e2ba6ba4b5bc66d9d328
#mysql容器創(chuàng)建成功
[root@localhost data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a54a9c659b12 a3b6608898d6 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mymysql_bak
[root@localhost data]#
再次連接mysql,能夠連接,說明數(shù)據(jù)保存在本地,沒問題,當(dāng)刪除容器后,數(shù)據(jù)沒有丟失
三.部署Redis,遠程連接Redis,啟動容器配置密碼
可以到https://hub.docker.com/或者Docker去搜索redis鏡像,然后下載,也可以直接通過docker pull redis下載鏡像,然后用這個redis鏡像啟動容器redis,這樣系統(tǒng)就集成了redis服務(wù)了
1.下載redis鏡像并啟動容器,然后本地連接
#下載redis
[root@localhost zph]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
a378f10b3218: Already exists
b266cd8112a6: Pull complete
7ba86e6448de: Pull complete
3aeb7c9e9a5f: Pull complete
de3be2a98bda: Pull complete
4f4fb700ef54: Pull complete
98e18d21aa3b: Pull complete
Digest: sha256:1f1bd4adf5dabf173b235ba373faef55f3ad53394791d1473763bf5a2181780d
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
#查看是否下載了redis鏡像
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest e579380d4317 12 days ago 138MB
#通過鏡像啟動一個redis容器
[root@localhost zph]# docker run -it -d --name -p 2379:2379 myredis1 e579380d4317
41d9fd8b87d365d94cf1262a612b8af04c39ccb71a49f07b9cf353a381cb5e56
#查看是否啟動了redis容器
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
41d9fd8b87d3 e579380d4317 "docker-entrypoint.s…" 5 seconds ago Up 3 seconds 6379/tcp myredis1
#進入reids容器
[root@localhost zph]# docker exec -it 41d9fd8b87d3 /bin/bash
#連接redis
root@41d9fd8b87d3:/data# redis-cli
#設(shè)置數(shù)據(jù)key=>value
127.0.0.1:6379> set username zhangsan
OK
127.0.0.1:6379> set age 10
OK
#獲取數(shù)據(jù):說明redis容器ok
127.0.0.1:6379> get username
"zhangsan"
127.0.0.1:6379> get age
"10"
2.遠程連接
如果需要在遠程 redis 服務(wù)上執(zhí)行命令,同樣使用的也是 redis-cli 命令,語法:????????
#-h 服務(wù)器地址 -p 端口號
redis-cli -h host -p port
C:\Users\zph>redis-cli -h 192.168.0.6 -p 6379
192.168.0.6:6379> get username
"zhangsan"
192.168.0.6:6379>
?3.啟動redis容器,配置密碼
配置密碼后,訪問redis時就需要輸入密碼,這樣安全性就高了,通過 --requirepass 可以配置密碼
#啟動redis容器,并配置訪問密碼
[root@localhost zph]# docker run -it -d --name myredis3 -p 2379:2379 e579380d4317 --requirepass "123456"
804b873aa59c00acbba1dd77fdbcb3d1a7e52db1b93b9b9693bd775608ca83ba
#查看是否啟動了redis
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
804b873aa59c e579380d4317 "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:2379->2379/tcp, :::2379->2379/tcp, 6379/tcp myredis3
[root@localhost zph]#
#進入redis容器
[root@localhost zph]# docker exec -it 804b873aa59c /bin/bash
#啟動redis客戶端
root@804b873aa59c:/data# redis-cli
#獲取數(shù)據(jù):提示沒得權(quán)限
127.0.0.1:6379> get *
(error) NOAUTH Authentication required.
#授權(quán)訪問:輸入密碼
127.0.0.1:6379> auth 123456
OK
#設(shè)置數(shù)據(jù)并訪問,操作成功
127.0.0.1:6379> set username zhangsan
OK
127.0.0.1:6379> get username
"zhangsan"
127.0.0.1:6379>
?4.遠程連接(有密碼的redis)
#注意映射的端口: -p 的端口
C:\Users\zph>redis-cli -h 192.168.0.6 -p 6379
192.168.241.0.6> auth 123456
OK
四.部署Mongodb,配置密碼 遠程連接
可以到https://hub.docker.com/或者Docker去搜索mongodb鏡像,然后下載,也可以直接通過docker pull mongodb下載鏡像,然后用這個mongodb鏡像啟動容器mongodb,這樣系統(tǒng)就集成了mongodb服務(wù)了
1.下載mongodb鏡像并啟動容器,然后本地連接(不需要密碼)
?
在啟動容器的時候,可以先使用-p映射端口,以及-v掛載數(shù)據(jù)目錄,這樣就可以數(shù)據(jù)持久化了
#下載mongo
[root@localhost ~]# docker pull mongo
Using default tag: latest
latest: Pulling from library/mongo
16ec32c2132b: Pull complete
6335cf672677: Pull complete
4b9c6ac629be: Pull complete
4de7437f497e: Pull complete
Digest: sha256:d78c7ace6822297a7e1c7076eb9a7560a81a6ef856ab8d9cde5d18438ca9e8bf
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest
#啟動容器 映射端口 掛載目錄
[root@localhost ~]#docker run --name mymongo -p 27017:27017 -v /var/www/data:/data/db -d mongo
#查看是否啟動mongo容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
f9b5dd026a4e mongo "docker-entrypoint.s…" 12 seconds ago Up 11 seconds
0.0.0.0:27017->27017/tcp, :::27017->27017/tcp mymongo
#進入mongo容器
[root@localhost ~]# docker exec -it f9b5dd026a4e /bin/bash
root@f9b5dd026a4e:/# mongo
MongoDB shell version v5.0.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mong
odb
Implicit session: session { "id" : UUID("d200ae7f-e85c-4a17-8b24-55f4f116e08a") }
MongoDB server version: 5.0.1
================ Warning: the "mongo" shell has been superseded by "mongosh", which delivers improved usability and compatibility.The "mongo" shell has been deprecate
d and will be removed in
an upcoming release. We recommend you begin using "mongosh". For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/ ================ Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
--- The server generated these startup warnings when booting:
2021-07-28T04:25:25.178+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2021-07-28T04:25:25.178+00:00: /sys/kernel/mm/transparent_hugepage/enabled is
'always'. We suggest setting it to 'never' ---
--- Enable MongoDB's free cloud-based monitoring service, which will then receive
and display
#展示數(shù)據(jù)庫
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
#創(chuàng)建test數(shù)據(jù)庫并進入
> use test
switched to db test
#查看是否創(chuàng)建好了test數(shù)據(jù)庫
> show dbs
admin 0.000GB
config 0.000GB
test 0.000GB
local 0.000GB
>
#創(chuàng)建user表并插入數(shù)據(jù)
> db.user.insert({username:"張三"})
WriteResult({ "nInserted" : 1 })
#查看數(shù)據(jù)表數(shù)據(jù)
> db.user.find({})
可以通果docker inspect mongo | grep data查看映射關(guān)系
2.遠程連接
需要在遠程服務(wù)器上安裝mongo
C:\Users\zph>mongo 192.168.0.6:27017
MongoDB shell version v4.2.5
connecting to: mongodb://192.168.0.6:27017/test?compressors=disabled&gssapiServiceN
ame=mongodb
Implicit session: session { "id" : UUID("2e196a91-5657-4dfb-b9e3-c68491385dc1") }
MongoDB server version: 5.0.1
WARNING: shell and server versions do not match
Server has startup warnings:
{"t":{"$date":"2021-07-28T04:25:25.178+00:00"},"s":"W", "c":"CONTROL", "id":22120, "ct
x":"initandlisten","msg":"Access control is not enabled for the database. Read and write a
ccess to data and configuration is unrestricted","tags":["startupWarnings"]}
{"t":{"$date":"2021-07-28T04:25:25.178+00:00"},"s":"W", "c":"CONTROL", "id":22178, "ct
x":"initandlisten","msg":"/sys/kernel/mm/transparent_hugepage/enabled is 'always'. We sugg
est setting it to 'never'","tags":["startupWarnings"]}
--- Enable MongoDB's free cloud-based monitoring service, which will then receive and displ
ay
metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessi
ble to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitori
ng() ---
#展示數(shù)據(jù)庫
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
#創(chuàng)建test數(shù)據(jù)庫并進入
> use test
switched to db test
#創(chuàng)建user表并插入數(shù)據(jù)
> db.user.insert({username:"張三"})
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.000GB
config 0.000GB
test0.000GB
local 0.000GB
> exit;
3.啟動mongo容器,配置密碼
配置密碼后,訪問mongo時就需要輸入密碼,這樣安全性就高了,語法:
docker run -d --name authMongo -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=123456 -p 27017:27017 -v /var/www/mongo/data:/data/db mongo --auth
#通過mongo鏡像啟動一個mongo容器
[root@localhost ~]#docker run -d --name authMongo -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=123456 -p 27017:27017 -v /var/www/mongo/data:/data/db mongo --auth
#查看是否啟動容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
1bae12b5f438 mongo "docker-entrypoint.s…" 3 seconds ago Up 2 seconds
0.0.0.0:27017->27017/tcp, :::27017->27017/tcp authMongo
#進入容器:并進入admin數(shù)據(jù)庫
[root@localhost ~]# docker exec -it 1bae12b5f438 mongo admin
MongoDB shell version v5.0.1
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName =mongodb
Implicit session: session { "id" : UUID("1c53734c-cc5e-41b1-a92c-d413a2a306e4")
#查看數(shù)據(jù)
> show dbs
#授權(quán)
> db.auth('admin', '123456')
#查看數(shù)據(jù)
> show dbs
admin 0.000GB
config 0.000GB
teset 0.000GB
local 0.000GB
#創(chuàng)建一個賬號并授權(quán)
db.createUser({ user:'zhangsan',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
> Successfully added user: { "user" : "zhangsan", "roles" : [
{
role" : "userAdminAnyDatabase", "db" : "admin"
},"readWriteAnyDatabase"
]
}
4.遠程連接(有密碼)
注意加上數(shù)據(jù)庫表
mongo 192.168.0.6:27017/zhangsan
C:\Users\zph>mongo 192.168.0.6:27017/zhangsan
MongoDB shell version v5.0.1
connecting to: mongodb://192.168.241.128:27017/admin?compressors=disabled&gssapiServic
eName=mongodb
Implicit session: session { "id" : UUID("c42d2b91-34ac-4d77-9754-38a826fe78b1") }
MongoDB server version: 5.0.1
================ Warning: the "mongo" shell has been superseded by "mongosh", which delivers improved usability and compatibility.The "mongo" shell has been deprecate
d and will be removed in
an upcoming release. We recommend you begin using "mongosh". For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/ ================
> db.auth("zhangsan","123456")
1
>
[上一節(jié)][Docker]三.Docker 部署nginx,以及映射端口,掛載數(shù)據(jù)卷?文章來源:http://www.zghlxwxcb.cn/news/detail-738404.html
[下一節(jié)][Docker]五.Docker中Dockerfile詳解文章來源地址http://www.zghlxwxcb.cn/news/detail-738404.html
到了這里,關(guān)于[Docker]四.Docker部署nodejs項目,部署Mysql,部署Redis,部署Mongodb的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!