简体   繁体   English

如何在 alpine linux docker 中使用端口

[英]how to use ports inside alpine linux docker

is there any way i can use redis service for my nodejs, i'am using alpine docker and somehow my node-redis client is not able to connect to redis (it says " can not connect to 127.0.0.1:6479 "), i've tried many thing but it is not working, and i want to achieve by only docker file not docker-compose.有什么办法可以为我的 nodejs 使用 redis 服务,我正在使用 alpine docker 并且我的 node-redis 客户端无法连接到 redis(它说“无法连接到 127.0.0.1:6479 ”),我已经尝试了很多东西但它不起作用,我只想通过 docker file 而不是 docker-compose 来实现。 here is my docker file code这是我的 docker 文件代码

FROM alpine
RUN apk add --update nodejs npm redis
EXPOSE 6379 9000
WORKDIR /server
COPY . /server/
RUN npm install
RUN redis-server &
CMD npm start

Your issue is RUN redis-server & .您的问题是RUN redis-server & RUN statements are run at build time, so when your container starts up, the redis-server process isn't there anymore. RUN 语句在构建时运行,因此当您的容器启动时,redis-server 进程不再存在。 You need to start it at run-time on the CMD statement like this您需要在运行时像这样在 CMD 语句上启动它

CMD redis-server & npm start

But as Zac points out, the 'docker way' to do it is to run Redis is a separate container.但正如 Zac 指出的那样,“docker 方式”是将 Redis 运行在一个单独的容器中。 There are ready made redis images on docker hub you can use.您可以使用 docker hub 上现成的 redis 映像。

You could run multiple commands in one Docker container, but that's a bad practice.可以在一个 Docker 容器中运行多个命令,但这是一种不好的做法。 You could do this by having your npm start run a script that runs both your server and Redis, or using a package like concurrently or npm-run-all .你可以通过让你的npm start运行一个同时运行你的服务器和 Redis 的脚本,或者使用像concurrentlynpm-run-all这样的包来做到这一点。 Docker good practice is to have one executable per container, though, so the Docker-correct thing to do would be to put Redis in a separate container.不过,Docker 的良好做法是每个容器都有一个可执行文件,因此 Docker 正确的做法是将 Redis 放在一个单独的容器中。

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

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