dockerdocker-composepycharmdockerfile

How can i solve "unable to prepare context: path "Dockerfile.xxx" not found" error


I am doing a project that runs TCP chatroom which is should containerize and build with docker-compose. Everything is ok till trying to build:

$docker build Dockerfile.server
[+] Building 0.0s (0/0)                                                          docker:default                                                                                                            ERROR: unable to prepare context: path "Dockerfile.server" not found

My file structure

.
└── projee docker-chatroom
    └── docker-compose.yml
    └── Dockerfile.server
    └── Dockerfile.client
    └── serveryt.py
    └── clientyt.py
    └── logs
        └── ...

serveryt.py:

import threading
import socket
import logging

# Logging ayarları
logging.basicConfig(filename='logs/server.log', level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

host = '127.0.0.1'  # Tüm IP adreslerinden bağlantı kabul edilecek
port = 33333

sv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sv.bind((host, port))
sv.listen()

cl = []
name = []


def broadcast(message):
    for client in cl:
        client.send(message)


def handle(cl):
    while True:
        try:
            message = cl.recv(1024)
            broadcast(message)

        except:
            index = cl.index(cl)
            cl.remove(cl)
            cl.close()
            nickname = name[index]
            broadcast(f'{nickname} left the chat.'.encode('ascii'))
            name.remove(nickname)
            logging.info(f'{nickname} left the chat.')
            break


def receive():
    while True:
        client, adrs = sv.accept()
        logging.info(f'Connected with {str(adrs)}')

        client.send('Name:'.encode('ascii'))
        nickname = client.recv(1024).decode('ascii')
        name.append(nickname)
        cl.append(client)

        logging.info(f"Name of the client is {nickname}.")
        broadcast(f"{nickname} connected to the chat.".encode('ascii'))
        client.send('Connected to the chat.'.encode('ascii'))

        thread = threading.Thread(target=handle, args=(client,))
        thread.start()


logging.info("Server is listening...")
receive()


clientyt.py

import socket
import threading
import os
import time

name = os.getenv("USERNAME")
if not name:
    raise ValueError("USERNAME environment variable not set")

cl = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

while True:
    try:
        cl.connect(('server', 33333))  # '127.0.0.1' yerine 'server' kullanıyoruz
        break
    except ConnectionRefusedError:
        print("Server henüz başlatılmadı, 5 saniye sonra tekrar denenecek...")
        time.sleep(5)

def recieve():
    while True:
        try:
            message = cl.recv(1024).decode('ascii')
            if message == 'Name:':
                cl.send(name.encode('ascii'))
            else:
                print(message)
        except:
            print("An error occurred")
            cl.close()
            break

def write():
    while True:
        message = f'{name}: {input("")}'
        cl.send(message.encode('ascii'))

rc_th = threading.Thread(target=recieve)
rc_th.start()

wr_th = threading.Thread(target=write)
wr_th.start()


Dockerfile.server:

# Dockerfile.serveryt
FROM python:3.9-slim

WORKDIR /app

COPY serveryt.py .

RUN pip install scikit-learn

CMD ["python", "serveryt.py"]


Dockerfile.client:

# Dockerfile.clientyt
FROM python:3.9-slim

WORKDIR /app

COPY clientyt.py .

CMD ["python", "clientyt.py"]


docker-compose.yml

version: '3.8'

services:
  server:
    build:
      context: .
      dockerfile: Dockerfile.serveryt
    ports:
      - "33333:33333"
    volumes:
      - ./logs:/app/logs
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    container_name: chatroom_server

  client:
    build:
      context: .
      dockerfile: Dockerfile.clientyt
    depends_on:
      - server
    environment:
      - USERNAME=YourName  # Enter nickname
    container_name: chatroom_client

What is the problem ? I still didnt get it. I thought that it can be a permission problem but docker has root access.

I tried an old stack overflow posts solve but it didn't work


Solution

  • As the docker build documentation says: if Dockerfile name is not Dockerfile, you need to use -f option.

    So you should use this command to build:

    docker build -f Dockerfile.server .
    
    docker build -f Dockerfile.client .
    

    Also dockerfile name is incorrect in your docker-compose.yml file.

    Also you can use docker compose up command to build, create and start the containers.