1

I have a docker-compose.yml (simplified example):

version: "3.8"

services:
  server:
    image: node:lts-slim
    container_name: example-container
    hostname: example-server
    ports:
      - 4000:4000
    working_dir: /server
    volumes:
      - ./someLocalDirectory:/server
    command: node --watch index.js

I create/start the container with docker-compose up -d server

Problem:
I'd prefer to not have command: node --watch index.js hardcoded inside the yml file.

Question:
Is it possible to pass command: node --watch index.js as an argument to docker-compose up -d server on the command line?

Thanks in advance :)


Solution:
You can prepend key-value-pairs on the command-line and treat them like regular variables inside docker-compose:

key="node --watch index.js" docker-compose up -d server

version: "3.8"

services:
  server:
    # ... see example above
    command: ${key}

(see https://stackoverflow.com/a/50991623/1054981)

cl10k
  • 912
  • 1
  • 7
  • 15
  • 1
    I think the best you can do is to have your command in an `.env` file – Hans Kilian Feb 15 '23 at 09:52
  • Can you just run `node --watch index.js`, without involving Docker at all? It seems like with this setup you want to provide the command interactively, and the code only lives on the host system; the only thing you're using Docker for is as an indirect way to get the Node interpreter. – David Maze Feb 15 '23 at 11:23

1 Answers1

0

You have three options:

run:

You can use docker-compose run to run commands against a service.

For example:

docker-compose run server node --watch index.js

Note: container stops when command exits. See also: docs

exec:

You can use docker-compose exec to run commands in a running container.

  1. To start and keep your server running:
services:
  server:
    [..]
    command: tail -f /dev/null
  1. Start service:
docker-compose up -d server
  1. Run commands:
docker-compose exec server node --watch index.js

See also: docs

using an environment variable:

You can pass an env var to your docker-compose file that contains the command you want to run.

  1. Pass an environment variable to server:
services:
  server:
    [..]
    command: $COMMAND
  1. Run service:
COMMAND="node --watch index.js" docker-compose up -d server

See also: docs

Blee
  • 419
  • 5
  • 11
  • 1
    The container immediately exists if I don't start anything. There are probably ways to handle that, but your suggestion unnecessarily increases complexity. – cl10k Feb 16 '23 at 14:52
  • A yes, `docker-compose run` wil exit when your command has `run` (but will the watch command not just keep on running?) Another solution would be to first start your server (something like `command: tail -f /dev/null`), and than you can use `docker-compose exec server ` to run any command you like. – Blee Feb 17 '23 at 12:42
  • Since you edited you initial post so nicely and also cover the solution, I'm marking your post as an Answer. Not sure though if I like your approach editing after I solved the problem myself... BUT as a general documentation for others, your edited post is great – cl10k Feb 21 '23 at 12:20
  • I understand your reservations, I didn't catch the requirement that the container had to keep running – Blee Feb 21 '23 at 13:30