Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker. Create directory in container after docker-compose up and give it r/w permissions

How to create another /tmp directory, for example, in the same container and give it r/w permissions?

docker-compose.yml:

nginx:
    image: nginx
    ports:
      - 80:80
    volumes:
      - ./volumes/nginx/conf.d:/etc/nginx/conf.d
    command: nginx -g "daemon off;"
    networks:
      - network
like image 200
Bulat Motygullin Avatar asked Sep 18 '25 04:09

Bulat Motygullin


2 Answers

You can create a directory or perform any other action by defining it in a Dockerfile. In the same directory as your docker-compose.yml create a Dockerfile:

touch Dockerfile

Add to your Dockerfile following line:

RUN mkdir /tmp2
RUN chmod 755 /tmp2

to the docker-compose.yaml add build information:

nginx:
      image: nginx
      build: .
      ports:
        - 80:80
      volumes:
        - ./volumes/nginx/conf.d:/etc/nginx/conf.d
      command: nginx -g "daemon off;"
      networks:
        - network
like image 96
dongi Avatar answered Sep 20 '25 18:09

dongi


Simply just add a volume with the directory you wish to create and it would be created automatically during startup

nginx:
    image: nginx
    ports:
      - 80:80
    volumes:
      - ./volumes/nginx/conf.d:/etc/nginx/conf.d
      - ./volumes/hosted-direcotry/hosted-sub-direcotry:/etc/created-direcotry/created-sub-directory/
    command: nginx -g "daemon off;"
    networks:
      - network
like image 31
DaviesTobi alex Avatar answered Sep 20 '25 19:09

DaviesTobi alex