Vscode not detecting git folder after build – Docker

huangapple go评论65阅读模式
英文:

Vscode not detecting git folder after build - Docker

问题

I am new to Docker and buidling a docker-compose file to setup my nodejs dev environment from a single file. Idea is to give a docker-compose file to a developer from where they can simply spin up the containers and start development.

When I docker-compose up, the application is built and runs with success. But my problem is when I open it in dev containers in Vscode, it fails to recognize as a git repo.

.dockerignore

node_modules
npm-debug.log

Dockerfile

FROM node:16.15.0 as develop-stage
WORKDIR /code
COPY package.json .
# Using package-lock file to fix the version of installed modules.
COPY package-lock.json .
RUN npm install
COPY . .
EXPOSE 9608
CMD ['nodemon' 'index.js']

Docker-compose

version: '3.8'
services:
  my-service:
    container_name: my-service
    build: 
      context: 'https://username:token@bitbucket.org/abc/test-repo'
      dockerfile: Dockerfile
    volumes:
      - my-data:/code
    command: nodemon index.js
    networks:
      - my-network
    expose:
      - 9608
    ports:
      - 9608:9608
    restart: on-failure
volumes:
  my-data:
    external: false
networks:
  my-network:
    external: false
英文:

I am new to Docker and buidling a docker-compose file to setup my nodejs dev environment from a single file. Idea is to give a docker-compose file to a developer from where they can simply spin up the containers and start development.

When I docker-compose up, the application is built and runs with success. But my problem is when I open it in dev containers in Vscode, it fails to recognize as a git repo.

.dockerignore

node_modules
npm-debug.log

Dockerfile

FROM node:16.15.0 as develop-stage
WORKDIR /code
COPY package.json .
# Using package-lock file to fix the version of installed modules.
COPY package-lock.json .
RUN npm install
COPY . .
EXPOSE 9608
CMD ['nodemon' 'index.js']

Docker-compose

version: '3.8'
services:
  my-service:
    container_name: my-service
    build: 
      context: 'https://username:token@bitbucket.org/abc/test-repo'
      dockerfile: Dockerfile
    volumes:
      - my-data:/code
    command: nodemon index.js
    networks:
      - my-network
    expose:
      - 9608
    ports:
      - 9608:9608
    restart: on-failure
volumes:
  my-data:
    external: false
networks:
  my-network:
    external: false

答案1

得分: 1

A docker-compose context 定义了一个 构建上下文,它可以使用一个 Git 仓库 URL

> 当 URL 参数指向 Git 仓库的位置时,该仓库将充当构建上下文。
构建器会递归地拉取该仓库及其子模块。执行浅克隆,因此仅拉取最新的提交,而不是整个历史记录。
>
> 首先将仓库拉取到主机上的临时目录。成功后,将该目录作为上下文发送给守护程序

换句话说,您的上下文代表远程 Git 仓库的内容,而不代表实际的(克隆的)Git 仓库。

  • 上下文是发送到 Docker 守护程序以构建镜像的文件集。
  • 上下文的来源可以是本地目录或 Git URL。

如果您需要在镜像中获取一个仓库,您的 Dockerfile/docker-compose 将会 克隆 Git 仓库,而不是使用上下文。

英文:

A docker-compose context defines a build context, which can use a Git repository URL

> When the URL parameter points to the location of a Git repository, the repository acts as the build context.
The builder recursively pulls the repository and its submodules. A shallow clone is performed and therefore pulls down just the latest commits, not the entire history.
>
> A repository is first pulled into a temporary directory on your host. After that succeeds, the directory is sent to the daemon as the context.

In other words, your context represent the content of the remote Git repository, it does not represent an actual (cloned) Git repository.

  • The context is the set of files that are sent to the docker daemon for building an image.
  • The source of the context can be a local directory or a git URL.

If you had to get a repository inside your image, your Dockerfile/docker-compose would clone the Git repository, not use a context.

答案2

得分: 0

您的.git文件夹不在要复制到Docker镜像的根目录内。根据您的文件夹结构设置方式,它可能位于根目录的上层一个或两个文件夹内。您可以通过执行 cd .. && ls -a 直到找到它。然后,使用明确的命令 COPY ../.git . 将其添加到Docker镜像中。

英文:

Your .git folder is not within the root directory that's being copied over to the docker image. Depending on how your folder structure is set up it may be a folder or 2 above. You can find it by cd .. && ls -a until found. Then use an explicit COPY ../.git . to add it into the Docker image.

huangapple
  • 本文由 发表于 2023年3月9日 13:38:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定