61

I have an issue when running docker-compose up. It hangs on attaching to and I cannot access my web app on localhost:4200.

docker-compose.yml:

version: '2' # specify docker-compose version

# Define the services/containers to be run
services:
  angular: # name of the first service
    build:
      context: . 
      dockerfile: ./.docker/scs.dockerfile  # specify the directory of the Dockerfile
    container_name: secure-cloud-storage-build-dev
    image: secure-cloud-storage
    ports:
      - "4200:4200" # specify port forwarding

dockerfile:

# Stage 0, based on Node.js, to build and compile Angular
FROM node:8.6 as node
WORKDIR /app
COPY package.json /app/
RUN npm install
COPY ./ /app/
ARG env=prod
RUN npm run build -- --prod --environment $env

# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx:1.13
COPY --from=node /app/dist/ /usr/share/nginx/html
COPY ./nginx-custom.conf /etc/nginx/conf.d/default.conf

nginx-custom.conf:

server {
  listen 80;
  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }
}

Here is verbose output:

docker-compose --verbose up output

Any clues what might be going wrong? I am quite fresh in Docker world, basically doing tutorials, so might be something obvious i missed. My localhost:4200 returns ERR_EMPTY_RESPONSE.

PerlMonk92
  • 619
  • 1
  • 5
  • 4
  • create images first and then dcompoise it instead of using build . – Jinna Balu May 01 '18 at 18:38
  • 1
    Your `docker-compose.yaml` specifies that your app in the container is running on port 4200, but your `nginx-custom.conf` listens on port 80. Those ports should match. – adebasi May 01 '18 at 18:44

8 Answers8

59

I know that I'm late to the party but this is to be expected. docker-compose up, just like docker run, requires a -d flag if you want to get your command line back.

Here is what the command would look like: docker-compose up -d

As far as why your app is returning an empty response I'm not sure.

Hope this helps others coming to this thread!

AJRohrer
  • 925
  • 9
  • 9
8

In my case docker also hangs showing the "Attaching to ..." message, but the application works. So it seems to be a display issue that you can ignore.

You can run docker in detached mode docker-compose up -d angular to suppress this message. Till now I did not figure out how to run it in foreground mode without receiving the message.

Your configuration files in general look ok, but you have to configure the right ports i.e. nginx also needs to listen on port 4200.

Martin
  • 391
  • 4
  • 6
4

If you end up here like I did where docker-compose didn't so much as hang but just exit: my problem was someone had added profiles to compose and I didn't catch that the bash script calling docker-compose didn't use the correct --profile argument.

If no profiles are matched, docker-compose will happily do nothing and simply say attaching to and then exit rather than something like "Hey, your profile argument matched nothing, did you do this on purpose?"

Grant Curell
  • 1,321
  • 2
  • 16
  • 32
3

Docker-compose hangs on Attaching to

Seems silly but make sure the log is being written to stdout. I lost an hour to discover that the log was being written to a file.

If you are sure that logs should be printed, change the Ngnix configuration file to point the log to stdout. Read more on this thread.

The message Attaching to container seems a bit misleading to me. I thought Docker was in the process of attaching to the console but it was already attached. Everything else was working normally. The container was up.

My localhost:4200 returns ERR_EMPTY_RESPONSE

In your docker-compose.yml, map the port to where Ngnix is listening. Given the line listen 80;, in your case is 80:

ports:
  - "4200:80" # specify port forwarding
Maicon Mauricio
  • 2,052
  • 1
  • 13
  • 29
0

Try to run this command in another terminal:

docker exec -it ${CONTAINERNAME} echo "Kick it" 

This for some reason helped with my hanging docker-compose attach to stuff. Seems like the STDOUT hangs for some reason and this kicks it further... Strangely it is only hanging when using docker-compose... Starting each service individually works as a charm...

0

If your code is working fine and you just see Attaching to [container name/id] then it is expected behaviour. After attaching it goes to print logs, but if there are no logs then it just waits in that running state, so if you are waiting for logs then there might be something wrong in your logger module check that to see your logs.

If you want up to exit use up -d.

Mahendra
  • 81
  • 1
  • 5
0

I faced such problem when I mapped ports incorrectly.
I mapped:

  ports:
    - "11000:9000"

But app in container was launched at 8000 port. Respectively docker-compose tried to attach service on port which was not open.

Archirk
  • 427
  • 7
  • 25
-1

Make sure service names are unique

I was copy/pasting and ended up with two services with the same name

services: 
  node_server:
    ...

  node_server: # Duplicate service!
    ...

This can cause docker compose to hang on Attaching to

joshuakcockrell
  • 5,200
  • 2
  • 34
  • 47