278

I use this image: dperson/samba

The image is defining its own entrypoint and I do not want to override it.

I need to pass arguments to the entrypoint, easy with docker only:

docker run ... dperson/samba arg1 arg2 arg3

But how to do it with docker_compose.yml ?

Right now I use as a workaround:

command: samba.sh arg1 arg2 arg3

But it is not satisfying as I force the redefinition of the entrypoint.

danronmoon
  • 3,814
  • 5
  • 34
  • 56
user1707414
  • 3,833
  • 2
  • 18
  • 19

7 Answers7

241

The command clause does work as @Karthik says.

As a simple example, the following service will have a -inMemory added to its ENTRYPOINT when docker-compose up is run.

version: '2'
services:
  local-dynamo:
    build: local-dynamo
    image: spud/dynamo
    command: -inMemory
Cadoiz
  • 1,446
  • 21
  • 31
Randy Larson
  • 6,817
  • 2
  • 18
  • 13
  • 2
    Is there documentation on this? I can't seem to find any reference to a command clause on a docker-compose service. – Matt C. May 19 '21 at 22:37
  • I get `Error parsing commandline arguments: unknown short flag '-w'` when I supply `command: - -web.config.file=web.yml` – volvox Jul 15 '21 at 14:56
  • 1
    Here are the docs for `command`: https://docs.docker.com/compose/compose-file/compose-file-v3/#command – Jeremy John Aug 23 '21 at 16:56
102

Whatever is specified in the command in docker-compose.yml should get appended to the entrypoint defined in the Dockerfile, provided entrypoint is defined in exec form in the Dockerfile:

ENTRYPOINT ["executable", "param1", "param2"]

If the EntryPoint is defined in shell form (e.g., ENTRYPOINT executable param1 param2), then any CMD arguments will be ignored.

rrauenza
  • 6,285
  • 4
  • 32
  • 57
Karthik
  • 1,383
  • 1
  • 10
  • 11
50

You can use docker-compose run instead of docker-compose up and tack the arguments on the end. For example:

docker-compose run dperson/samba arg1 arg2 arg3

If you need to connect to other docker containers, use can use --service-ports option:

docker-compose run --service-ports dperson/samba arg1 arg2 arg3
afaulconbridge
  • 1,107
  • 9
  • 21
  • 2
    sadly in swarm this is not an option – holms Jun 20 '18 at 13:25
  • 2
    Unfortunately, that creates a container with a long name (with an ID appended) so you can't use `docker-compose logs ` because the service name is different (and no doubt other differences.) – Jeff Learman Jan 28 '22 at 22:17
44

To override the default entrypoint, use entrypoint option. To pass the arguments use command.

Here is the example of replacing bash with sh in ubuntu image:

version: '3'
services:
  sh:
    entrypoint: /bin/sh
    command: -c "ps $$(echo $$$$)"
    image: ubuntu
    tty: true
  bash:
    entrypoint: /bin/bash
    command: -c "ps $$(echo $$$$)"
    image: ubuntu
    tty: true

Here is the output:

$ docker-compose up   
Starting test_sh_1                ... done
Starting 020211508a29_test_bash_1 ... done
Attaching to test_sh_1, 020211508a29_test_bash_1
sh_1    |   PID TTY      STAT   TIME COMMAND
sh_1    |     1 pts/0    Ss+    0:00 /bin/sh -c ps $(echo $$)
020211508a29_test_bash_1 |   PID TTY      STAT   TIME COMMAND
020211508a29_test_bash_1 |     1 pts/0    Rs+    0:00 ps 1
kenorb
  • 155,785
  • 88
  • 678
  • 743
19

I was facing the same issue with jenkins ssh slave 'jenkinsci/ssh-slave'. However, my case was a bit complicated because it was necessary to pass an argument which contained spaces. I've managed to do it like below (entrypoint in dockerfile is in exec form):

command: ["some argument with space which should be treated as one"]
Pang
  • 9,564
  • 146
  • 81
  • 122
Alina Grosu
  • 253
  • 3
  • 8
12

you can pass the args in the command field just like that

command:
  - arg1
  - arg2
alirezaSafi
  • 161
  • 1
  • 2
0

You also don't need to use the command option in docker-compose.yml, you can just pass them as additional bullet points in the entrypoint section like this:

    entrypoint:
      - /entrypoint.sh
      - arg1
      - arg2
ShaneOH
  • 1,454
  • 1
  • 17
  • 29