3

I'm new to Docker and I tried to run a container of the create-react-app image so these are the steps that I have done:

  1. npx create-react-app frontend

  2. I created a Dockerfile.dev like below:

    FROM node:alpine
    WORKDIR '/app'
    COPY package.json .
    RUN npm install
    COPY . . 
    CMD ["npm" , "run" , "start"]
    
  3. I used this command to build the image:

    docker build -f Dockerfile.dev .
    
  4. When i run the container using the image id provided:

    docker run -p 3000:3000 my_docker_image_id
    

Nothing happens, as seen in this screenshot.

But when I add the -i argument to my command everything works fine, as seen in this screenshot:

docker run -p 3000:3000 -i my_docker_image_id

Any idea please?

robyaw
  • 2,274
  • 2
  • 22
  • 29
noussair
  • 81
  • 10

2 Answers2

5

There is an issue with the version 3.4.1 of react-scripts,

So i added a docker-compose file and i specified this line who solve the problem and save my day :

stdin_open: true 

So my docker-compose.yml file looks like this :

version : '3'
services:
    web:
        build: 
            context: .
            dockerfile: Dockerfile.dev
        stdin_open: true    
        ports:
            - "3000:3000"
        volumes:
            - /app/node_modules
            - .:/app     
noussair
  • 81
  • 10
0

The -i flag enables Interactive mode which connects the output to your terminal. Did you try accessing the site without the -i flag? It should have served your page, but just not display output to your console.


UPDATE:

So based on the GitHub issue you found, you'll also be able to use docker run with the -it flags. -i is explained above, but -t enables the TTY in a similar way to the stdin_open: true line in your docker-compose.yml

docker -it run -p 3000:3000 my_docker_image_id


Add the -d flag instead which enables Detached mode, and will allow Docker to run your container in the background. You can then run docker logs {container_id} to see the output of the server.

Here's a link in the Docker documentation: https://docs.docker.com/engine/reference/run/#detached-vs-foreground

When starting a Docker container, you must first decide if you want to run the container in the background in a “detached” mode or in the default foreground mode:

-d=false: Detached mode: Run container in the background, print new container id

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
  • Thank you for the response but when i run the commande without -i and i try to access localhost:3000 i got ERR_CONNECTION_REFUSED. And when i used the -d argument i run docker logs with my id container the same output is printed "Starting the development server..." and nothing happens , if i run docker ps after that i have no container running. – noussair Apr 26 '20 at 19:16
  • Try adding `EXPOSE 3000` to your Dockerfile. – Joe Doyle Apr 26 '20 at 19:52
  • Thank you again , it seem that you are the only one who help me :D , i try it an i have the same issue :( – noussair Apr 26 '20 at 21:31
  • 1
    Hello again Mr Joe , i found a solution and i think it is a problem in react-scripts@3.4.1 , so i added a docker-compose file and i added the flowing line who solved my problem : stdin_open: true . If you want to know more i provide you a link to the page that i found the soution in. https://github.com/facebook/create-react-app/issues/8688. Thank you again for your precisous help Sir. – noussair Apr 27 '20 at 00:50
  • Glad you were able to get working again. I did update my answer to also show what's needed to use the `docker run` version. – Joe Doyle Apr 27 '20 at 19:29
  • Thank you Mr Joe. – noussair Apr 27 '20 at 22:29