简体   繁体   English

为什么 Mac OSX 在使用 Docker Desktop for Mac 时仍然不区分大小写

[英]Why does Mac OSX still exhibit case insensitivity when using Docker Desktop for Mac

I am using Docker Desktop for Mac to containerize a basic LAMP stack in my local environment, then periodically deploying a simple MVC application to a shared Linux server using a Bitbucket pipeline.我正在使用 Docker Desktop for Mac 将本地环境中的基本 LAMP 堆栈容器化,然后使用 Bitbucket 管道定期将简单的 MVC 应用程序部署到共享 Linux 服务器。

Everything is behaving as expected apart from the router.除了路由器之外,一切都按预期运行。 When in prod, the autoload fails because the Linux environment distinguishes between the routes, App/Controllers/Posts and app/controllers/posts, for example.在 prod 中,自动加载失败,因为 Linux 环境区分路由,例如 App/Controllers/Posts 和 app/controllers/posts。 I realise that for PSR-4 autoloading to work, the case of each folder / namespace and file / class have to match, and clearly the reason it's failing in prod is because of the live environment's case sensitivity highlighting this mismatch.我意识到要使 PSR-4 自动加载工作,每个文件夹/命名空间和文件/类的大小写必须匹配,显然它在生产中失败的原因是因为实时环境的大小写敏感性突出了这种不匹配。

However, the obvious reason for using Docker is to avoid these differing behaviours in local and prod environments.然而,使用 Docker 的明显原因是避免在本地和生产环境中出现这些不同的行为。 Is it definitely the case that using Docker on a Mac won't exhibit case sensitivity, even when in a container based on an Ubuntu image, for instance, so that I can pick up on these issues locally without having to test in prod?例如,在基于 Ubuntu 映像的容器中,即使在基于 Ubuntu 映像的容器中,在 Mac 上使用 Docker 也绝对不会表现出区分大小写的情况,这样我就可以在本地解决这些问题而无需在 prod 中进行测试吗? An issue raised on Github ( https://github.com/docker/for-mac/issues/320 back in 2016) suggests that this is not possible due to Mac's "osxfs”. However, when looking at the Docker for Mac docs ( https://docs.docker.com/docker-for-mac/ ) it would appear that it should pick up on this; specifically: Github 上提出的一个问题( https://github.com/docker/for-mac/issues/320早在 2016 年)表明,由于 Mac 的“osxfs”,这是不可能的。但是,当查看 Docker for Mac 文档时( https://docs.docker.com/docker-for-mac/ )它似乎应该对此有所了解;特别是:

By default, Mac file systems are case-insensitive while Linux is case-sensitive.默认情况下,Mac 文件系统不区分大小写,而 Linux 区分大小写。 On Linux, it is possible to create 2 separate files: test and Test, while on Mac these filenames would actually refer to the same underlying file.在 Linux 上,可以创建 2 个单独的文件:test 和 Test,而在 Mac 上,这些文件名实际上指的是同一个底层文件。 This can lead to problems where an app works correctly on a Mac (where the file contents are shared) but fails when run in Linux in production (where the file contents are distinct).这可能会导致应用程序在 Mac 上正常运行(共享文件内容)但在 Linux 生产环境中运行时失败(文件内容不同)的问题。 To avoid this, Docker Desktop insists that all shared files are accessed as their original case .为避免这种情况,Docker Desktop 坚持将所有共享文件作为其原始 case 进行访问 Therefore, if a file is created called test, it must be opened as test...因此,如果创建了一个名为 test 的文件,则必须将其打开为 test...

(bold added by me) (粗体是我加的)

Does this not indicate that case sensitivity should be exhibited, or am I reading the docs incorrectly?这是否表示应该展示区分大小写,或者我是否错误地阅读了文档? For the record, here is my Dockerfile and docker-compose file:作为记录,这是我的 Dockerfile 和 docker-compose 文件:

FROM php:7.4-apache

RUN a2enmod rewrite

RUN apt-get update
RUN apt-get install git -y
RUN apt-get install -y libzip-dev zip

RUN docker-php-ext-install pdo pdo_mysql

RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer

WORKDIR /var/www/developmental

COPY .docker/vhost.conf /etc/apache2/sites-available/000-default.conf
version: "3.8"

services:
  php:
    build:
      context: .
      dockerfile: ./.docker/Dockerfile
    ports:
      - 8080:80
    volumes:
      - .:/var/www/developmental

  db:
    image: mysql/mysql-server
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - 3308:3306
    environment:
      MYSQL_ROOT_PASSWORD: "${DB_ROOT_PASSWORD}"
    volumes:
      - db-data:/var/lib/mysql
      - ./data:/docker-entrypoint-initdb.d

volumes:
  db-data:

Many thanks in advance (and go easy - I'm a Docker rookie and this is my first Stackoverflow question!)提前非常感谢(并且放轻松 - 我是 Docker 新手,这是我的第一个 Stackoverflow 问题!)

Since you're bind-mounting a directory from the host into the container, it comes along with any irregularities the host filesystem might have;由于您将目录从主机绑定安装到容器中,因此主机文件系统可能存在任何违规行为; for example the paragraph you quoted about case-insensitivity on MacOS.例如你引用的关于 MacOS 不区分大小写的段落。

It's often better to make a Docker image be self-contained: write a Dockerfile that COPY all of the required application code into the image, and avoid this bind mount entirely.使 Docker 映像成为自包含的通常更好:编写一个 Dockerfile,将所有必需的应用程序代码COPY到映像中,并完全避免这种绑定安装。 If you do this, then the application code will be only in the case-sensitive Linux filesystem.如果你这样做,那么应用程序代码将只在区分大小写的 Linux 文件系统中。 If you need to correct the case of filenames you can RUN mv foo Foo for example.如果您需要更正文件名的大小写,您可以RUN mv foo Foo例如。

# In the Dockerfile
...
WORKDIR /var/www/developmental
COPY . .
# In docker-compose.yml
services:
  php:
    build:
      context: .
      dockerfile: ./.docker/Dockerfile
    ports:
      - 8080:80
    # and remove `volumes:`

One way to solve this on modern Mac APFS based systems is to create a case-sensitive volume where you store you store your docker volume data.在基于 Mac APFS 的现代系统上解决此问题的一种方法是创建一个区分大小写的卷,您可以在其中存储您的 docker 卷数据。 The following will create a new volume that which is case-sensitive (APFSX) that you can use for your volume data.下面将创建一个区分大小写 (APFSX) 的新卷,您可以将其用于卷数据。 Note: You should do this in a fresh directory.注意:您应该在新目录中执行此操作。

mkdir <docker volume directory>
sudo diskutil apfs addVolume disk1 APFSX docker -mountpoint <docker volume directory>
sudo chown -R $(id -u):$(id -g) <docker volume directory>

Ref: Feature to opt-in into a case sensitive file-system (osxfs) on a volume mount参考: 在卷挂载上选择加入区分大小写的文件系统 (osxfs) 的功能

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

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