在 nodejs Docker 镜像上运行 redis [英] Running redis on nodejs Docker image

查看:38
本文介绍了在 nodejs 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:

来自节点:碳

工作目录/应用程序

复制 package.json/app

COPY package.json /app

运行 npm install

RUN npm install

复制./应用

曝光 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,但您将使用 redis:6379 而不是 localhost: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.最佳做法是使用 network 而不是 links,但这是为了简单起见.

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中的sleep是为了让redis在app真正启动之前就启动,你应该调整到最适合你的.希望这会有所帮助,并且您使用第一种方法,因为它是更好的做法

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

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

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