使用编译语言(打字稿)在开发环境中使用docker

时间:2019-09-10 07:54:39

标签: typescript docker docker-compose dockerfile

我有10个用打字稿写的微服务,我想运行 docker-compose up --build将会:

  • 构建每个容器

  • 编译打字稿

  • 使用pm2运行每个微服务

我希望能够使用“ vscode”在主机上编辑代码,并查看容器上的更改

我试图将文件复制到docker文件上,然后进行构建。 但是当我挂载该卷时,它会覆盖我的代码,因此无法使用pm2

运行它

docker compose

version: '3'
services:
  web_service_1:
    container_name: m1_container
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - 3014:3014
    volumes: 
      - ./:/source/m1

docker文件

FROM ubuntu:18.04

RUN mkdir -p /source/m1 

RUN apt-get update && apt-get install -y sudo &&  apt-get install -y curl

#install node js
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get install -y nodejs

#typescript
RUN npm install -g typescript@3.2.2
#pm2 3.2.4
RUN  npm install pm2@3.2.4 -g
#text editor
RUN sudo apt-get install nano
# RUN sudo apt-get install vim

#copy project files
COPY ./m1 /source/m1

#create dist folder
RUN mkdir dist

#build the typescript
RUN cd /source/m1 && tsc

#run with pm2
CMD ["pm2-runtime", "/source/m1/ecosystem.config.js"]

有什么解决方案可以避免对每个容器构建和运行代码使用ssh?

1 个答案:

答案 0 :(得分:0)

您可以在容器内运行任何命令而无需进入容器。使用“ docker-compose exec”。例如,您可以运行“ docker-compose exec web_service_1 apt-get update”,它将在容器内运行“ apt-get update”。