在Node.js Docker映像上运行Redis [英] Running redis on nodejs Docker image

查看:142
本文介绍了在Node.js Docker映像上运行Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Docker映像,它是一个node.js应用程序。该应用程序从本地运行的Redis检索一些配置值。因此,我正在尝试在Docker映像内的同一容器中安装和运行Redis。

I have a Docker image which is a node.js application. The app retrieves some configuration value from Redis which is running locally. Because of that, I am trying to install and run Redis within the same container inside the Docker image.

如何扩展Docker文件并在其中配置Redis?

How can I extend the Docker file and configure Redis in it?

到目前为止,Dockerfile如下:

As of now, the Dockerfile is as below:


FROM节点:碳

FROM node:carbon

WORKDIR / app

WORKDIR /app

COPY package.json / app

COPY package.json /app

运行npm install

RUN npm install

COPY。 / app

COPY . /app

EXPOSE 3011

EXPOSE 3011

CMD节点/app/src/server.js

CMD node /app/src/server.js


推荐答案

最好的解决方案是使用docker compose。这样,您将创建一个redis容器,链接到它,然后启动您的node.js应用程序。首先是要在此处详细安装docker compose-( https://docs.docker.com/compose/安装/ )。

The best solution would be to use docker compose. With this you would create a redis container, link to it then start your node.js app. First thing would be to install docker compose detailed here - (https://docs.docker.com/compose/install/).

启动并运行后,应在与应用程序的dockerfile相同的文件夹中创建docker-compose.yml。它应包含以下

Once you have it up and running, You should create a docker-compose.yml in the same folder as your app's dockerfile. It should contain the following

version: '3'
services:
  myapp:
    build: .  
    ports:
     - "3011:3011"
    links:
     - redis:redis
  redis:
    image: "redis:alpine"

然后可以从您的node.js应用程序访问redis,而不是 localhost:6379 ,您将使用 redis:6379 访问redis实例。

Then redis will be accessible from your node.js app but instead of localhost:6379 you would use redis:6379 to access the redis instance.

要启动您的应用您将在终端中运行 docker-compose up 。最佳做法是使用网络代替链接,但这是为了简单起见。

To start your app you would run docker-compose up, in your terminal. Best practice would be to use a network instead of links but this was made for simplicity.

这也可以根据需要完成,将redis和node.js放在同一个映像上,以下Dockerfile应该可以工作,它基于问题所在:

This can also be done as desired, having both redis and node.js on the same image, the following Dockerfile should work, it is based off what is in the question:

FROM node:carbon

RUN wget http://download.redis.io/redis-stable.tar.gz && \
    tar xvzf redis-stable.tar.gz && \
    cd redis-stable && \
    make && \
    mv src/redis-server /usr/bin/ && \
    cd .. && \
    rm -r redis-stable && \
    npm install -g concurrently   

EXPOSE 6379

WORKDIR /app

COPY package.json /app

RUN npm install

COPY . /app

EXPOSE 3011

EXPOSE 6379

CMD concurrently "/usr/bin/redis-server --bind '0.0.0.0'" "sleep 5s; node /app/src/server.js" 

第二种方法确实是不好的做法为了简化起见,我同时使用而不是主管或类似工具。 CMD中的睡眠是为了允许Redis在实际启动应用程序之前启动,您应该对其进行调整以使其最适合您。希望这会有所帮助,并且您会使用第一种方法,因为这是更好的做法

This second method is really bad practice and I have used concurrently instead of supervisor or similar tool for simplicity. The sleep in the CMD is to allow redis to start before the app is actually launched, you should adjust it to what suits you best. Hope this helps and that you use the first method as it is much better practice

这篇关于在Node.js Docker映像上运行Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆