问题
I'm new with docker and i tried to run a container of create-react-app image so this is 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 provided in the screen shot :
nothing happens with this command
But when i add -i argument to my command everything works fine :
docker run -p 3000:3000 -i my_docker_image_id
ok command
Any idea please ?
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/61444970/i-cant-run-a-docker-container-of-my-reactjs-app