skip to Main Content

I’ve got a simple Node / React project. I’m trying to use Docker to create two containers, one for the server, and one for the client, each with their own Dockerfile in the appropriate directory.

docker-compose.yml

version: '3.9'
services:
  client:
    image: node:14.15-buster
    build:
      context: ./src
      dockerfile: Dockerfile.client
    ports:
      - '3000:3000'
      - '45799:45799'
    volumes:
      - .:/app
    tty: true

  server:
    image: node:14.15-buster
    build:
      context: ./server
      dockerfile: Dockerfile.server
    ports:
      - '3001:3001'
    volumes:
      - .:/app
    depends_on:
      - redis
    links:
      - redis
    tty: true

  redis:
    container_name: redis
    image: redis
    ports:
      - '6379'

src/Dockerfile.client

FROM node:14.15-buster

# also the directory you land in on ssh
WORKDIR /app

CMD cd /app && 
  yarn && 
  yarn start:client

server/Dockerfile.server

FROM node:14.15-buster

# also the directory you land in on ssh
WORKDIR /app

CMD cd /app && 
  yarn && 
  yarn start:server

After building and starting the containers, both containers run the same command, seemingly at random. Either both run yarn start:server or yarn start:client. The logs clearly detail duplicate startup commands and ports being used. Requests to either port 3000 (client) or 3001 (server) confirm that the same one is being used in both containers. If I change the command in both Dockerfiles to echo the respective filename (Dockerfile.server! or Dockerfile.client!), startup reveals only one Dockerfile being used for both containers. I am also running the latest version of Docker on Mac.

What is causing docker-compose to use the same Dockerfile for both containers?

2

Answers


  1. Chosen as BEST ANSWER

    After a lengthy and painful bout of troubleshooting, I narrowed the issue down to duplicate image references. image: node:14.15-buster for each service in docker-compose.yml and FROM node:14.15-buster in each Dockerfile.

    Why this would cause this behavior is unclear, but after removing the image references in docker-compose.yml and rebuilding / restarting, everything works as expected.


  2. When you run docker-compose build with both image and build properties set on a service, it will build an image according to the build property and then tag the image according to the image property.

    In your case, you have two services building different images and tagging them with the same tag node:14.15-buster. One will overwrite the other.

    This probably has the additional unintended consequence of causing your next image to be built on top of the previously built image instead of the true node:14.15-buster.

    Then when you start the service, both containers will use the image tagged node:14.15-buster.

    From the docs:

    If you specify image as well as build, then Compose names the built image with the webapp and optional tag specified in image

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