0

I'm trying to run a react app from a docker container (currently developing locally.) I get this error

Attaching to my-app_c
my-app_c  | 
my-app_c  | > my-app@0.1.0 start
my-app_c  | > sudo PORT=80 react-scripts start
my-app_c  | 
my-app_c  | sh: sudo: not found
my-app_c exited with code 127

I do have access to sudo. This q/a recommended running docker exec -u root -t -i container_id /bin/bash but this triggered another error: Error response from daemon: Container bad3f69996f3814078d1ed94d3d261e3ffea2ab6a95f7151f576115fa6854f72 is not running.

So it seems that I cannot give a docker container root permissions until it is already running and it cannot run until it has sudo access. How to I solve this chicken or the egg type problem?

Edit:

Showing Docker File and YAML file

Dockerfile

FROM node:17-alpine 
WORKDIR /my-app
COPY package.json . 
RUN npm install 
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

docker-compose.yaml

version: "3.8"
services:
  my-app: 
    build: ./my-app
    container_name: my-app_c
    ports:
      - '3000:3000'
    stdin_open: true
    tty: true   
jbuddy_13
  • 902
  • 2
  • 12
  • 34
  • Could you please provide the Dockerfile that you use if you have it? – Daniel Mar 08 '23 at 22:46
  • You don't show where the `sudo` actually is (in your `package.json` file, maybe?) but it works poorly in Docker and you almost never need it. Possibly setting `CMD npx react-scripts start` could be a workaround? – David Maze Mar 09 '23 at 01:13
  • Is that CMD [“npm”, “react-scripts”, “start”]? Or do I misunderstand you? – jbuddy_13 Mar 09 '23 at 05:09
  • @DavidMaze could you clarify the CMD inputs? – jbuddy_13 Mar 09 '23 at 12:40
  • So `CMD` just specifies the single command the container runs. My best guess is that your `package.json` embeds the incorrect `sudo` command in its `"start"` script. But if that just runs `react-scripts start` then you could put that command directly in `CMD` without the `npm` wrapper, using the [`npx`](https://docs.npmjs.com/cli/v9/commands/npx) wrapper to find it in `node_modules/.bin`. – David Maze Mar 09 '23 at 13:41

1 Answers1

0

The issue is that npm start effectively runs a script, once the script is done, the container shuts down. You need to include a CMD or RUN that has a foreground process that doesn't stop (until you say so).

Also, the command you probably wanted is docker run -it <image> /bin/bash as given in this answer. The exec part only works to run a command on a running container, not to start one in a particular fashion.

randy
  • 369
  • 4
  • 12
  • Could you provide a CMD [x,…] example that illustrates your intent? – jbuddy_13 Mar 09 '23 at 05:38
  • Generally speaking, that means an app that "runs." I Googled and found [this](https://stackoverflow.com/a/70755884/6765734). Also, [Baeldung](https://www.baeldung.com/ops/running-docker-containers-indefinitely) never disappoints. – randy Mar 09 '23 at 12:21
  • Thanks, also I tried `docker run -it my-app_c /bin/bash` but `my-app_c` could not be located. Is this what you meant by the image? – jbuddy_13 Mar 09 '23 at 12:55
  • Have you built the image? – randy Mar 09 '23 at 15:22
  • I ran ‘docker-compose up’ which I believe includes ‘docker build’ plus some additional steps – jbuddy_13 Mar 09 '23 at 19:54
  • 1
    If you only used `docker-compose up`, it probably won't build automatically, you'd need to include the `--build` option. I've never used it that way as I like a bit of separation in the steps to create versus run. But if all you've done is make a `Dockerfile` and a `docker-compose.yaml` and haven't given a build instruction, that's the missing piece. Perhaps, include any outputs or errors in your question so we can get a better idea of your full process. – randy Mar 09 '23 at 20:05