Answers: <p>Looks like you need a <code>Dockerfile</code> to build your image. What I like to do is build the image, first, then reference that image in <code>docker-compose.yml</code> such that I am only using docker-compose for running the containers.</p>
  <p>Here is a good tutorial with an example of a <code>Dockerfile</code> that can help:
  <a href="https://dockerize.io/guides/node-socket-giude" rel="nofollow noreferrer">https://dockerize.io/guides/node-socket-giude</a></p>
  <p>Also, have a look at existing Docker images that have socket.io support:
  <a href="https://hub.docker.com/search?q=socket.io" rel="nofollow noreferrer">https://hub.docker.com/search?q=socket.io</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2023-12-17T20:56:11.147Z
Answers: 2023-12-17T20:56:11.147Z
Answers: mikequentel
Body: <p>Forgive the vague question title, I'm a complete beginner and I lack the terminology to pose my question more specifically.</p>
  <p>I've been developing a simple application that consists of a game server (using socket.io, so WebSockets) and a game client, which is just an HTTP server. I've finished developing locally and would now like to host it on the servers of my student organisation; seeing as that machine already has Docker installed I figured that would be the easiest solution.</p>
  <p>Concretely, in local development I've set the WebSockets server to use port 3141 and the HTTP server to use port 5173. I tried my hand at using <code>docker compose</code> and came up with the following configuration:</p>
  <pre class="lang-yaml prettyprint-override"><code>services:
    server:
      build: .
      ports:
        - 3141
    frontend:
      build: ./kwispel-client
      ports:
        - 5173
  
  </code></pre>
  <p>Using <code>docker inspect kwispel-server-server-1</code> I can then manually figure out the IP address of the HTTP server and connect to it. However, container naming resolution doesn't work; I need that for</p>
  <ul>
  <li>telling the client side of socket.io where to connect to, and passing in <code>http://server:3141</code> doesn't work;</li>
  <li>telling the server side of socket.io which CORS headers to allow, and passing in <code>http://frontend:5173</code> doesn't work
  Locally the project works fine using <code>localhost</code> instead of <code>server</code> or <code>frontend</code>.</li>
  </ul>
  <p>If I try defining a shared network, like such:</p>
  <pre class="lang-yaml prettyprint-override"><code>services:
    server:
      build: .
      ports:
        - 3141
      networks:
        kwispel-server_gedeeldNetwerk:
          ipv4_address: 172.16.238.10  
    frontend:
      build: ./kwispel-client
      ports:
        - 5173
      networks:
        kwispel-server_gedeeldNetwerk:
          ipv4_address: 172.16.238.20
  networks:
    kwispel-server_gedeeldNetwerk:
      driver: bridge
      ipam:
        config:
          - subnet: &quot;172.16.238.0/24&quot;
  </code></pre>
  <p>so that I can have fixed IP addresses, I can't even connect to the HTTP server on that address.</p>
  <p>Am I missing something obvious? Do I need to pass an extra flag to <code>docker-compose up</code> to expose the ports I need? I'm at a complete loss. Any pointers in the right direction are much appreciated.</p>
  
Tags: <docker><docker-compose>
Title: How do I "Dockerise" a client/server setup using socket.io?
UserDisplayName: simeondermaats
ThreadText: # How do I "Dockerise" a client/server setup using socket.io?
  
  (Asked by: username_0 on 2023-12-16)
  
  Forgive the vague question title, I'm a complete beginner and I lack the terminology to pose my question more specifically.
  
  I've been developing a simple application that consists of a game server (using socket.io, so WebSockets) and a game client, which is just an HTTP server. I've finished developing locally and would now like to host it on the servers of my student organisation; seeing as that machine already has Docker installed I figured that would be the easiest solution.
  
  Concretely, in local development I've set the WebSockets server to use port 3141 and the HTTP server to use port 5173. I tried my hand at using `docker compose` and came up with the following configuration:
  
  ```yaml
  services:
    server:
      build: .
      ports:
        - 3141
    frontend:
      build: ./kwispel-client
      ports:
        - 5173
  
  ```
  
  Using `docker inspect kwispel-server-server-1` I can then manually figure out the IP address of the HTTP server and connect to it. However, container naming resolution doesn't work; I need that for
  
  * telling the client side of socket.io where to connect to, and passing in `http://server:3141` doesn't work;
  * telling the server side of socket.io which CORS headers to allow, and passing in `http://frontend:5173` doesn't work
    Locally the project works fine using `localhost` instead of `server` or `frontend`.
  
  If I try defining a shared network, like such:
  
  ```yaml
  services:
    server:
      build: .
      ports:
        - 3141
      networks:
        kwispel-server_gedeeldNetwerk:
          ipv4_address: 172.16.238.10  
    frontend:
      build: ./kwispel-client
      ports:
        - 5173
      networks:
        kwispel-server_gedeeldNetwerk:
          ipv4_address: 172.16.238.20
  networks:
    kwispel-server_gedeeldNetwerk:
      driver: bridge
      ipam:
        config:
          - subnet: "172.16.238.0/24"
  
  ```
  
  so that I can have fixed IP addresses, I can't even connect to the HTTP server on that address.
  
  Am I missing something obvious? Do I need to pass an extra flag to `docker-compose up` to expose the ports I need? I'm at a complete loss. Any pointers in the right direction are much appreciated.
  
  **Tags:** docker, docker-compose
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-12-17. Score: 2
  
  Looks like you need a `Dockerfile` to build your image. What I like to do is build the image, first, then reference that image in `docker-compose.yml` such that I am only using docker-compose for running the containers.
  
  Here is a good tutorial with an example of a `Dockerfile` that can help:
  <https://dockerize.io/guides/node-socket-giude>
  
  Also, have a look at existing Docker images that have socket.io support:
  <https://hub.docker.com/search?q=socket.io>
  
  
  ---
  
  