compose icon indicating copy to clipboard operation
compose copied to clipboard

Docker compose run --entrypoint error: usr/local/bin/npm: npm: line 2: syntax error near unexpected token `;'

Open jlavado17 opened this issue 4 years ago • 13 comments

Description I'm basically getting an npm error on compose v2 but works on compose v1. I recently updated from v1 in order to use the compose v2 secrets syntax. But while I was testing to see if everything was working correctly, this command failed:

$ docker-compose run --rm --entrypoint /bin/bash app
ERROR: The Compose file './docker-compose.yml' is invalid because:
services.app.build contains unsupported option: 'secrets'

And, of course, it failed because I'm using v2 now, so I ran, without success, the new syntax:

docker compose run --rm --entrypoint /bin/bash app

Results:

[+] Running 3/0
 ⠿ Container project-db_user-1    Running                                                                                                                  0.0s
 ⠿ Container project-auth-1       Running                                                                                                                  0.0s
 ⠿ Container project-db_1-1    Running                                                                                                                  0.0s
/usr/local/bin/npm: npm: line 2: syntax error near unexpected token `;'
/usr/local/bin/npm: npm: line 2: `;(function () { // wrapper in case we're in module_context mode'

What's the error here? I didn't change anything except the version of my docker-compose file and using the new property secrets.

Basically, what I usually get, using v1, is this:

$ docker-compose run --rm --entrypoint /bin/bash app
Creating project_app_run ... done
bash-5.1$ 

docker-compose.yml file if needed:

version: '3'
services:
  app:
    build:
      context: "."
      secrets:
        - my_secret
    command: "npm run dev"
    ports:
      - "8001:8080"
      - "9229:9229"
    volumes:
      - ".:/app"
    depends_on:
      - "db_1"
      - "db_2"
      - "auth"
secrets:
  my_secret:
    file: ./my_secret.txt

Additional information you deem important (e.g. issue happens only occasionally):

Output of docker compose version:

Docker Compose version v2.5.1

Output of docker info:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc., v0.8.2)
  compose: Docker Compose (Docker Inc., v2.5.1)
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
  scan: Docker Scan (Docker Inc., v0.17.0)

Server:
 Containers: 63
  Running: 4
  Paused: 0
  Stopped: 59
 Images: 50
 Server Version: 20.10.14
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: active
  NodeID: r9cg6b0sinrcmfcxdkuczb7ju
  Is Manager: true
  ClusterID: 13zx2ftbwf69w84qhsdhrdc5w
  Managers: 1
  Nodes: 1
  Default Address Pool: 10.0.0.0/8  
  SubnetSize: 24
  Data Path Port: 4789
  Orchestration:
   Task History Retention Limit: 5
  Raft:
   Snapshot Interval: 10000
   Number of Old Snapshots to Retain: 0
   Heartbeat Tick: 1
   Election Tick: 10
  Dispatcher:
   Heartbeat Period: 5 seconds
  CA Configuration:
   Expiry Duration: 3 months
   Force Rotate: 0
  Autolock Managers: false
  Root Rotation In Progress: false
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 3df54a852345ae127d1fa3092b95168e4a88e2f8
 runc version: v1.0.3-0-gf46b6ba
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.10.104-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 4.082GiB
 Name: docker-desktop
 ID: HKLF:46HA:WZNW:2JWD:5HHH:K3Z5:UWV5:TOYD:ZTK6:OS6I:YTM3:YCDI
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5000
  127.0.0.0/8
 Live Restore Enabled: false

Additional environment details:

jlavado17 avatar May 18 '22 21:05 jlavado17

@jlavado17

According to documentation secret section should not be under build section. Moreover I can't help with your npm error since it come from your own image.

See following minimal working example :

version: '3'
services:
  app:
    image: alpine
    command: "cat /run/secrets/my_secret"
    secrets:
      - my_secret
secrets:
  my_secret:
    file: my_secret.txt
docker compose up --build
[+] Running 2/0
 ⠿ Network test-compose-9483_default  Created                                                                                        0.0s
 ⠿ Container test-compose-9483-app-1  Created                                                                                        0.0s
Attaching to test-compose-9483-app-1
test-compose-9483-app-1  | HelloWorld!
test-compose-9483-app-1 exited with code 0
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc., v0.8.2)
  compose: Docker Compose (Docker Inc., v2.5.0)
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
  scan: Docker Scan (Docker Inc., v0.17.0)

maxcleme avatar May 19 '22 07:05 maxcleme

Hey @maxcleme thanks for your help, I actually tried that but it doesn't work for me. It fails if I do a docker compose build. It's like the secret it's not defined and returns the same error as mentioned at the top. I am using Buildkit as well by the way. My Dockerfile has something like this:

RUN --mount=type=secret,id=my_secret,uid=1000 \
    MY_SECRET=$(cat /run/secrets/my_secret) \

That's why I am using a secret under the build section following this implementation: https://github.com/docker/compose/pull/9386

jlavado17 avatar May 19 '22 13:05 jlavado17

Ok just to be sure to understand the whole context:

  • when you're using docker-compose the python version of Compose is called (aka compose v1)?
  • when you're using docker compose the Go version of Compose (aka Compose v2), you got a npm error?

So if docker-compose --version mentions a v1.x.x, that's logic you get an error with the new secrets property of the build section because this new attribute isn't supported in the Python version of Compose

Can you share with us the output of:

  • docker compose build --no-cache of your compose stack
  • docker compose up instead of just running a oneoff container

glours avatar May 19 '22 13:05 glours

@glours

That's correct about the versions, I forgot to mention that docker compose build --no-cache having the secrets under the build property works perfectly. Also running docker compose up works well. My only issue here is this command:

docker compose run --rm --entrypoint /bin/bash app

jlavado17 avatar May 19 '22 14:05 jlavado17

Ok fine, so your error is related to the fact that you're trying to ask bash to run you application which should be javascript code and not a bash script. So first question why are you changing the entrypoint of you container and what is the original entrypoint defined in your Dockerfile?

We can expect that docker compose run --rm app npm run dev will work (but maybe you don't want to run the dev version of your app), can you give it a try?

Can you also share your Dockerfile (remove the critical part if needed)?

glours avatar May 19 '22 15:05 glours

The entrypoint in the Dockerfile is a script:

ENTRYPOINT ["/app/entrypoint.sh"]

Running the command docker compose run --rm app npm run dev works well but it doesn't give me the bash/terminal I usually expect:

Example of a working example of my v1 configuration

$ docker-compose run --rm --entrypoint /bin/bash app Creating project_app_run ... done bash-5.1$

jlavado17 avatar May 19 '22 15:05 jlavado17

So you don't want to use docker compose run but docker compose exec --rm --entrypoint /bin/bash app, no?

glours avatar May 19 '22 15:05 glours

I do want to use docker compose run and pass in the entrypoint in order to access the bash of my app service.

By the way, docker compose exec --rm --entrypoint /bin/bash app says that --rm and --entrypoint are unknown flags.

And also, reading the documentation, should I execute it like this?

$ docker compose run --rm app bash
[+] Running 3/0
 ⠿ Container project-db_user-1  Running                                                                                                                                             0.0s
 ⠿ Container project-db_1-1    Running                                                                                                                                             0.0s
 ⠿ Container project-auth-1       Running                                                                                                                                             0.0s
'/app/logs' -> '/log/app'
bash-5.1$ 

It gives me the bash but I'm not sure if that would be the same as running docker-compose run --rm --entrypoint /bin/bash app do you have a better idea, please?

jlavado17 avatar May 19 '22 15:05 jlavado17

Ok my bad I did a mix between exec and run commands 🤦‍♂️

Yes you're inside your container

We usually use exec to open a shell session if the container is already running, that way the command defined in your compose file is still running and you can inspect it if needed docker compose exec app /bin/bash

glours avatar May 19 '22 16:05 glours

So, just to be clear. Is this:

docker compose run --rm  app bash

The replacement of:

docker compose run --rm --entrypoint /bin/bash app

If so, I can go ahead and close the issue.

Also, thanks for the explanation about exec but sometimes I run this command without the container running.

Edit:

But it would have been great to know what the reason was for the error when running docker compose run --rm --entrypoint /bin/bash app.

jlavado17 avatar May 19 '22 16:05 jlavado17

Yep this command open a bash session in your container

Honestly did know why the command with the --entrypoint doesn't work on your side I did a couple of test on my side with containers of different Compose stacks and wasn't able to reproduce your issue

glours avatar May 19 '22 17:05 glours

Ok I managed to reproduce it, so the issue is the usage of the command property in your Compose file which is executed by default with your new /bin/bash entrypoint

So to be able to run your docker compose run --rm --entrypoint /bin/bash app, you should:

  • remove the command from the compose file
  • add CMD ["npm", "run", "dev"] at the end of your dockerfile

glours avatar May 19 '22 19:05 glours

First of all, thank you very much for taking the time to look into this.

Great to know how to solve this. I think I can go ahead and put the CMD ["npm", "run", "dev"] at the end of my Dockerfile without worrying because every service has its own command property and it will override the one from the Dockerfile, right?

But will CMD ["npm", "run", "dev"] in the Dockerfile affect the services that don't have a command defined in the compose file?

Like:

db_service:
    image: "postgres:11"

I mean, I just added the changes you mentioned to my compose file (removed the command from the app service) and also to the Dockerfile (added the CMD at the end of the file), and everything's working fine but it doesn't hurt to ask.

One last question, do you know why the command: "npm run dev" is working on v1 but isn't working on v2?

jlavado17 avatar May 19 '22 21:05 jlavado17