docker - docker下运行vue/cli app simple index.html打开

标签 docker vue.js vuejs2 vue-cli

我想在 docker 下安装我的 @vue/cli 4.0.5 应用程序,我找到了这个包 https://hub.docker.com/r/ebiven/vue-cli 假设这就是我需要的(?)我修改了 _Docker/docker-compose.yml

web:
    container_name: vtasks_web

    build:
        context: ./web
        dockerfile: Dockerfile.yml

    environment:
        - APACHE_RUN_USER=www-data
    volumes:
        - ${APP_PATH_HOST}:${APP_PTH_CONTAINER}
    ports:
        - 8088:80
    working_dir: ${APP_PTH_CONTAINER}

...

vue_cli:
    container_name: vtasks_vue_cli
    image: ebiven/vue-cli:latest
    volumes:
        - ${APP_PATH_HOST}:${APP_PTH_CONTAINER}
    working_dir: ${APP_PTH_CONTAINER}
    command: npm install

构建应用程序并接下来检查日志我没有错误,还创建了node_modules(我之前删除了它):

但是我跑进灌木丛

npm run serve

命令,它显示:

ONE  Compiled successfully in 10065ms                                                                                                                                                                                            4:21:52 PM


  App running at:
  - Local:   http://localhost:8080/ 

  It seems you are running Vue CLI inside a container.
  Access the dev server via http://localhost:<your container's external mapped port>/

  Note that the development build is not optimized.
  To create a production build, run npm run build.

所以我就跑了

http://localhost:8088/

如在网络容器中我设置了 8088:80 但是在浏览器中我看到index.html被打开(通过标题和页面内容)但没有渲染js?

什么是有效的方法?

修改 block : 查看演示 https://github.com/ebiven/docker-vue-cli我看到 ebiven/docker-vue-cli 用作 Web 容器,因此删除了 node_modules 目录并重新制作了我的 _Docker/docker-compose.yml :

version: '3.5'

services:



    web:
        container_name: vtasks_web

        image: ebiven/vue-cli

        command: npm install
#        command: npm install ; npm run serve  // I GOT ERROR HERE
#        command: npm run serve

        build:
            context: ./web
            dockerfile: Dockerfile.yml

        environment:
            - APACHE_RUN_USER=www-data

        volumes:
            - ${APP_PATH_HOST}:${APP_PTH_CONTAINER}
        ports:
            - "8088:80"

        working_dir: ${APP_PTH_CONTAINER}


    db:
        container_name: vtasks_db
        image: mysql:5.7.28
        restart: always
        environment:
            - MYSQL_DATABASE=DockerVTasks
            - MYSQL_USER=docker_user
            - MYSQL_PASSWORD=4321
            - MYSQL_ALLOW_EMPTY_PASSWORD=false
            - MYSQL_ROOT_PASSWORD=321
            - TZ=Europe/Kiev

        volumes:
            - ${DB_PATH_HOST}:/var/lib/mysql


    adminer:
      container_name: vtasks_adminer
      image: adminer
      restart: always
      ports:
        - 8089:8080
      links:
        - db

结果我看到:

$ docker-compose up -d --build     
Building web
Step 1/6 : FROM php:7.3-apache
 ---> 5af347316d4b
Step 2/6 : RUN apt-get update &&     apt-get install -y     python     libfreetype6-dev     libwebp-dev     libjpeg62-turbo-dev     libpng-dev     libzip-dev     nano     mc     git-core     curl     build-essential     openssl     libssl-dev     libgmp-dev     libldap2-dev     netcat     locate     && git clone https://github.com/nodejs/node.git     && cd node     && git checkout v12.0.0     && ./configure      && make      && make install
 ---> Using cache
 ---> b56b2543f6bd
Step 3/6 : RUN npm install cross-env
 ---> Using cache
 ---> f8abda742c47
Step 4/6 : RUN  docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-webp-dir=/usr/include/  --with-jpeg-dir=/usr/include/
 ---> Using cache
 ---> df0636ba5b86
Step 5/6 : RUN  docker-php-ext-install gd pdo pdo_mysql zip gmp bcmath pcntl ldap sysvmsg exif && a2enmod rewrite
 ---> Using cache
 ---> 307c9f243f02
Step 6/6 : COPY virtualhost.conf /etc/apache2/sites-enabled/000-default.conf
 ---> Using cache
 ---> 3c733883faaa

Successfully built 3c733883faaa
Successfully tagged ebiven/vue-cli:latest
Recreating vtasks_web ... 
vtasks_db is up-to-date
Recreating vtasks_web
Recreating vtasks_web ... done
serge@athoe:/mnt/_work_sdb8/wwwroot/lar/VApps/vtasks/_Docker$ docker logs --tail=40  vtasks_web

> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd93929998d08e9c8e8ebdc9d3ccced3cd" rel="noreferrer noopener nofollow">[email protected]</a> install /var/www/vtasks_docker_root/node_modules/node-sass
> node scripts/install.js

Downloading binary from https://github.com/sass/node-sass/releases/download/v4.13.0/linux-x64-72_binding.node
Download complete
Binary saved to /var/www/vtasks_docker_root/node_modules/node-sass/vendor/linux-x64-72/binding.node
Caching binary to /root/.npm/node-sass/4.13.0/linux-x64-72_binding.node

> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ceada1bcabe3a4bd8efde0f8e0ff" rel="noreferrer noopener nofollow">[email protected]</a> postinstall /var/www/vtasks_docker_root/node_modules/core-js
> node -e "try{require('./postinstall')}catch(e){}"

Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js on Open Collective or Patreon: 
> https://opencollective.com/core-js 
> https://www.patreon.com/zloirock 

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)


> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8bdb2ab98eaf6eff6ec" rel="noreferrer noopener nofollow">[email protected]</a> postinstall /var/www/vtasks_docker_root/node_modules/ejs
> node ./postinstall.js

Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)


> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f896979c9dd58b998b8bb8ccd6c9cbd6c8" rel="noreferrer noopener nofollow">[email protected]</a> postinstall /var/www/vtasks_docker_root/node_modules/node-sass
> node scripts/build.js

Binary found at /var/www/vtasks_docker_root/node_modules/node-sass/vendor/linux-x64-72/binding.node
Testing binary
Binary is fine
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7016031506151e040330415e425e4141" rel="noreferrer noopener nofollow">[email protected]</a> (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5b3a6b0a3b0bba1a695e4fbe7fbe4e4" rel="noreferrer noopener nofollow">[email protected]</a>: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

added 1239 packages from 876 contributors and audited 19413 packages in 26.747s
found 0 vulnerabilities

我看到node_modules目录已生成,但我还需要运行

npm run serve

之后

npm install

修改 block #2:

修改命令为

command: bash -c "npm install && npm run serve"

我得到了下一个没有错误的输出:

$ docker logs --tail=20  vtasks_web
<s> [webpack.Progress] 93% after chunk asset optimization
<s> [webpack.Progress] 93% asset optimization
<s> [webpack.Progress] 94% after asset optimization
<s> [webpack.Progress] 94% after seal
<s> [webpack.Progress] 95% emitting
<s> [webpack.Progress] 95% emitting HtmlWebpackPlugin
<s> [webpack.Progress] 95% emitting CopyPlugin
<s> [webpack.Progress] 98% after emitting
<s> [webpack.Progress] 98% after emitting CopyPlugin
 DONE  Compiled successfully in 10316ms4:36:18 AM

<s> [webpack.Progress] 100% 


  App running at:
  - Local:   http://localhost:8080/ 

  It seems you are running Vue CLI inside a container.
  Access the dev server via http://localhost:<your container's external mapped port>/

但是我要运行哪个网址? 上

 http://localhost:8080/

我收到错误:

 This site can’t be reachedlocalhost 
 refused to connect.

我尝试设置一些格式代码: 但我不确定他们叫什么:

your container's external mapped port

但是我应该使用哪种语法?

修改 block #3

正在运行

http://localhost:8080/ 

在我的浏览器中出现错误:

This site can’t be reached
localhost refused to connect.


Checking logs I see : $ docker logs --tail=40 vtasks_web
<s> [webpack.Progress] 87% after chunk id optimization
<s> [webpack.Progress] 87% record modules
<s> [webpack.Progress] 87% record modules RecordIdsPlugin
<s> [webpack.Progress] 87% record chunks
<s> [webpack.Progress] 87% record chunks RecordIdsPlugin
<s> [webpack.Progress] 88% hashing
<s> [webpack.Progress] 88% after hashing
<s> [webpack.Progress] 88% after hashing HotModuleReplacementPlugin
<s> [webpack.Progress] 89% record hash
<s> [webpack.Progress] 89% module assets processing
<s> [webpack.Progress] 90% chunk assets processing
<s> [webpack.Progress] 90% additional chunk assets processing
<s> [webpack.Progress] 90% additional chunk assets processing HotModuleReplacementPlugin
<s> [webpack.Progress] 91% recording
<s> [webpack.Progress] 91% recording HotModuleReplacementPlugin
<s> [webpack.Progress] 92% additional asset processing
<s> [webpack.Progress] 92% chunk asset optimization
<s> [webpack.Progress] 93% after chunk asset optimization
<s> [webpack.Progress] 93% asset optimization
<s> [webpack.Progress] 94% after asset optimization
<s> [webpack.Progress] 94% after seal
<s> [webpack.Progress] 95% emitting
<s> [webpack.Progress] 95% emitting HtmlWebpackPlugin
<s> [webpack.Progress] 95% emitting CopyPlugin
<s> [webpack.Progress] 98% after emitting
<s> [webpack.Progress] 98% after emitting CopyPlugin
 DONE  Compiled successfully in 11638ms12:10:15 PM

<s> [webpack.Progress] 100% 


  App running at:
  - Local:   http://localhost:8080/ 

  It seems you are running Vue CLI inside a container.
  Access the dev server via http://localhost:<your container's external mapped port>/

  Note that the development build is not optimized.
  To create a production build, run npm run build.

但是在我的 _Docker/docker-compose.yml 中我有选项:

ports:
    - "8088:80"

实际上我有几个 Docker 项目,例如:

$ docker ps                                                                                                                                                
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                          PORTS                    NAMES                                                                    
99bc972f0840        ebiven/vue-cli      "docker-php-entrypoi"   32 hours ago        Up 8 minutes                    0.0.0.0:8088->80/tcp     vtasks_web                                                             
fcb6d879effa        adminer             "entrypoint.sh docke"   2 weeks ago         Up 8 minutes                    0.0.0.0:8089->8080/tcp   vtasks_adminer                                                         
b0b81c6587bb        mysql:5.7.28        "docker-entrypoint.s"   2 weeks ago         Restarting (1) 47 seconds ago                            vtasks_db       

端口问题令人困惑......

谢谢!

最佳答案

您需要为 node_modules 目录和 dist 目录创建一个“命名卷”,以便这些目录不会被您的绑定(bind)挂载覆盖(此东西:${APP_PATH_HOST}:${APP_PTH_CONTAINER})。

This教程解释得更好:

The second is a named volume, node_modules. When Docker runs the npm install instruction listed in the application Dockerfile, npm will create a new node_modules directory on the container that includes the packages required to run the application. The bind mount we just created will hide this newly created node_modules directory, however. Since node_modules on the host is empty, the bind will map an empty directory to the container, overriding the new node_modules directory and preventing our application from starting. The named node_modules volume solves this problem by persisting the contents of the /home/node/app/node_modules directory and mounting it to the container, hiding the bind.

为此,您可以在 web 服务下添加两个新卷,并将它们添加到 docker-compose.yml 底部的 volumes也是:

web:
    volumes:
        - ${APP_PATH_HOST}:${APP_PTH_CONTAINER}
        - node_modules:${APP_PTH_CONTAINER}/node_modules
        - dist:${APP_PTH_CONTAINER}/dist

volumes:
  node_modules:
  dist:

我使用以下文件让 VueCLI 在 Docker 中工作:

Dockerfile:

FROM node:lts-alpine

# install simple http server for serving static content
RUN npm install --quiet -g http-server

# install the vue-cli
RUN npm install --quiet --global \
      @vue/cli

# create app directory
RUN mkdir /app

# copy both 'package.json' and 'package-lock.json' (if available)
COPY package*.json /app/

# copy project files and folders to the current working directory (i.e. 'app' folder)
COPY . /app/

# make the 'app' folder the current working directory
WORKDIR /app

# install project dependencies
RUN npm install

# build the app
RUN npm run build

docker-compose.yml:

version: '3'

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/app
      - node_modules:/app/node_modules
      - dist:/app/dist
    ports:
      - "8080:8080"
    # uncomment command below to run development version
    # command: "npm run serve"
    command: "http-server dist"

volumes:
  node_modules:
  dist:

我运行了docker-compose up,然后我的应用程序可以在http://localhost:8080/上使用。

第一次更新 Dockerfile 后,请务必重建容器,例如docker-compose up --build

关于docker - docker下运行vue/cli app simple index.html打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59491055/

相关文章:

vue.js - 如何异步验证 Vuetify 文本字段?

ruby-on-rails - Bundler 无法在任何资源中找到 rake

vue.js - 测试使用过滤器的 vue 组件

javascript - 测试元素是否存在 Vuejs, Karma, Mocha

javascript - Graphql Apollo Server + Vue => 图片上传

javascript - 如何从我的 vue 路由器中的商店/状态访问此变量?

docker - curl : (56) Recv failure: Connection reset by peer - Docker

docker - 如何配置 Docker 容器以从 ESX 上运行的 dhcp 服务器获取 DHCP IP/s

Docker 撰写抛出 - adduser : group 'www-data' in use

javascript - 为什么这个 Vuejs "Hello World"示例无法在我的计算机上呈现?