файл docker-compose:
Код: Выделить всё
services:
server:
build:
context: .
dockerfile: Dockerfile
container_name: server
volumes:
- ./data:/app/data
ports:
- "3000:3000"
networks:
- app_network
stdin_open: true
tty: true
client:
build:
context: .
dockerfile: Dockerfile.client
container_name: client
networks:
- app_network
stdin_open: true
tty: true
networks:
app_network:
name: netflix_app_network
driver: bridge
Код: Выделить всё
# Use an official image with CMake and GCC pre-installed
FROM ubuntu:20.04
# Set environment variables to prevent interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive
# Install necessary tools and dependencies
RUN apt-get update && apt-get install -y \
build-essential \
cmake \
git \
wget \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Copy the entire project into the container
RUN mkdir -p /app/data
COPY . /app
# Set the working directory to the project root
WORKDIR /app
# Create a build directory, configure, and build the project
RUN mkdir -p build && \
cd build && \
cmake .. && \
cmake --build .
# Expose server's port
EXPOSE 3000
VOLUME /app/data
# Set the default command to run the application
CMD ["./build/App"]
Код: Выделить всё
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Copy the client code into the container
COPY . /app
# Install any necessary dependencies
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt
# Command to run the client program
CMD ["tail", "-f", "/dev/null"]
Код: Выделить всё
#include
#include
#include
#include
#include // For memset
TCPServer::TCPServer(int port, IClientHandler* handler)
: port(port), clientHandler(handler) {
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket < 0) {
perror("Failed to create socket");
exit(EXIT_FAILURE);
}
sockaddr_in serverAddr{};
memset(&serverAddr, 0, sizeof(serverAddr)); // Clear memory
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY; // Bind to all available IPv4 addresses
serverAddr.sin_port = htons(port);
if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
perror("Bind failed");
close(serverSocket);
exit(EXIT_FAILURE);
}
}
TCPServer::~TCPServer() {
close(serverSocket);
}
void TCPServer::start() {
if (listen(serverSocket, 5) < 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79276600/trouble-running-client-and-server-program-with-docker[/url]
Мобильная версия