1

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?

6
  • 1
    This sounds like an XY problem. What is your goal, why are you trying to create a directory inside of your new docker container? Commented Dec 17, 2020 at 19:20
  • See the EDIT part please :) @knittl Commented Dec 17, 2020 at 20:05
  • 2
    You need to run it in a separate container, or from outside of Docker entirely. Anything you put in a Compose command: runs instead of the process the container normally runs. Commented Dec 17, 2020 at 20:19
  • Can you redirect me to an example? I am new to this environment. Commented Dec 17, 2020 at 20:31
  • @Siddharth I have written another answer, that does not modify the command in the docker-compose file, but creates a new image. Commented Dec 18, 2020 at 6:47

3 Answers 3

4

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'

Sign up to request clarification or add additional context in comments.

2 Comments

Tried this and I am getting this error now ------- ]# docker-compose up Recreating rspm_uat_rstudio-package-manager_1 ... done Attaching to rspm_uat_rstudio-package-manager_1 rspm_uat_rstudio-package-manager_1 exited with code 1
I don't know what your docker image does or what it provides. Maybe it does not have a tmp folder or the user does not have permission. Then the command will fail. If you only run a single command, the container will terminate once the command has executed. it sounds to me like an XY problem
0

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.

1 Comment

Not working. It says the application should be running for executing these commands in bash. So basically, I need these commands to run after the container is UP. Just something like PostStart function.
0

I know it is probably late but if anyone stumbles upon this, you can try this:-

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'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.