简体   繁体   中英

Create a directory in docker-compose.yml file

I have a docker-compose yml that creates a sftp image on my docker. 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.

Here's my yml file;

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?

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. 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)

using 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

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

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

Examples

Simplest docker run example

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". No mounted directories or custom UID/GID. Later you can inspect the files and use --volumes-from to mount them somewhere else (or see next example).

see the offical documentation

try this, and append your command to the command list

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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