bash - 在 docker entrypoint sh 脚本中复制文件夹会产生错误的文件夹名称
问题描述
我有一个基于 docker 的小型项目,我想将本地 .ssh 文件夹复制到其中,然后修改文件夹权限。
- 在 docker-compose.yml 中,我将 .ssh 文件夹挂载到容器的 tmp/.ssh 文件夹中。
- 然后在启动时我将 tmp/.ssh 的内容复制到 root/.ssh 并更改文件夹权限。 (这一步是必要的,因为当我通过 composer 安装我的 PHP 包时,它会抱怨文件夹权限太开放)
不幸的是,我的 .sh 脚本有问题,因为它在 /root 中创建的文件夹名称如下所示:'.ssh'$'\r'
码头工人-compose.yml:
version: '3.7'
services:
sandbox:
build:
context: ./
dockerfile: ./Dockerfile
volumes:
- .:/var/www/html
- ~/.ssh:/tmp/.ssh
restart: always
ports:
- "8060:80"
environment:
- APACHE_RUN_DIR=/var/run/apache2
- APACHE_RUN_USER=#1000
- APACHE_RUN_GROUP=#1000
- APACHE_LOG_DIR=/var/log/apache2
- APACHE_PID_FILE=/tmp/apache2.pid
- APACHE_LOCK_DIR=/var/lock/apache2
- JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Dockerfile:
FROM php:7.4.1-apache-buster
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
RUN a2enmod rewrite
COPY vhost-configs/vhost.local.conf /etc/apache2/sites-available/
COPY vhost-configs/vhost.local.conf /etc/apache2/sites-enabled/
RUN rm /etc/apache2/sites-enabled/000-default.conf
EXPOSE 80
COPY docker-entrypoint.sh /bin/docker-entrypoint.sh
RUN chmod 777 /bin/docker-entrypoint.sh && chmod +x /bin/docker-entrypoint.sh
CMD /usr/sbin/apache2 -DFOREGROUND
ENTRYPOINT ["sh", "/bin/docker-entrypoint.sh"]
码头入口点.sh:
#!/bin/sh -e
cp -R /tmp/.ssh /root/.ssh
chmod 700 /root/.ssh
chmod 400 /root/.ssh/id_rsa
exec "$@"
解决方案
docker-entrypoint.sh 文件是在 windows 上创建的,并包含 dos 行结尾。
我安装了 dos2unix 并转换了容器内的文件:
RUN apt-get install -y dos2unix
RUN dos2unix /bin/docker-entrypoint.sh
推荐阅读
- html - 使用 vw 和 vh 单位居中文本
- c# - 如何制作一个规则字符串,说明是否允许另一个字符串返回布尔值
- python - Dask Spectral Clustering ValueError: Input contains NaN, infinity or a value too large for dtype('float64')
- javascript - 保持下拉角度的选定值从一个组件到另一个组件 Angular
- c++ - 使用 shared_pointer 作为实例变量将 void* 指针转换回
- mongodb - Vue.JS 拖放
- laravel - 如何延迟 Laravel 作业队列
- angular - 仅从特定组件打开 mat-menu 时如何降低整个背景页面的不透明度?
- reactjs - 如何将 react-redux 与下一个 js 和类组件一起使用
- azure-ad-b2c - Blazor WASM B2C - AccessTokenNotAvailableException 上的重定向