noussair
noussair

Reputation: 81

I can't run a docker container of my reactjs app

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?

Upvotes: 3

Views: 2370

Answers (2)

Joe Doyle
Joe Doyle

Reputation: 6383

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

Upvotes: 0

noussair
noussair

Reputation: 81

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     

Upvotes: 5

Related Questions