skip to Main Content

After a week or so of reading Ive been experimenting with docker compose.

I have a docker compose file:

version: "3.8"

x-common-variables: &common-variables
  MYSQL_USER: root
  MYSQL_PASSWORD: MyPassWord
  MYSQL_DATABASE: wedding
  MYSQL_HOST_IP: 127.0.0.1
  REACT_APP_SERVER_PORT: 4000

services:
  mysql:
    image: mysql:latest
    environment:
      <<: *common-variables
      MYSQL_HOST: localhost
      MYSQL_ROOT_PASSWORD: root
    ports:
      - 3307:3307
    restart: unless-stopped
    volumes:
      - /wedding_Script2.sql:/docker-entrypoint-initdb.d/wedding_Script2.sql
  phpmyadmin:
    depends_on:
      - mysql
    image: phpmyadmin/phpmyadmin
    environment:
      PMA_HOST: mysql
    links:
      - mysql:mysql
    ports:
      - 8080:80
    restart: always
  server:
    build: ./weddingApp-Be
    depends_on:
      - mysql
    expose:
      - 4000
    environment:
      <<: *common-variables
      MYSQL_HOST_IP: mysql
      NODE_PATH: src
    ports:
      - 4000:4000
    volumes:
      - ./weddingApp-Be/src:/server
    links:
      - mysql
    command: npm run dev
  client:
    build: ./weddingApp-Fe
    image: nginx:alpine
    environment:
      <<: *common-variables
      NODE_PATH: src
    expose:
      - 3000
    ports:
      - 3000:3000
    volumes:
      - ./weddingApp-Fe/src:/index
    links:
      - server
    command: nginx, -g, daemon off

My project structure:

enter image description here

inside of weddingApp-Fe my dockerFile:

FROM node:12.2.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json /app/package.json
RUN npm install --silent
RUN npm install [email protected] -g --silent
COPY . /app
RUN npm run build

# production environment
FROM nginx:1.16.0-alpine
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 3000
CMD ["nginx", "-g", "daemon off;"]

When i do docker compose build:

mysql uses an image, skipping
phpmyadmin uses an image, skipping
Building server
Step 1/8 : FROM node:10-alpine
 ---> 8e473595b853
Step 2/8 : RUN mkdir -p /app
 ---> Using cache
 ---> 19b56a37f612
Step 3/8 : WORKDIR /app
 ---> Using cache
 ---> 8dce08d04982
Step 4/8 : COPY package.json /app
 ---> Using cache
 ---> e5efea9d7cd2
Step 5/8 : COPY yarn.lock /app
 ---> Using cache
 ---> 94ed16d1b60f
Step 6/8 : COPY . /app
 ---> Using cache
 ---> 57942181ca20
Step 7/8 : RUN npm install
 ---> Using cache
 ---> cc6cf02a698f
Step 8/8 : CMD ["npm", "run dev"]
 ---> Using cache
 ---> 043145db7a94

Successfully built 043145db7a94
Successfully tagged weddingapp_server:latest
Building client
Step 1/12 : FROM node:12.2.0-alpine as build
 ---> f391dabf9dce
Step 2/12 : WORKDIR /app
 ---> Using cache
 ---> 33e6357adf78
Step 3/12 : ENV PATH /app/node_modules/.bin:$PATH
 ---> Using cache
 ---> 90bef54edec6
Step 4/12 : COPY package.json /app/package.json
 ---> Using cache
 ---> 92f7b7643519
Step 5/12 : RUN npm install --silent
 ---> Using cache
 ---> 912d5018ac08
Step 6/12 : RUN npm install [email protected] -g --silent
 ---> Using cache
 ---> bec736fe9b8f
Step 7/12 : COPY . /app
 ---> Using cache
 ---> 29232568be7d
Step 8/12 : RUN npm run build
 ---> Using cache
 ---> 59219cc1e335

Step 9/12 : FROM nginx:1.16.0-alpine
 ---> ef04b00b089d
Step 10/12 : COPY --from=build /app/build /usr/share/nginx/html
 ---> Using cache
 ---> a5be9b459a1b
Step 11/12 : EXPOSE 3000
 ---> Using cache
 ---> 684c6b70aed5
Step 12/12 : CMD ["nginx", "-g", "daemon off;"]
 ---> Using cache
 ---> c38b886354d6

Successfully built c38b886354d6
Successfully tagged nginx:alpine

Problem:

After this I do docker compose up

and thats when I get the following error:

Removing weddingapp_client_1
weddingapp_mysql_1 is up-to-date
weddingapp_phpmyadmin_1 is up-to-date
weddingapp_server_1 is up-to-date
Recreating 5b9004354493_weddingapp_client_1 ... error

ERROR: for 5b9004354493_weddingapp_client_1  Cannot start service client: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: "nginx,": executable file not found in $PATH": unknown

ERROR: for client  Cannot start service client: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: "nginx,": executable file not found in $PATH": unknown
Encountered errors while bringing up the project.

I figure its a problem with my nginx but I dont know what.

EDIT:

If i would change to npm start

at the bottom of the docker-compose then I get

ERROR: for weddingapp_client_1 Cannot start service client: OCI
runtime create failed: container_linux.go:349: starting container
process caused "exec: "npm": executable file not found in $PATH":
unknown

ERROR: for client Cannot start service client: OCI runtime create
failed: container_linux.go:349: starting container process caused
"exec: "npm": executable file not found in $PATH": unknown

2

Answers


  1. The Dockerfile for your client app is specifying node:12.2.0-alpine as the base image which does not include nginx. You will need to install NGINX in this image in order for it to work. Even though you have nginx:alpine as the specified image in your docker-compose file, you have pointed docker-compose build context to ./weddingApp-Fe. This means that docker-compose will search for a Dockerfile in that directory and use it to build your image. NGINX not being installed is also the reason it doesn’t crash when you remove the command: nginx, -g, daemon off line from the compose file.

    Login or Signup to reply.
  2. You’re telling Compose to override the command with something that doesn’t make sense:

    command: nginx, -g, daemon off
    

    This has the same effect as running that command at the command line: the comma is interpreted as part of the command.

    You don’t actually need this option, since the Dockerfile has

    CMD ["nginx", "-g", "daemon off;"]
    

    which is what you want it to do.

    You can similarly delete expose: and links: (not useful in present-day Docker networking) and volumes: (because the code is already in your image). If you are build: your own image, you do not also want image: naming a public Docker Hub image (your locally-built image will overwrite that one and you’ll have confusing results), and the database address is probably not 127.0.0.1 ("this container"). You can probably successfully trim that part of the docker-compose.yml file down to

    client:
      build: ./weddingApp-Fe
      environment:
        MYSQL_USER: root
        MYSQL_PASSWORD: MyPassWord
        MYSQL_DATABASE: wedding
        MYSQL_HOST_IP: mysql
      ports:
        - 3000:3000
    

    (If you really wanted to override the command in the Dockerfile, YAML list syntax command: [nginx, -g, 'daemon off;'] is what you’d need here, but using the default command from the Dockerfile is better.)

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