简体   繁体   English

在 Docker 中,如何从本地目录复制文件,以便我可以将这些文件复制到我的 Docker 容器中?

[英]In Docker, how do I copy files from a local directory so that I can then copy those files into my Docker container?

I'm using Docker我正在使用 Docker

Docker version 19.03.8, build afacb8b

I have the following docker-compose.yml file...我有以下 docker-compose.yml 文件...

version: "3.2"
services:

  sql-server-db:
    build: ./
    container_name: sql-server-db
    image: microsoft/mssql-server-linux:2017-latest
    ports:
      - "1433:1433"
    environment:
      SA_PASSWORD: "Password1!"
      ACCEPT_EULA: "Y"

and here is the Docker file it uses to build...这是用于构建的 Docker 文件...

FROM microsoft/mssql-server-linux:latest
  
# Create work directory
RUN mkdir -p /usr/work

WORKDIR /usr/work

# Copy all scripts into working directory
COPY . /usr/work/

# Grant permissions for the import-data script to be executable
RUN chmod +x /usr/work/import-data.sh

EXPOSE 1433

CMD /bin/bash ./entrypoint.sh

On my local machine, I have some files in a "../../scripts/myproject/*.sql" directory (the ".." are relative to the directory where my docker-compose.yml file is stored).在我的本地机器上,我在“../../scripts/myproject/*.sql”目录中有一些文件(“..”是相对于我的 docker-compose.yml 文件存储的目录)。 Is there a way I can run "docker-compose up" and have those files copied into a directory from which I can then copy them into the container's "/usr/work" directory?有没有办法可以运行“docker-compose up”并将这些文件复制到一个目录中,然后我可以将它们复制到容器的“/usr/work”目录中?

There are 2 ways to solve this, with one being easier than the other, but both have use cases.有两种方法可以解决这个问题,一种比另一种更容易,但都有用例。

The easy way简单的方法

You could mount the directory directly to the container through the docker-compose like this:您可以通过docker-compose将目录直接挂载到容器,如下所示:

version: "3.2"
services:

  sql-server-db:
    build: ./
    container_name: sql-server-db
    image: microsoft/mssql-server-linux:2017-latest
    ports:
      - "1433:1433"
    environment:
      SA_PASSWORD: "Password1!"
      ACCEPT_EULA: "Y"
    volumes:
      - ../../scripts/myproject:/path/to/dir

Note the added volumes compared to the yaml in your question.请注意与您的问题中的 yaml 相比增加的volumes This will mount the myproject directory to /path/to/dir within the container.这会将myproject目录挂载到容器内的/path/to/dir中。 What this will also mean is that if the sql-server-db container writes to any of the files in /path/to/dir , then the file in myproject on the host machine will also change, since the files are mounted.这也意味着如果sql-server-db容器写入/path/to/dir中的任何文件,那么主机上myproject中的文件也将更改,因为这些文件已挂载。

The less easy way不太容易的方法

You could copy the files directly during the build of the image.您可以在构建映像期间直接复制文件。 This is a little bit harder, since the build stage of docker doesn't allow the copying of parent directories unless you add some extra arguments.这有点困难,因为 docker 的构建阶段不允许复制父目录,除非您添加一些额外的 arguments。 What needs to happen is that you set the context of the build stage to a different directory than the current directory.需要发生的是将构建阶段的context设置为与当前目录不同的目录。 The context determines which files are sent to the build stage. context确定将哪些文件发送到构建阶段。 This is the same directory as the directory the Dockerfile resides in by default.这与Dockerfile默认所在的目录相同。

To take this approach, you need the following in your docker-compose.yml :要采用这种方法,您需要docker-compose.yml中的以下内容:

version: "3.2"
services:

  sql-server-db:
    build:
      context: ../..
      dockerfile: path/to/Dockerfile # Here you should specify the path to your Dockerfile, this is a relative path from your context
    container_name: sql-server-db
    image: microsoft/mssql-server-linux:2017-latest
    ports:
      - "1433:1433"
    environment:
      SA_PASSWORD: "Password1!"
      ACCEPT_EULA: "Y"

So above the context is now ../.. so that you are able to copy files two directories above.所以上面的context现在是../..这样您就可以将文件复制到上面的两个目录中。 You can then copy the myproject directory in your Dockerfile like this:然后,您可以像这样复制 Dockerfile 中的myproject目录:

FROM microsoft/mssql-server-linux:latest

COPY ./scripts/myproject /myfiles

The advantage of this approach is that the files are copied instead of being mounted, so the docker container can write whatever it wants to these files, without affecting the host machine.这种方法的优点是文件被复制而不是被挂载,因此 docker 容器可以向这些文件写入任何它想要的内容,而不会影响主机。

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

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