Bulat Motygullin
Bulat Motygullin

Reputation: 93

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

Upvotes: 4

Views: 26877

Answers (3)

DaviesTobi alex
DaviesTobi alex

Reputation: 670

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

Upvotes: 6

danielarend
danielarend

Reputation: 1427

If you are using only docker-compose without Dockerfile, can be done this way:

You can get into container, like this:

docker exec -ti $(docker ps --filter name='nginx' --format "{{ .ID }}")

Then, inside the container, you can run:

mkdir /tmp2
chmod 755 /tmp2

Upvotes: 4

dongi
dongi

Reputation: 428

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

Upvotes: 6

Related Questions