skip to Main Content

Let’s say I want to create two services:

  • The first one on port 5000
  • The second one on port 7000

So I create docker-compose.yml file

version: "3"
services:
  project-server:
    build: .
    ports:
      - "5000:5000"
    environment:
      - PORT=5000
    depends_on:
      - mongo
  project-socket-server:
    build:
      context: "."
      dockerfile: "./Dockerfile-socket"
    ports:
      - "7000:7000"
    environment:
      - PORT=7000
    depends_on:
      - project-server

  mongo:
    image: mongo
    environment:
      - MONGO_INITDB_ROOT_USERNAME=project_admin
      - MONGO_INITDB_ROOT_PASSWORD=9293
    volumes:
      - mongo-db:/data/db
volumes:
  mongo-db:

and docker-compose.dev.yml

version: "3"
services:
  project-server:
    build:
      context: .
      args:
        NODE_ENV: development
    volumes:
      - .:/server
      - /server/node_modules
    environment:
      - NODE_ENV=development
      - MONGO_USER=project_admin
      - MONGO_PASSWORD=9293
    command: npm run dev
  project-socket-server:
    build:
      context: "."
      dockerfile: "./Dockerfile-socket"
      args:
        NODE_ENV: development
    volumes:
      - .:/server
      - /server/node_modules
    environment:
      - NODE_ENV=development
      - MONGO_USER=project_admin
      - MONGO_PASSWORD=9293
    command: npm run dev
  mongo:
    environment:
      - MONGO_INITDB_ROOT_USERNAME=project_admin
      - MONGO_INITDB_ROOT_PASSWORD=9293

Dockerfile – for the first service

FROM node:15
WORKDIR /server
COPY package.json .
RUN npm install --only=production

ARG NODE_ENV
RUN if [ "$NODE_ENV" = "development" ]; 
        then npm install; 
        else npm install --only=production; 
        fi

COPY . ./
EXPOSE 5000
CMD ["node", "index.js"]

And Dockerfile-socket for the second service

FROM node:15
WORKDIR /server
COPY package.json .
RUN npm install --only=production

ARG NODE_ENV
RUN if [ "$NODE_ENV" = "development" ]; 
        then npm install; 
        else npm install --only=production; 
        fi

COPY . ./
EXPOSE 7000
CMD ["node", "server.js"]

And package.json as well:

{
  "name": "project-server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon index.js && nodemon server.js",
    "dev": "nodemon index.js && nodemon server.js"
  },
  "dependencies": {
    "@socket.io/mongo-adapter": "^0.2.1",
    "cookie-parser": "^1.4.6",
    "express": "^4.18.1",
    "mongodb": "^4.6.0",
    "mongoose": "^6.3.2",
    "nodemailer": "^6.7.5",
    "socket.io": "^4.5.1",
    "stripe": "^9.11.0",
    "ws": "^8.6.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.16"
  }
}

The problem is, after I call docker-compose the both containers will start – there is no problem here, as I call localhost:5000, it gives me correct data, but after I call localhost:7000 I see all the data as if I call localhost:5000. Am I doing something wrong?

UPD:

it looks like on port 7000 starts index.js, and not server.js script

index.js file:

const port = process.env.PORT;
console.log("connected to port index " + port);

gives back:

connected to port index 5000

server.js file:

const port = process.env.PORT;
console.log("connected to port server " + port);

gives back:

connected to port index 7000

UPD2:

inspect port 5000:

[
    {
        "Id": "6a7729ba9734df523fc7424221d866a63a83faec716f6c7efbf77a3797ff9a51",
        "Created": "2022-07-10T16:01:57.4280865Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "npm",
            "run",
            "dev"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 29648,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-07-10T16:01:58.2066126Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:797d9993a50984ae392476eb54bf79014011d6f146c6b9f2b2f4d46bb6ecdf20",
        "ResolvConfPath": "/var/lib/docker/containers/6a7729ba9734df523fc7424221d866a63a83faec716f6c7efbf77a3797ff9a51/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/6a7729ba9734df523fc7424221d866a63a83faec716f6c7efbf77a3797ff9a51/hostname",
        "HostsPath": "/var/lib/docker/containers/6a7729ba9734df523fc7424221d866a63a83faec716f6c7efbf77a3797ff9a51/hosts",
        "LogPath": "/var/lib/docker/containers/6a7729ba9734df523fc7424221d866a63a83faec716f6c7efbf77a3797ff9a51/6a7729ba9734df523fc7424221d866a63a83faec716f6c7efbf77a3797ff9a51-json.log",
        "Name": "/project-server_project-server_1",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/Users/nikita/Desktop/project-server:/server:rw"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "project-server_default",
            "PortBindings": {
                "5000/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "5000"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": [],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "private",
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": null,
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": null,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/cb1f731b1d50d3ecb220dd8769e1e9088d501429a7760b23474b40fb2baae477-init/diff:/var/lib/docker/overlay2/z2db8ueu6gzx4g6kml9gm4lqh/diff:/var/lib/docker/overlay2/4a5siyd2j04hfi7shvk5uyen8/diff:/var/lib/docker/overlay2/je4ef30hthjwlgjf41mrz7c6p/diff:/var/lib/docker/overlay2/o1n9ak157b8zru8l8zip2oiwu/diff:/var/lib/docker/overlay2/dbdi66mhdlurq8ljn2p8gnotx/diff:/var/lib/docker/overlay2/30480c3ac46a01c8279f265b8055162c1c628ed901b2fa5f13a481933fb6acf2/diff:/var/lib/docker/overlay2/18eb589f35db055e79336c54cc0e5da75d5fed390e4ef3edc149767578e4d304/diff:/var/lib/docker/overlay2/1f82814e597da8a6eef3b98677f9a00eda7ca517b3413fcc43de663692a522b2/diff:/var/lib/docker/overlay2/68e13aa159454b660beda0b20784243a29422d1e67c76a4043d485d2f2bf7fc2/diff:/var/lib/docker/overlay2/3befd14aea9bb18d7ba0fd5aecc2205420fc373fa70b99d93ce1c85de6434fc0/diff:/var/lib/docker/overlay2/85a4d0b611d3d1cd423a78a68cdede7193f8665fef0cfdf573e849abdf32aa08/diff:/var/lib/docker/overlay2/3ee2ce61fd86f7d3eee34d744dfc313068ed76c3a1df068db9d3614c1c4626d5/diff:/var/lib/docker/overlay2/785dcc56e0a473c70c3a8b19b2a163c729fe43c8a2cf8f1a17afc30af0b6a81c/diff:/var/lib/docker/overlay2/a3d6650199856fc04ea61fb1936888adc9324b19fd8ec60f19445bc2fafed8ac/diff",
                "MergedDir": "/var/lib/docker/overlay2/cb1f731b1d50d3ecb220dd8769e1e9088d501429a7760b23474b40fb2baae477/merged",
                "UpperDir": "/var/lib/docker/overlay2/cb1f731b1d50d3ecb220dd8769e1e9088d501429a7760b23474b40fb2baae477/diff",
                "WorkDir": "/var/lib/docker/overlay2/cb1f731b1d50d3ecb220dd8769e1e9088d501429a7760b23474b40fb2baae477/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/Users/nikita/Desktop/project-server",
                "Destination": "/server",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "volume",
                "Name": "54ae173b93a965d9ac3feba255694b31718c684026fb05fc5d811de873319ebf",
                "Source": "/var/lib/docker/volumes/54ae173b93a965d9ac3feba255694b31718c684026fb05fc5d811de873319ebf/_data",
                "Destination": "/server/node_modules",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "6a7729ba9734",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "5000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PORT=5000",
                "NODE_ENV=development",
                "MONGO_USER=project_admin",
                "MONGO_PASSWORD=9293",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NODE_VERSION=15.14.0",
                "YARN_VERSION=1.22.5"
            ],
            "Cmd": [
                "npm",
                "run",
                "dev"
            ],
            "Image": "project-server_project-server",
            "Volumes": {
                "/server": {},
                "/server/node_modules": {}
            },
            "WorkingDir": "/server",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "com.docker.compose.config-hash": "3ac7f110d763362d49938c1ed5213e7e50072c8ab95ee0b8c58f9dc2826d9ae6",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "project-server",
                "com.docker.compose.project.config_files": "docker-compose.yml,docker-compose.dev.yml",
                "com.docker.compose.project.working_dir": "/Users/nikita/Desktop/project-server",
                "com.docker.compose.service": "project-server",
                "com.docker.compose.version": "1.29.2"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "ebc830dec3ca4c02819e90f2fa79b0db8f88043ba56abb67795bb510fba14ad4",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "5000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "5000"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/ebc830dec3ca",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "project-server_default": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "project-server",
                        "6a7729ba9734"
                    ],
                    "NetworkID": "3f5807c2c3187687876240b47feb4fd5e997c561dbef6462fd3a195ad0b11f43",
                    "EndpointID": "2ccabd2ce6680746c69b9dfd03b85cf17fe9fea4d614ffc58e2e4b74e1eec9a5",
                    "Gateway": "192.168.192.1",
                    "IPAddress": "192.168.192.3",
                    "IPPrefixLen": 20,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:c0:a8:c0:03",
                    "DriverOpts": null
                }
            }
        }
    }
]

inspect port 7000

[
    {
        "Id": "856312d4b656a689e54567e0c2f33319d7396e16c2d0927b003c4a204b6d58b4",
        "Created": "2022-07-10T16:01:58.2531359Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "npm",
            "run",
            "dev"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 29787,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-07-10T16:01:58.6972426Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:d9831965ad489f691c2dedbdd5c12c4afc780322e43105add43f0db791d88362",
        "ResolvConfPath": "/var/lib/docker/containers/856312d4b656a689e54567e0c2f33319d7396e16c2d0927b003c4a204b6d58b4/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/856312d4b656a689e54567e0c2f33319d7396e16c2d0927b003c4a204b6d58b4/hostname",
        "HostsPath": "/var/lib/docker/containers/856312d4b656a689e54567e0c2f33319d7396e16c2d0927b003c4a204b6d58b4/hosts",
        "LogPath": "/var/lib/docker/containers/856312d4b656a689e54567e0c2f33319d7396e16c2d0927b003c4a204b6d58b4/856312d4b656a689e54567e0c2f33319d7396e16c2d0927b003c4a204b6d58b4-json.log",
        "Name": "/project-server_project-socket-server_1",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/Users/nikita/Desktop/project-server:/socket-server:rw"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "project-server_default",
            "PortBindings": {
                "7000/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "7000"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": [],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "private",
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": null,
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": null,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/f92796078a9effb55efa307514fcaea257d3ca5cfe1fa9717dd8f26eda800a8b-init/diff:/var/lib/docker/overlay2/z2db8ueu6gzx4g6kml9gm4lqh/diff:/var/lib/docker/overlay2/4a5siyd2j04hfi7shvk5uyen8/diff:/var/lib/docker/overlay2/je4ef30hthjwlgjf41mrz7c6p/diff:/var/lib/docker/overlay2/o1n9ak157b8zru8l8zip2oiwu/diff:/var/lib/docker/overlay2/dbdi66mhdlurq8ljn2p8gnotx/diff:/var/lib/docker/overlay2/30480c3ac46a01c8279f265b8055162c1c628ed901b2fa5f13a481933fb6acf2/diff:/var/lib/docker/overlay2/18eb589f35db055e79336c54cc0e5da75d5fed390e4ef3edc149767578e4d304/diff:/var/lib/docker/overlay2/1f82814e597da8a6eef3b98677f9a00eda7ca517b3413fcc43de663692a522b2/diff:/var/lib/docker/overlay2/68e13aa159454b660beda0b20784243a29422d1e67c76a4043d485d2f2bf7fc2/diff:/var/lib/docker/overlay2/3befd14aea9bb18d7ba0fd5aecc2205420fc373fa70b99d93ce1c85de6434fc0/diff:/var/lib/docker/overlay2/85a4d0b611d3d1cd423a78a68cdede7193f8665fef0cfdf573e849abdf32aa08/diff:/var/lib/docker/overlay2/3ee2ce61fd86f7d3eee34d744dfc313068ed76c3a1df068db9d3614c1c4626d5/diff:/var/lib/docker/overlay2/785dcc56e0a473c70c3a8b19b2a163c729fe43c8a2cf8f1a17afc30af0b6a81c/diff:/var/lib/docker/overlay2/a3d6650199856fc04ea61fb1936888adc9324b19fd8ec60f19445bc2fafed8ac/diff",
                "MergedDir": "/var/lib/docker/overlay2/f92796078a9effb55efa307514fcaea257d3ca5cfe1fa9717dd8f26eda800a8b/merged",
                "UpperDir": "/var/lib/docker/overlay2/f92796078a9effb55efa307514fcaea257d3ca5cfe1fa9717dd8f26eda800a8b/diff",
                "WorkDir": "/var/lib/docker/overlay2/f92796078a9effb55efa307514fcaea257d3ca5cfe1fa9717dd8f26eda800a8b/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/Users/nikita/Desktop/project-server",
                "Destination": "/socket-server",
                "Mode": "rw",
                "RW": true,
                "Propagation": "rprivate"
            },
            {
                "Type": "volume",
                "Name": "7a7d8d6a4c7c004640e16bc4f50be2f9b2cfb94855ebbf864999d5f8a263aa54",
                "Source": "/var/lib/docker/volumes/7a7d8d6a4c7c004640e16bc4f50be2f9b2cfb94855ebbf864999d5f8a263aa54/_data",
                "Destination": "/socket-server/node_modules",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "856312d4b656",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "7000/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PORT=7000",
                "NODE_ENV=development",
                "MONGO_USER=project_admin",
                "MONGO_PASSWORD=9293",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NODE_VERSION=15.14.0",
                "YARN_VERSION=1.22.5"
            ],
            "Cmd": [
                "npm",
                "run",
                "dev"
            ],
            "Image": "project-server_project-socket-server",
            "Volumes": {
                "/socket-server": {},
                "/socket-server/node_modules": {}
            },
            "WorkingDir": "/server",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
                "com.docker.compose.config-hash": "1b0d7e398115bad6eae26e92b1d54d15163ca74e7f6570492daf14f023014052",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "project-server",
                "com.docker.compose.project.config_files": "docker-compose.yml,docker-compose.dev.yml",
                "com.docker.compose.project.working_dir": "/Users/nikita/Desktop/project-server",
                "com.docker.compose.service": "project-socket-server",
                "com.docker.compose.version": "1.29.2"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "fe3cffcd72a06aaa92c87cd94280ce914bc13f7e269c51a98901ce639ba4a4d1",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "7000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "7000"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/fe3cffcd72a0",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "project-server_default": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "project-socket-server",
                        "856312d4b656"
                    ],
                    "NetworkID": "3f5807c2c3187687876240b47feb4fd5e997c561dbef6462fd3a195ad0b11f43",
                    "EndpointID": "bd3915089cf996887646b20d34386214eca5ea484daa74284c5e1a63c144c3bd",
                    "Gateway": "192.168.192.1",
                    "IPAddress": "192.168.192.4",
                    "IPPrefixLen": 20,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:c0:a8:c0:04",
                    "DriverOpts": null
                }
            }
        }
    }
]

2

Answers


  1. As mentioned in "Putting Your NodeJS App in a Docker Container"

    EXPOSE is a little different from the other instructions in that it doesn’t have a practical purpose.
    It only exists to provide metadata about what port the container expects to expose.

    You don’t need to use EXPOSE in your container, but anyone who has to understand how to connect to it will appreciate it.

    You need to check if CMD ["node", "server.js"] in Dockerfile-socket does take advantage at runtime of the project-socket-server service environment: PORT=7000.
    It uses the socketio/socket.io-mongo-adapter, which could use the port of the existing MongoDB it adapts to.


    As discussed with the OP, and detailed jccampanero‘s more accurate answer, the root cause is the common package.json, with the common dev directive starting both index.js (first) and then server.js.

    The first (index.js) would answer the query even though it is in the server container.

    Login or Signup to reply.
  2. According to the docker containers inspect output you provided it seems you are running the following command:

     "Path": "docker-entrypoint.sh",
     "Args": [
       "npm",
       "run",
       "dev"
     ],
    
     
    
     "Cmd": [
       "npm",
       "run",
       "dev"
     ]
    

    in both of them, which is consistent with the command you provided for both containers in your docker-compose.dev.yml file:

    command: npm run dev
    

    As stated in the docker-compose documentation, please, be aware that by doing so you are overwriting your containers Dockerfile command.

    It means that both containers are running the dev scripts.

    According to your project structure it turns out both containers are sharing the same package.json and code.

    In package.json you defined the dev script like this:

    "scripts": {
      "start": "nodemon index.js && nodemon server.js",
      "dev": "nodemon index.js && nodemon server.js"
    },
    

    It means you are launching in both containers index.js, with different ports, according to your PORT variables declaration in each container, but they are both running index.js after all.

    That explains why you are getting the same output in every call:

    connected to port index ...
    

    Although my advice is that you separate the source code for every container, to solve the problem you could try running a different script per every container. I mean, in your package.json file I would include a new script, server, for example:

    "scripts": {
      "start": "nodemon index.js && nodemon server.js",
      "dev": "nodemon index.js",
      "server": "nodemon server.js"
    },
    

    And use the new script when launching your container in docker-compose:

      project-socket-server:
        build:
          context: "."
          dockerfile: "./Dockerfile-socket"
          args:
            NODE_ENV: development
        volumes:
          - .:/server
          - /server/node_modules
        environment:
          - NODE_ENV=development
          - MONGO_USER=project_admin
          - MONGO_PASSWORD=9293
        command: npm run server
    

    As an alternative, you can remove the lines:

    command: npm run dev
    

    from your docker-compose.dev.yml file:

    ersion: "3"
    services:
      project-server:
        build:
          context: .
          args:
            NODE_ENV: development
        volumes:
          - .:/server
          - /server/node_modules
        environment:
          - NODE_ENV=development
          - MONGO_USER=project_admin
          - MONGO_PASSWORD=9293
      project-socket-server:
        build:
          context: "."
          dockerfile: "./Dockerfile-socket"
          args:
            NODE_ENV: development
        volumes:
          - .:/server
          - /server/node_modules
        environment:
          - NODE_ENV=development
          - MONGO_USER=project_admin
          - MONGO_PASSWORD=9293
      mongo:
        environment:
          - MONGO_INITDB_ROOT_USERNAME=project_admin
          - MONGO_INITDB_ROOT_PASSWORD=9293
    

    This will make docker to use the CMD provided in the different Dockerfiles:

    CMD ["node", "index.js"]
    

    and:

    CMD ["node", "server.js"]
    

    which I think it will work as well.

    As I said, please, consider in any case reorder your project structure to avoid using the same directory for building your two apps, as you can see it could be the cause of several problems.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search