32

I've run an image with: 'docker-compose up'

With 'docker ps' i get:

CREATED             STATUS              PORTS                    NAMES
55e1fd18acf1        simpleappnodedocker_web   "node app.js"            6 seconds ago       Up 6 seconds        0.0.0.0:9000->3000/tcp   myapp
9879ff20e241        postgres:9.6              "docker-entrypoint..."   36 hours ago        Up 36 hours         0.0.0.0:5432->5432/tcp   nd-db

I try run the bash to enter to the shell, but i get an error, how to solve this, i thinking i'm doing something wrong.

$docker-compose run myapp /bin/bash   
ERROR: No such service: myapp

docker-compose.yml:

version: '2'
services:
  web:
    container_name: myapp
    build: .
    command: node app.js
    ports:
      - "9000:3000"
6
  • you are very definitely doing something wrong. To help you, I would need to know what you did and how you did it. Please post your docker-compose.yaml and a complete recall of all your steps. Oct 7, 2018 at 9:29
  • it's working with: docker exec -it myapp /bin/bash
    – stackdave
    Oct 7, 2018 at 9:30
  • @Phillip-ZyanKLee-Stockmann you have the yml config, but really need use docker-compose run, if i can do it with docker run?
    – stackdave
    Oct 7, 2018 at 9:33
  • nope, you don't. but it is a great tool to have the config in a git repo for example Oct 7, 2018 at 9:35
  • so, where is the erro?
    – stackdave
    Oct 7, 2018 at 9:50

4 Answers 4

45

I think you got the relation of docker and docker-compose wrong:

docker-compose is a wrapper around docker. To do its job docker-compose needs its config: docker-compose.yaml

Spinning your example further:

create docker-compose.yaml:

version: '2'
services:
  web:
    container_name: myapp
    build: .
    command: node app.js
    ports:
      - "9000:3000"

use docker-compose to start the container and run a command in the running container:

docker-compose up
docker-compose exec web /bin/bash

docker-compose uses the name of the service - in your case this is web - whereas docker uses the container name - in this case myapp.

So to run /bin/bash through docker, you would use the following:

docker exec -ti myapp /bin/bash

you could remove the container_name from docker-compose.yaml, then the container would be named automatically by docker-compose - similar to the service, but prefixed with the name of the docker-compose stack (the foldername where docker-compose.yaml is located).

1
  • 3
    You can specify the docker-compose yaml file with : docker-compose -f docker-compose-custom.yml Nov 6, 2019 at 17:30
3

The other answer is right but somehow was a bit confusing in its broad explanation on "the relation of docker and docker-compose".

The main thing to get rid of the error in question is to do what the error says: "ERROR: No such service:", therefore, you must choose a service that is in your compose file, and not the container name. In the example, it is web, and NOT myapp.

In your case, do not run

docker-compose run --rm myapp bash

but run

docker-compose run --rm web bash
0

Also make sure you're running the command in the same folder than your docker-compose.yml, otherwise you'll get the same error message but Phillip's answer above won't work.

0

The answer is ... use the service tag, not your chosen container name, as mentioned in the answer by questionto42standswithUkraine

I also had this issue, because I had 5 different flavours of my docker-compose.yml file, called docker-compose1/2/3/4/5.yml. Each of them had the same service names, just the environment definitions were different between them.

docker-compose logs would only work if I supplied the config file I had used to run the the system up.

so

docker-compose -f docker-compose-3.yml -d

needed

docker-compose -f docker-compose-3.yml logs <service-name>

to work

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .