Docker Cookbook


Exposing a Container Port on the Host | 71



Download 6,31 Mb.
Pdf ko'rish
bet65/260
Sana21.04.2022
Hajmi6,31 Mb.
#570749
1   ...   61   62   63   64   65   66   67   68   ...   260
Bog'liq
Docker Cookbook

3.2 Exposing a Container Port on the Host | 71


FROM python:2.7.10
RUN pip install flask
COPY hello.py /tmp/hello.py
CMD ["python","/tmp/hello.py"]
This is similar to what you saw in 
Recipe 2.4
. Let’s build this image and run a con‐
tainer without any port mapping flags:
$ docker build -t flask
$ docker run -d --name foobar flask
You can find the IP address and reach the Flask application on port 5000 from within
the host like so:
$ docker inspect -f '{{.NetworkSettings.IPAddress}}' foobar
172.17.0.2
$ curl http://172.17.0.2:5000/
Hello World!
However, you cannot reach this application from 
outside
the host. To make this work,
you are going to run the container again but this time using port mapping:
$ docker kill foobar
$ docker rm foobar
$ docker run -d -p 5000 --name foobar flask
$ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
2cc258827b34 flask "python /tmp/hello.p ... 0.0.0.0:32768->5000/tcp foobar
You see that the 
PORTS
column of 
docker ps
now returns a mapping between port
32768 and port 5000 of the container. The host listens on interface 0.0.0.0, TCP port
32768 and forwards the requests to port 5000 of the container. Try to 
curl
the
Docker host on port 32768, and you will see that you reach the Flask application.
While 
docker ps
returns the port-mapping information and you
can use 
docker inspect
, Docker also has the useful command
docker port
to list only the port mappings of a container. Try this:
$ docker port foobar 5000
0.0.0.0:32768
You might have noticed that your Dockerfile does not contain an 
EXPOSE
statement as
it did in 
Recipe 2.4
. If you add this statement, you can use the 
-P
flag to expose the
port and you do not have to specify the application port. Docker will automatically
set the proper mapping. Add the 
EXPOSE 5000
statement to the Dockerfile, build a
new image, and run the container like so:
$ docker run -d -P flask

Download 6,31 Mb.

Do'stlaringiz bilan baham:
1   ...   61   62   63   64   65   66   67   68   ...   260




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish