安装
-
docker 操作 nginx
# -d 后台启动nginx
docker run -p 8000:80 -d -v $PWD/www:/usr/share/nginx/html nginx
# 查看进程, docker ps -a 查看所有进程
docker ps
# 停止stop, 启动start,删除rm
docker stop ID
#进入容器
docker exec -it ID /bin/bash
-
docker-compose 操作 mongo
version: '3.1'
services:
mongo:
image: mongo
restart: always
ports:
- 27017:27017
mongo-express:
image: mongo-express
restart: always
ports:
- 8000:8081
执行 docker-compose up
-
docker-compose 操作nginx实战
- 安装 vscode 插件 deploy,本地代码可以同步服务器
项目根目录新建 .vscode文件夹,下面新建settings.json
{
"deploy": {
"packages": [{
"files": [
"**/*",
],
"exclude": [
"node_modules/**",
".git/**",
".vscode/**",
"**/node_modules/**",
],
"deployOnSave": false
}],
"targets": [{
"type": "sftp",
"name": "AliyunServer",
"dir": "/root/source/docker_ci",
"host": "47.98.252.43",
"port": 22,
"user": "root",
"privateKey": "/Users/xia/.ssh/id_rsa"
}],
},
}
- 项目根目录下新建 /nginx/conf.d/docker.conf 文件
server {
listen 80;
# server_name www.josephxia.com;
location / {
root /var/www/html;
index index.html index.htm;
}
location ~ \.(gif|jpg|png)$ {
root /static;
index index.html index.htm;
}
location /api {
proxy_pass http://app-pm2:3000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# location = / {
# rewrite ^(.*) https://www.josephxia.com/$1 permanent;
# }
}
# server {
# listen 443;
# server_name localhost;
# ssl on;
# root html;
# index index.html index.htm;
# ssl_certificate conf.d/cert/www.josephxia.com.pem;
# ssl_certificate_key conf.d/cert/www.josephxia.com.key;
# ssl_session_timeout 5m;
# ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_prefer_server_ciphers on;
# location / {
# root /var/www/html;
# index index.html index.htm;
# }
# }
- 项目根目录下 新建 docker-compose.yml文件
version: '3.1'
services:
app-pm2:
container_name: app-pm2
#构建容器
build: ./backend
#直接从git拉去
# build: git@github.com:su37josephxia/docker_ci.git#:backend
# 需要链接本地代码时
# volumes:
# - ./backend:/usr/src/app
ports:
- "3000:3000"
mongo:
image: mongo
restart: always
ports:
- 27017:27017
# mongo-express:
# image: mongo-express
# restart: always
# ports:
# - 8081:8081
nginx:
restart: always
image: nginx
ports:
- 8091:80
volumes:
- ./nginx/conf.d/:/etc/nginx/conf.d
- ./frontend/dist:/var/www/html/
- ./static/:/static/
- 执行 docker-compose up 命令
网友评论