简体   繁体   English

Node.js 未在 Docker 中运行,我的 docker-compose.yml 是否正确?

[英]Node.js is not running in Docker is my docker-compose.yml correct?

The problem:问题:

I'm trying to set up a Docker WordPress development environment on Windows 11 with wsl2.我正在尝试使用 wsl2 在 Windows 11 上设置 Docker WordPress 开发环境。 I created a docker-compose.yml and everything works apart from the node install.我创建了一个 docker-compose.yml,除了节点安装外,一切正常。 The container tries to start it and then just stops.容器尝试启动它,然后停止。 Is something in my docker-compose file wrong?我的 docker-compose 文件有问题吗?

I want to use node because I use gulp, npm and browser sync for my WordPress themes.我想使用节点,因为我为我的 WordPress 主题使用 gulp、npm 和浏览器同步。

This is my docker-compose.yml:这是我的 docker-compose.yml:

version: "3.9"

services:
  db:
    image: mysql:5.7
    volumes:
      - dbdata:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - wp-content:/var/www/html/wp-content
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
  node:
    restart: "no"
    image: node:latest
    container_name: nodejs
    depends_on:
      - wordpress
    volumes:
      - node-data:/usr/src/app
    ports:
      - 3000:3000
      - 3001:3001
volumes:
  dbdata:
  wp-content:
  node-data:

You have to use a Dockerfile otherwise it will never ever work!您必须使用 Dockerfile 否则它将永远无法工作!

Problem is that you have mentioned to spin up the docker container but just think for a sec how will you command node to tell that what it needs to do.When it sees no action commanded it basically closes up.问题是您已经提到要启动 docker 容器,但请想一想您将如何命令节点来告诉它需要做什么。当它没有看到任何命令时,它基本上会关闭。

Solution解决方案

  1. Long way - You can use up a dockerfile, as mentioned by Tom, dockerfile is generally used to containerise your application so whatever code you need do just write it, then build your code into a docker image with node using Dockerfile, but in that case trouble is that everytime you are doing any code change you have reiterate over the same process and again build your code. Long way - You can use up a dockerfile, as mentioned by Tom, dockerfile is generally used to containerise your application so whatever code you need do just write it, then build your code into a docker image with node using Dockerfile, but in that case麻烦的是,每次您进行任何代码更改时,您都会重复相同的过程并再次构建您的代码。
  2. Another simple way I could think of is add a command:npm start in your node image and this should help you probably我能想到的另一种简单方法是在您的节点映像中添加一个command:npm start ,这可能会对您有所帮助

Forgive me if I have suggested something wrong because I am also learner in docker:)如果我提出了错误的建议,请原谅我,因为我也是 docker 的学习者:)

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

相关问题 如何在不使用 Dockerfile 的情况下仅使用 Docker-compose.yml 文件启动 Node.js 应用程序的容器 - How to start the container for the Node.js application using just Docker-compose.yml file without using the Dockerfile 在没有docker-compose.yml文件的单个docker文件中运行react和node - running react and node in a single docker file without docker-compose.yml file 为什么我的节点容器会忽略我在docker-compose.yml中设置的环境变量? - Why does my node container ignore the environment variable I set in docker-compose.yml? 源代码控制(git)docker-compose.yml - Source controlling (git) docker-compose.yml .dockerignore 不忽略 docker-compose.yml - .dockerignore not ignoring docker-compose.yml docker-compose.yml问题nodejs和mysql - docker-compose.yml issue nodejs and mysql docker-compose.yml用于postgres遇到错误 - docker-compose.yml for postgres encountering error 能够使用 docker-compose.yml 在容器和 redis 容器中启动 node js 应用程序,但无法访问应用程序 - Able to bring up a node js application in a container and a redis container using docker-compose.yml but unable to access application 如何使用docker-compose.debug.yml调试在docker中运行的节点? - How do I use docker-compose.debug.yml to debug my node running in docker? 基于 docker-compose.yml 文件的节点环境变量的切换命令 - switching command based on node env variable for the docker-compose.yml file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM