admin管理员组

文章数量:1123344

I'm trying to run a client-server application in Python using docker-compose:

I wrote a client.py file that awaits input: command=input("enter command"). The client.py sends requests to the server based on the command it got.

The project tree looks like this:

.
├── client
│   ├── client.py
│   └── Dockerfile
├── docker-compose.yml
└── server
    ├── Dockerfile
    └── server.py

The server.py receives the request and sends back a response to the client.

Now, I'm trying to build and run the scripts:

This is the docker-compose.yml:


version: '3'

services:
  server:
    build:
      context: ./server
      network: host
    command: python ./server.py
    ports:
      - 1337:1337

  client:
    build:
      context: ./client
      network: host
    command: python ./client.py
    depends_on:
      - server
    network_mode: host
    stdin_open: true
    tty: true

The Dockerfiles are pretty simple:

In client folder:

FROM python:latest
ADD client.py /client/
WORKDIR /client/

In server folder:

FROM python:latest
RUN pip install bcrypt
ADD server.py /server/
WORKDIR /server/

I run sudo docker-compose up and then attach to the client container: sudo docker attach a3_client_1 but it doesn't print the string in the input. It prints it only in the terminal where I ran sudo docker-compose up and only after I input a command in the other attached terminal.

Is there a way to show the prints made before the attach ?

Maybe there is a way to run the script only when someone attaches to the container ?

I'm trying to run a client-server application in Python using docker-compose:

I wrote a client.py file that awaits input: command=input("enter command"). The client.py sends requests to the server based on the command it got.

The project tree looks like this:

.
├── client
│   ├── client.py
│   └── Dockerfile
├── docker-compose.yml
└── server
    ├── Dockerfile
    └── server.py

The server.py receives the request and sends back a response to the client.

Now, I'm trying to build and run the scripts:

This is the docker-compose.yml:


version: '3'

services:
  server:
    build:
      context: ./server
      network: host
    command: python ./server.py
    ports:
      - 1337:1337

  client:
    build:
      context: ./client
      network: host
    command: python ./client.py
    depends_on:
      - server
    network_mode: host
    stdin_open: true
    tty: true

The Dockerfiles are pretty simple:

In client folder:

FROM python:latest
ADD client.py /client/
WORKDIR /client/

In server folder:

FROM python:latest
RUN pip install bcrypt
ADD server.py /server/
WORKDIR /server/

I run sudo docker-compose up and then attach to the client container: sudo docker attach a3_client_1 but it doesn't print the string in the input. It prints it only in the terminal where I ran sudo docker-compose up and only after I input a command in the other attached terminal.

Is there a way to show the prints made before the attach ?

Maybe there is a way to run the script only when someone attaches to the container ?

Share Improve this question asked 12 hours ago WahalezWahalez 4891 gold badge6 silver badges24 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

For the setup you show, don't run the client in a container. Compose services aren't usually a good match for programs that accept input on stdin, or that routinely exit and restart. Since your backend service declares ports:, it can be accessed from outside Docker space.

That is, you should be able to reduce your Compose file to

version: '3.8'
services:
  server:
    build: ./server
    ports:
      - 1337:1337

and run docker-compose up -d. Then you can run the client locally

./client.py localhost:1337

and it will accept stdin from the console normally, and connect to the published port on the local machine. As with any other networked service, this leaves you free to run the client on a different machine (use the server machine's DNS name instead of localhost), and you don't require administrator-level permission to use docker commands to run the client.

If the service runs over HTTP, then you can also use ordinary HTTP tools like curl(1) or a Web browser to interact with it. Again, these don't need to run in a container; you can just use the copy of these standard tools that you have on the desktop system.

本文标签: pythonShow waiting input string when attaching to a containerStack Overflow