简体   繁体   中英

Unable to use localhost with Docker Container

I have tried other answers on stackoverflow and github, but none of them are working. When I run the container I get this as the output(Which is the standard "flask run" output)

This is my Dockerfile

FROM alpine:latest

RUN apk add --no-cache python3-dev && pip3 install --upgrade pip 

WORKDIR /app

COPY . /app

RUN pip3 install Flask && pip3 install requests && pip3 install simplejson

EXPOSE 5000
CMD [ "flask", "run" ]

I have tried 0.0.0.0:5000 too but its not working.

Run this command to know the IP address of the host. boot2docker ip

Now use this IPAddress along with the port number mapped to your host.

When you spawn a container that is running a process such as a web server, you need to specify what ports to publish from the container to the host, for example

docker run -p 8888:8080 tomcat

this publishes port 8080 from the container to the host's port 8888

  1. You can expose a port through your Dockerfile or use --expose and then publish it with the -P flag. This will bind the exposed port to your Docker host on a random port (verified by running docker container ls).

  2. You can expose a port through your Dockerfile or use --expose and then publish it with the -p 80:80 flag. This will bind the exposed port to your Docker host on port 80, and it expects the exposed port is 80 too (adjust as necessary with HOST:CONTAINER).

  3. You can ignore exposing anything and just use -p 80:80 in which case this doubles as both exposing AND publishing the port.

Try running,

docker run -d --name <your_ctr_name>-p 5000:5000 <your_image_name>:latest

If you need to bind host,

docker run -d --name container_name -p 10.x.x.100:5000:5000 image_name:latest

If you want to expose the docker using network host,

docker run -d  --network host --name container_name image_name:latest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM