使用单个Dockerfile部署Nginx+Flask+Mongo

MongoDB如何查看版本信息详解

使用Docker部署Nginx+Flask+Mongo的应用

Dockerfile方式

1. 目录结构

2. Dockerfile

FROM ubuntu:18.04

# 设置时区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# flask
ENV FLASK_WORKDIR /home/flask

# mongo
ENV MONGO_WORKDIR /usr/local/work

RUN mkdir -p $FLASK_WORKDIR
WORKDIR $FLASK_WORKDIR
ADD src/requirements.txt $FLASK_WORKDIR/requirements.txt
COPY src/ $FLASK_WORKDIR

# 安装mongodb,python和nginx
RUN apt-get update && apt-get install -y --no-install-recommends python3-pip python3-dev python3-setuptools python3-wheel && ln -s /usr/bin/python3 /usr/bin/python \
    && pip3 install -U pip \
    && pip3 install -i https://pypi.douban.com/simple/ -r requirements.txt \
    && mkdir -p $MONGO_WORKDIR && mkdir -p /home/run && mkdir -p /data/db \
    && apt-get install -y mongodb \
    && apt-get install -y --no-install-recommends nginx net-tools vim lsof

COPY conf/nginx.conf /etc/nginx/nginx.conf
COPY conf/mongodb.conf /etc/mongodb.conf
COPY frontend/dist/ /usr/share/nginx/html

# 启动flask命令
# CMD ["python", "manage.py","run"]
# 使用bash启动多个服务
COPY start.sh /home/run/start.sh

CMD ["/bin/bash","-c","source /home/run/start.sh"]

3. start.sh

#!/bin/bash

# 安装网络工具
apt-get install net-tools &


service nginx start &

# 启动flask
python manage.py run &


service mongodb start 

mongo 127.0.0.1:27017 <<EOF
use admin;  
db.createUser( {user: "root",pwd: "root123",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]});
db.auth('root', 'root123');
use currency;
db.createUser({ user: "user", pwd: "password", roles: [{ role: "dbOwner", db: "currency" }] });
db.createCollection("test");
EOF

mongo 127.0.0.1:27017 <<EOF
use admin;
db.shutdownServer();
EOF


mongod --auth --config /etc/mongodb.conf

4. conf/nginx.conf

# user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid    /var/run/nginx.pid;

events {
  worker_connections 1024;
}


http {
  include    /etc/nginx/mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx/access.log main;

  sendfile    on;
  tcp_nopush   on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;

    ##
    # Logging Settings
    ##
 
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
 
    ##
    # Gzip Settings
    ##

  # 开启gzip
  gzip on;
  gzip_min_length 100;
  # gzip_buffers 4 16k;
  #gzip_http_version 1.0;
  gzip_comp_level 9;
  gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
  # gzip_vary off;
  gzip_disable "MSIE [1-6]\.";

  server {
    listen 10234;
    # server_name www.frontend.com;
    # server_name localhost;
    keepalive_timeout 5;
    root /usr/share/nginx/html/;

    # location /static/ {
    #   alias /usr/share/nginx/html/src/app/static/;
    # }

    location / {
        # checks for static file, if not found proxy to app

        # proxy_redirect off;
        # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # proxy_set_header Host $http_host;
        # proxy_set_header X-Real-IP $remote_addr;

        #proxy_buffers 8 32k;
        #proxy_buffer_size 64k;

        index index.html index.htm;
        try_files $uri $uri/ /index.html;
        expires -1;
    }

            
    # location /api/v1 {
        # proxy_pass http://192.168.0.2:5000; # 发布在阿里云上,应填写内网IP
    #     proxy_pass http://0.0.0.0:5000;
    # }
  }
}

5. conf/mongodb.conf

# mongodb.conf

# Where to store the data.
dbpath=/var/lib/mongodb

#where to log
logpath=/var/log/mongodb/mongodb.log

logappend=true

bind_ip = 0.0.0.0
port = 27017

# Enable journaling, http://www.mongodb.org/display/DOCS/Journaling
journal=true

# Enables periodic logging of CPU utilization and I/O wait
#cpu = true

# Turn on/off security.  Off is currently the default
#noauth = true
#auth = true

# Verbose logging output.
#verbose = true

# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true

# Enable db quota management
#quota = true

# Set diagnostic logging level where n is
#   0=off (default)
#   1=W
#   2=R
#   3=both
#   7=W+some reads
#diaglog = 0

# Diagnostic/debugging option
#nocursors = true

# Ignore query hints
#nohints = true

# Disable the HTTP interface (Defaults to localhost:27018).
#nohttpinterface = true

# Turns off server-side scripting.  This will result in greatly limited
# functionality
#noscripting = true

# Turns off table scans.  Any query that would do a table scan fails.
#notablescan = true

# Disable data file preallocation.
#noprealloc = true

# Specify .ns file size for new databases.
# nssize = <size>

# Accout token for Mongo monitoring server.
#mms-token = <token>

# Server name for Mongo monitoring server.
#mms-name = <server-name>

# Ping interval for Mongo monitoring server.
#mms-interval = <seconds>

# Replication Options

# in replicated mongo databases, specify here whether this is a slave or master
#slave = true
#source = master.example.com
# Slave only: specify a single database to replicate
#only = master.example.com
# or
#master = true
#source = slave.example.com

# Address of a server to pair with.
#pairwith = <server:port>
# Address of arbiter server.
#arbiter = <server:port>
# Automatically resync if slave data is stale
#autoresync
# Custom size for replication operation log.
#oplogSize = <MB>
# Size limit for in-memory storage of op ids.
#opIdMem = <bytes>

# SSL options
# Enable SSL on normal ports
#sslOnNormalPorts = true
# SSL Key file and password
#sslPEMKeyFile = /etc/ssl/mongodb.pem
#sslPEMKeyPassword = pass

6. 构建镜像

docker build -t [镜像名称] .

7. 构建容器

docker run -v -p 27017:27017 -p 5000:5000 -p 10234:10234 [镜像名称]

8. 进入容器

docker exec -it [容器id] /bin/bash

docker compose方式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值