简体   繁体   中英

Run bash commands in docker-compose

I am using default image and my requirement is to run a few Linux commands when I run docker-compose file. Os is redhat. This is my docker-compose file

version: '3.4'

services:
  rstudio-package-manager:
    image: 'rstudio/rstudio-package-manager:latest'
    restart: always
    volumes:
      - '$PWD/rstudio-pm.gcfg:/etc/rstudio-pm/rstudio-pm.gcfg'
    command: bash -c mkdir "/tmp/hello"
    ports:
      - '4242:4242'

This is the error:

rstudio-package-manager_1  | mkdir: missing operand
rstudio-package-manager_1  | Try 'mkdir --help' for more information.

Any help would be appreciated

EDIT

I have to run a few commands after the container starts. It can be added as a bash script too. For that, I tried this

version: '3.4'

services:
  rstudio-package-manager:
    privileged: true
    image: 'rstudio/rstudio-package-manager:latest'
    restart: always
    environment:
      - RSPM_LICENSE=1212323123123123
    volumes:
      - './rstudio-pm.gcfg:/etc/rstudio-pm/rstudio-pm.gcfg'
      - './init.sh:/usr/local/bin/init.sh'

    command:
    - init.sh
    ports:
      - '4242:4242'

Inside init.sh is this

alias rspm='/opt/rstudio-pm/bin/rspm'
rspm create repo --name=prod-cran --description='Access CRAN packages'
rspm subscribe --repo=prod-cran --source=cran

And that also didn't work. Can anyone helpme out?

You are passing 3 arguments to bash :

  1. -c
  2. mkdir
  3. /tmp/hello

but you need to pass only two:

  1. -c
  2. mkdir /tmp/hello

In other words: -c expects a single "word" to follow it. Anything after that is considered a positional parameter.

Therefore:

bash -c 'mkdir /tmp/hello'

Based on your edit, it doesn't sound like you want to change the command when running a container, but you want to create a derived image based on an existing one.

You want a Dockerfile which modifies an existing image and add your files/applies your modifications.

docker-compose.yml :

version: '3.4'

services:
  rstudio-package-manager:
    privileged: true
    build:
      context: folder_with_dockerfile
    image: your_new_image_name
    restart: always
    environment:
      - RSPM_LICENSE=1212323123123123
    volumes:
      - './rstudio-pm.gcfg:/etc/rstudio-pm/rstudio-pm.gcfg'
      - './init.sh:/usr/local/bin/init.sh'
    ports:
      - '4242:4242'

folder_with_dockerfile/Dockerfile :

FROM rstudiol/rstudio-package-manager:latest

RUN alias rspm='/opt/rstudio-pm/bin/rspm' && \
  rspm create repo --name=prod-cran --description='Access CRAN packages' && \
  rspm subscribe --repo=prod-cran --source=cran
RUN commands in next layer
RUN commands in third layer

Then build the new image with docker-compose build and start normally. You could also run docker-compose up --build to build & run.

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