简体   繁体   English

Docker如何在Container中使用redis启动nodejs应用程序?

[英]Docker how to start nodejs app with redis in the Container?

I have simple but curious question, i have based my image on nodejs image and i have installed redis on the image, now i wanted to start redis and nodejs app both running in the container when i do the docker-compose up. 我有一个简单但好奇的问题,我已将我的图像基于nodejs图像,我已在图像上安装了redis,现在我想启动redis和nodejs应用程序在我执行docker-compose时在容器中运行。 However i can only get one working, node always gives me an error. 但是我只能得到一个工作,节点总是给我一个错误。 Does anyone has any idea to 有没有人有任何想法

  1. How to start the nodejs application on the docker-compose up ? 如何在docker-compose上启动nodejs应用程序?
  2. How to start the redis running in the background in the same image/container ? 如何在同一图像/容器中启动redis在后台运行?

My Docker file as below. 我的Docker文件如下。

# Set the base image to node
FROM node:0.12.13

# Update the repository and install Redis Server
RUN apt-get update && apt-get install -y redis-server libssl-dev wget curl gcc

# Expose Redis port 6379
EXPOSE      6379
# Bundle app source
COPY ./redis.conf /etc/redis.conf

EXPOSE  8400

WORKDIR /root/chat/
CMD ["node","/root/www/helloworld.js"]
ENTRYPOINT  ["/usr/bin/redis-server"]

Error i get from the console logs is 我从控制台日志得到的错误是

[36mchat_1 | [36mchat_1 | [0m[1] 18 Apr 02:27:48.003 # Fatal error, can't open config file 'node' [0m [1] 18 Apr 02:27:48.003#致命错误,无法打开配置文件'node'

Docker-yml is like below Docker-yml如下所示

chat:
    build: ./.config/etc/chat/
    volumes:
        - ./chat:/root/chat
    expose:
        - 8400
    ports:
        - 6379:6379  
        - 8400:8400   
    environment:
        CODE_ENV: debug
        MYSQL_DATABASE: xyz
        MYSQL_USER: xyz
        MYSQL_PASSWORD: xyz        
    links:
        - mysql   
    #command: "true"  

A docker file can have but one entry point(either CMD or ENTRYPOINT, not both). docker文件只能有一个入口点(CMD或ENTRYPOINT,而不是两者)。 But, you can run multiple processes in a single docker image using a process manager like systemd. 但是,您可以使用systemd等进程管理器在单个docker镜像中运行多个进程。 There are countless recipes for doing this all over the internet. 互联网上有无数的食谱可供选择。 You might use this docker image as a base: 您可以使用此泊坞窗图像作为基础:

https://github.com/million12/docker-centos-supervisor https://github.com/million12/docker-centos-supervisor

However, I don't see why you wouldn't use docker compose to spin up a separate redis container, just like you seem to want to do with mysql. 但是,我不明白为什么你不会使用docker compose来启动一个单独的redis容器,就像你似乎想要使用mysql一样。 BTW where is the mysql definition in the docker-compose file you posted? BTW您发布的docker-compose文件中的mysql定义在哪里?

Here's an example of a compose file I use to build a node image in the current directory and spin up redis as well. 这是一个我用来在当前目录中构建节点图像的compose文件的示例,并且也会启动redis。

web:
  build: .
  ports:
    - "3000:3000"
    - "8001:8001"
  environment:
    NODE_ENV: production
    REDIS_HOST: redis://db:6379
  links:
    - "db"
db:
  image: docker.io/redis:2.8

It should work with a docker file looking like the one you have minus trying to start up redis. 它应该与docker文件一起工作,看起来就像你试图启动redis的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM