【问题标题】:Copy folder in docker entrypoint sh script produces wrong folder name在 docker entrypoint sh 脚本中复制文件夹会产生错误的文件夹名称
【发布时间】:2021-02-26 03:51:44
【问题描述】:

我有一个基于 docker 的小型项目,我想将本地 .ssh 文件夹复制到其中,然后修改文件夹权限。

  1. 在 docker-compose.yml 中,我将 .ssh 文件夹挂载到容器的 tmp/.ssh 文件夹中。
  2. 然后在启动时我将 tmp/.ssh 的内容复制到 root/.ssh 并更改文件夹权限。 (这一步是必须的,因为当我通过composer安装我的PHP包时,它会抱怨文件夹权限太开放)

不幸的是,我的 .sh 脚本有问题,因为它在 /root 中创建的文件夹名称如下所示:'.ssh'$'\r'

docker-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"]

docker-entrypoint.sh:

#!/bin/sh -e
cp -R /tmp/.ssh /root/.ssh
chmod 700 /root/.ssh
chmod 400 /root/.ssh/id_rsa
exec "$@"

【问题讨论】:

  • 顺便说一句:sh (Bourne-shell) 通常不是 bash (Bourne-again shell)。
  • '.ssh'$'\r'你的文件可能有dos行结尾
  • 感谢@KamilCuk 的帮助。我使用:dos2unix /bin/docker-entrypoint.sh 然后脚本得到修复。将解决方案作为答案发布。

标签: bash docker dockerfile sh


【解决方案1】:

docker-entrypoint.sh 文件是在 windows 上创建的,包含 dos 行结尾。

我安装了dos2unix并转换了容器内的文件:

RUN apt-get install -y dos2unix


RUN dos2unix /bin/docker-entrypoint.sh

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 2022-07-26
    相关资源
    最近更新 更多