Neil Gaetano Lindberg
Neil Gaetano Lindberg

Reputation: 2935

Cannot exec into container using GitBash when using Docker Compose

I'm new to Docker Compose, but have used Docker for years. The screen shot below is of PowerShell and of GitBash. If I run containers without docker-compose I can docker exec -it <container_ref> /bin/bash with no problems from either of these shells. However, when running using docker-compose up both shells give no error when attempting to use docker-compose exec. They both just hang a few seconds and return to prompt. Lastly, for some reason I do get an error in GitBash when using what I know: docker exec.... I've used this for years so I'm perplexed and posting a question. What does Docker Compose do that messes with GitBash docker ability, but not with PowerShell? And, why the hang when using docker-compose exec..., but no error? can exec using powershell but not gitbash

I am using tty: true in the docker-compose.yml, but that honestly doesn't seem to make a difference. Not to throw a bunch of questions in one post, but whatever is going on could it also be the reason I can't hit my web server in the browser only when using Docker Compose to run it?

version: '3.8'

volumes:
  pgdata:
     external: true   

services:
    db:
        image: postgres
        container_name: trac-db
        tty: true
        restart: 'unless-stopped'
        environment:
            POSTGRES_PASSWORD: postgres
            POSTGRES_USER: postgres
            POSTGRES_DB: iol
        volumes:
            - pgdata:/var/lib/postgresql/data
        network_mode: 'host'
        expose:
            - 5432
    web:
        image: lindben/trac-server
        container_name: trac-server
        tty: true
        restart: 'unless-stopped'
        environment:
            ADDRESS: localhost
            PORT: 3000
            NODE_ENV: development
        depends_on:
            - db
        network_mode: 'host'
        privileged: true
        expose:
            - 1234
            - 3000
    ```

Upvotes: 2

Views: 2472

Answers (1)

Noam Yizraeli
Noam Yizraeli

Reputation: 5404

Issue explanation

I'm going to be assuming you're using Docker for Desktop and so the reason you can docker exec just fine using PowerShell is because both PS and Docker for Desktop built for windows while GitBash which is based on bash which isn't natively used in Windows but in Linux and is based on the Linux shell (bash = Bourne-Again SHell).

Basic Solution

So when using a windows program that needs a tty you need some sort of "adapter", like winpty for example, to bridge the gap between Docker for Desktop native interface and GitBash's (or Bash's) one.

Here's a more detailed explanation on winpty

Better Solution - specific

putting all of this aside, if trying only to use the compose options it maybe better for you to advise this question

Subsequent Hangup Issue

Now, regarding your web service issue, I think that you're not actually publicly exposing your application using the expose tag. take a look at the docker-compose expose reference. what you need is to add a "ports" tag like so as referenced here:

db:
    ports:
      - "5432:5432"
web:
    ports:
      - "1234:1234"
      - "3000:3000"

I hope this solves your pickle ;)

Upvotes: 2

Related Questions