简体   繁体   English

在 docker-compose.yml 文件中创建目录

[英]Create a directory in docker-compose.yml file

I have a docker-compose yml that creates a sftp image on my docker.我有一个 docker-compose yml,它在我的 docker 上创建一个 sftp 图像。 I'd like to write a script in the yml file as I want directories to be created automatically as soon as I run the docker-compose.yml.我想在 yml 文件中编写一个脚本,因为我希望在运行 docker-compose.yml 后立即自动创建目录。

Here's my yml file;这是我的 yml 文件;

sftp:
  image: atmoz/sftp
  volumes:
    - C:\tmp\sftp:/home/foo/upload
  ports:
    - "2222:22"
  command: username:password:1001

Is there a way to write mkdir and chmod in this file?有没有办法在这个文件中写入mkdirchmod

You do not need to create and set mod manually, just pass the directory name to CMD and the entrypoint will create one for you.您不需要手动创建和设置 mod,只需将目录名称传递给CMD ,入口点将为您创建一个。 Here is the simplest example.这是最简单的例子。

Define users in (1) command arguments , (2) SFTP_USERS environment variable or (3) in file mounted as /etc/sftp/users.conf (syntax: user:pass[:e][:uid[:gid[:dir1[,dir2]...]]]... , see below for examples)在 (1) command arguments 、 (2) SFTP_USERS环境变量或 (3) 在挂载为/etc/sftp/users.conf的文件中定义用户(语法: user:pass[:e][:uid[:gid[:dir1[,dir2]...]]]... ,请参阅下面的示例)

using docker-compose使用 docker-compose

sftp:
  image: atmoz/sftp
  command: username:password:100:100:upload

it will create user name username and directory upload under /home/username You can verify this using它将在/home/username下创建用户名username和目录上传您可以使用验证这一点

docker exec -it --user username <container_id> bash -c "ls /home/username"

if you want to access upload files from host just add mounting in your docker-compose如果您想从主机访问上传文件,只需在 docker-compose 中添加安装

sftp:
  image: atmoz/sftp
  command: username:password:100:100:upload
  volumes:
        - /host/upload:/home/username/upload

Examples例子

Simplest docker run example最简单的 docker 运行示例

docker run -p 22:22 -d atmoz/sftp foo:pass:::upload

User "foo" with password "pass" can login with sftp and upload files to a folder called "upload".密码为“pass”的用户“foo”可以使用 sftp 登录并将文件上传到名为“upload”的文件夹。 No mounted directories or custom UID/GID.没有安装目录或自定义 UID/GID。 Later you can inspect the files and use --volumes-from to mount them somewhere else (or see next example).稍后您可以检查文件并使用 --volumes-from 将它们挂载到其他地方(或参见下一个示例)。

see the offical documentation官方文档

try this, and append your command to the command list试试这个,然后 append 你的命令到command列表

version: "3.7"
services:
  sftp:
    image: atmoz/sftp
    volumes:
      -  C:\tmp\sftp:/home/foo/upload
    ports:
      - "2222:22"
    command: ["/bin/bash", "-c", "mkdir -p /home/foo/upload/test1 && chmod 755 /home/foo/upload/test1 && username:password:1001"]

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

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