HiveBrain v1.2.0
Get Started
← Back to all entries
patterndockerMinor

docker-compose exec does not execute command even when container is running

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
containerevendockerrunningcomposedoeswhenexeccommandnot

Problem

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?

Solution

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 __1

Code Snippets

$ docker-compose exec -h
Execute a command in a running container

Usage: exec [options] [-e KEY=VAL...] [--] SERVICE COMMAND [ARGS...]
docker-compose -f docker-compose-custom.yml exec myapp-back-preproduction npm install

Context

StackExchange DevOps Q#15146, answer score: 2

Revisions (0)

No revisions yet.