Answers: <p>You messed up with the syntax.</p>
  <pre><code>$ docker-compose exec -h
  Execute a command in a running container
  
  Usage: exec [options] [-e KEY=VAL...] [--] SERVICE COMMAND [ARGS...]
  </code></pre>
  <p>Your command should be like:</p>
  <pre><code>docker-compose -f docker-compose-custom.yml exec myapp-back-preproduction npm install
  </code></pre>
  <p><code>-p</code> switch is for specifying a project name: identifier that used to construct container name like <code>&lt;project_name&gt;_&lt;service_name&gt;_1</code></p>
  
Answers: CC BY-SA 4.0
Answers: 2021-12-20T15:31:38.167Z
Answers: 2021-12-20T15:31:38.167Z
Answers: xy2
Answers: <p>You can use <code>docker exec -it &lt;container&gt; &lt;command&gt;</code> to execute a command in a running container (see <a href="https://docs.docker.com/engine/reference/commandline/exec/#run-docker-exec-on-a-running-container" rel="nofollow noreferrer">docs</a>). Notably, this enables you to start an interactive shell session within the container, which is really handy for all sorts of things.</p>
  <p>I do wonder why you would need to call <code>npm</code>, though. If this is necessary to run your application, it should be done in the Dockerfile. (Alternatively it could be done in a start-up script.)</p>
  <p>Do note that your container will only stay up while it is actually running an application (that is, while the entry point call did not return), so starting up an inactive container and then manually running applications won't work. The container would simply be stopped immediately.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-12-20T12:31:04.443Z
Answers: 2021-12-20T12:36:42.480Z
Answers: 2021-12-20T12:36:42.480Z
Answers: Lykanion
Answers: <p>this is sample command</p>
  <blockquote>
  <p>docker-compose exec nginx apk add nodejs</p>
  </blockquote>
  <p>it that command i try to install nodejs, so the command you need to execute</p>
  <blockquote>
  <p>docker-compose exec container_namme npm install xxx</p>
  </blockquote>
  
Answers: CC BY-SA 4.0
Answers: 2022-01-03T04:23:38.427Z
Answers: 2022-01-03T04:23:38.427Z
Answers: Iyus Dedi Putra
Body: <p>I have a docker-compose-custom.yml file like this:</p>
  <pre><code>version: '3.8'
  
  services:
    myapp-back-preproduction:
      build:
        context: .
      container_name: myapp-back-preproduction
      image: 'myapp-back-preproduction:latest'
   ....
  </code></pre>
  <p>I want to execute a simple command within the container after this one started to run using <code>docker-compose exec</code>:</p>
  <p><code>docker-compose -p myapp-back-preproduction -f docker-compose-custom.yml exec npm install</code></p>
  <p>I expect <code>npm install</code> to be executed within the container.</p>
  <p>But it displays nothing in the console, it simply returns.</p>
  <p><code>docker exec</code> instead works but it logically lacks all environment variables declared in my <code>docker-compose-custom.yml</code> file; I want to use <code>docker-compose exec</code>.</p>
  <p>My <code>docker-compose</code> version is: <strong>1.29.2</strong></p>
  <p>How to make it work?</p>
  
Comments: CC BY-SA 4.0
Comments: 2022-11-29T16:58:17.057Z
Comments: I had a similar issue. In my case, docker-compose.yml passed some envvars through into the container. The shell from which I wanted to run `docker-compose exec` was lacking these envvars. Once I defined them, it ran successfully.
  Surprisingly running `exec bash`, envvars were using the values defined in the `docker-compose up` command's shell, not in the `docker-compose exec bash` shell.
  (Using docker-compose version 1.29.2, docker-compose.yml version 3.7)
Comments: Vajk Hermecz
Tags: <docker><docker-compose>
Title: docker-compose exec does not execute command even when container is running
UserDisplayName: Mik378
ThreadText: # docker-compose exec does not execute command even when container is running
  
  (Asked by: username_0 on 2021-12-20)
  
  I have a docker-compose-custom.yml file like this:
  
  ```
  version: '3.8'
  
  services:
    myapp-back-preproduction:
      build:
        context: .
      container_name: myapp-back-preproduction
      image: 'myapp-back-preproduction:latest'
   ....
  
  ```
  
  I want to execute a simple command within the container after this one started to run using `docker-compose exec`:
  
  `docker-compose -p myapp-back-preproduction -f docker-compose-custom.yml exec npm install`
  
  I expect `npm install` to be executed within the container.
  
  But it displays nothing in the console, it simply returns.
  
  `docker exec` instead works but it logically lacks all environment variables declared in my `docker-compose-custom.yml` file; I want to use `docker-compose exec`.
  
  My `docker-compose` version is: **1.29.2**
  
  How to make it work?
  
  **Tags:** docker, docker-compose
  
  > **Comments:**
  > **username_1**: I had a similar issue. In my case, docker-compose.yml passed some envvars through into the container. The shell from which I wanted to run `docker-compose exec` was lacking these envvars. Once I defined them, it ran successfully. Surprisingly running `exec bash`, envvars were using the values defined in the `docker-compose up` command's shell, not in the `docker-compose exec bash` shell. (Using docker-compose version 1.29.2, docker-compose.yml version 3.7)  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_2 on 2021-12-20. Score: 2 | Accepted answer
  
  You messed up with the syntax.
  
  ```
  $ docker-compose exec -h
  Execute a command in a running container
  
  Usage: exec [options] [-e KEY=VAL...] [--] SERVICE COMMAND [ARGS...]
  
  ```
  
  Your command should be like:
  
  ```
  docker-compose -f docker-compose-custom.yml exec myapp-back-preproduction npm install
  
  ```
  
  `-p` switch is for specifying a project name: identifier that used to construct container name like `<project_name>_<service_name>_1`
  
  
  ---
  
  ### Answer by username_3 on 2021-12-20. Score: 1
  
  You can use `docker exec -it <container> <command>` to execute a command in a running container (see [docs](https://docs.docker.com/engine/reference/commandline/exec/#run-docker-exec-on-a-running-container)). Notably, this enables you to start an interactive shell session within the container, which is really handy for all sorts of things.
  
  I do wonder why you would need to call `npm`, though. If this is necessary to run your application, it should be done in the Dockerfile. (Alternatively it could be done in a start-up script.)
  
  Do note that your container will only stay up while it is actually running an application (that is, while the entry point call did not return), so starting up an inactive container and then manually running applications won't work. The container would simply be stopped immediately.
  
  
  ---
  
  ### Answer by username_4 on 2022-01-03. Score: 0
  
  this is sample command
  
  > docker-compose exec nginx apk add nodejs
  
  it that command i try to install nodejs, so the command you need to execute
  
  > docker-compose exec container\_namme npm install xxx
  
  
  ---
  
  