简体   繁体   English

Docker 无法创建 nginx

[英]Docker can't create nginx

I try create docker container container with nginx.我尝试使用 nginx 创建 docker 容器容器。 (Windows 10 64 bit) (Windows 10 64 位)

  site:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    networks:
      - laravel

And got error:并得到错误:

ERROR: for site  Cannot start service site: OCI runtime create failed: container_linux.go:344: starting container process caused "process_linux.go:424: container init caused \"rootfs_linux.go:58: mounting \\\"/host_mnt/e/laravel-docker-new/docker-compose-laravel/nginx
/default.conf\\\" to rootfs \\\"/var/lib/docker/overlay2/cc15f98e2704e4be5aa3d26ab7a216f3c301ebfcf678c951c2ae5aefad0f794a/merged\\\" at \\\"/var/lib/docker/overlay2/cc15f98e2704e4be5aa3d26ab7a216f3c301ebfcf678c951c2ae5aefad0f794a/merged/etc/nginx/conf.d/default.conf\\
\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

How fix this?如何解决这个问题?

Update.更新。
Try conf试试看

 site:

image: nginx:stable-alpine
container_name: nginx
ports:
  - "8080:80"
volumes:
  - ./src:/var/www/html/public
   ./nginx/default.conf COPY /etc/nginx/conf.d/default.conf
depends_on:
  - php
  - mysql
networks:
  - laravel

But how use artisan?但是如何使用工匠?

artisan: build: context: .工匠:构建:上下文:。 dockerfile: php.dockerfile container_name: artisan volumes: -./src:/var/www/html/public depends_on: - mysql working_dir: /var/www/html/public entrypoint: [ '/var/www/html/public/artisan'] networks: - laravel dockerfile: php.dockerfile container_name: artisan volumes: -./src:/var/www/html/public depends_on: - mysql working_dir: /var/www/html/public entrypoint: [ '/var/www/html/public/工匠'] 网络:-laravel

And how I can got access to laravel?以及如何访问 laravel?

Files structure文件结构

E:/laravel-docker-new/docker-compose-laravel E:/laravel-docker-new/docker-compose-laravel

.github
nginx
nginx/default.conf
src
src/app
src/bootstrap
src/config
src/database
src/public
src/resources
src/routes
src/storage
src/test
src/tests
src/vendor
src/.editorconfig
src/.env
src/.env.example
src/.gitattributes
src/.gitignore
src/.styleci.yml
src/composer.phar

Do you still encounter the error if you omit this line?如果省略此行,是否仍会遇到错误?

- ./nginx/default.conf:/etc/nginx/conf.d/default.conf

You may be better off using the Docker COPY directive to place that file into the desired location, since if you are trying to use volume mounts as a means to reflect live changes in your code without the need to rebuild the image this will not work as expected for Nginx configuration files, as this would require an Nginx restart to take effect.您最好使用 Docker COPY指令将该文件放置到所需位置,因为如果您尝试使用卷挂载来反映代码中的实时更改而无需重建图像,这将不起作用预计 Nginx 配置文件,因为这需要 Nginx 重新启动才能生效。

There is some strange error fallback if the mounted file doesn't exist on the host.如果主机上不存在挂载的文件,则会出现一些奇怪的错误回退。 Is there really an nginx folder next to the compose file containing a default.conf?在包含 default.conf 的撰写文件旁边真的有一个 nginx 文件夹吗? Also, using relative paths doesn't work in all cases.此外,并非在所有情况下都使用相对路径。 Please make sure the file really exists on the host system and maybe try passing an absolute path using $pwd (although I'm pretty sure relative paths work with compose - they don't with plain docker run).请确保该文件确实存在于主机系统上,并可能尝试使用 $pwd 传递绝对路径(尽管我很确定相对路径适用于 compose - 它们不适用于普通的 docker 运行)。

Check this answer for more details: How to mount a single file in a volume查看此答案以获取更多详细信息: 如何在卷中安装单个文件

Based on your updated question I assume the problem is caused by your sources being put on the E:\ drive which is not available to the virtual machine running your containers by default.根据您更新的问题,我认为问题是由于您的源被放置在 E:\ 驱动器上,默认情况下运行您的容器的虚拟机无法使用该驱动器。 You might need to go to the settings of your docker for windows and add this path to the available shared folders.您可能需要将 go 设置为 windows 的 docker 的设置,并将此路径添加到可用的共享文件夹。

For details see: https://docs.docker.com/docker-for-windows/troubleshoot/#volume-mounting-requires-shared-folders-for-linux-containers有关详细信息,请参阅: https://docs.docker.com/docker-for-windows/troubleshoot/#volume-mounting-requires-shared-folders-for-linux-containers

In my case it was failing to mount the folder because docker expects an absolute path:在我的情况下,它无法挂载文件夹,因为 docker 需要一个绝对路径:

$ sudo docker run -v "./nginx.conf:/etc/nginx/nginx.conf" -p 8081:80 -d nginx
docker: Error response from daemon: create ./nginx.conf: "./nginx.conf" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
See 'docker run --help'.
$ sudo docker run -v /home/ubuntu/nginx/nginx.conf:/etc/nginx/nginx.conf -p 8081:80 -d 
nginx57f95da476a6794a26530b6140066b3362c166c3040eb8d3be0aece916441402

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

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