Simple ping Docker container will ping 8.8.8.8 once, then not after that. What am I doing wrong?

So I am learning Docker on my test Ubuntu laptop.

I build a simple Dockerfile

FROM alpine

RUN apk add python3

CMD [“8.8.8.8”]

ENTRYPOINT [“ping”, “-c”, “5”]

I build the image from the directory $ docker build . -t myweb:3
it build fine, then I run it
$ docker run myweb:3 it will ping 8.8.8.8 5 times, just like expected.
then I try to run it again, 100% packet loss

If I reboot the system, and start Docker and go right to $ docker run myweb:3, again, it pings 5 times as expected, then I run the container again, 100% packet loss.
When I check the logs of that container via Docker Desktop, you see the first 5 pings as successful, then the 2nd+ times, 100% packet loss.

I have tried building a custom network with my local home network.
I have modded the daemon.json file with all the correct into.
I cant figure it out.

To add to this, if I change the Dockerfile to ping google.com,. save it, and build the image with a -t myweb:4, it tries to ping google.com via ipv6 and 100% packet loss.
If I reboot, and try running $ docker run myweb:4 it fails 100% loss via ipv6
if I then try to run myweb:3, it fails 100% loss
I can only get it to ping 8.8.8.8 running myweb:3 fresh off a reboot, and it only does it the one time.

When I run $ docker inspect container_name, under “Network Settings”: it says “Bridge”: " ",

Is that supposed to say someone in the quotes after bridge? Should it say “Bridge”: “my_network”?

I am thoroughly confused of why the container will successfully run once, and then not anytime after that. Hopefully I am missing something simple.

Thanks for your time!

Sometimes to catch an error you need to start splitting by halves where’s the problem until you reach it.

Maybe you can change the entrypoint to a bash. Then repeat the exact test but running -it and instead of expecting it to run the ping automatically. When you get control into the bash, do the ping manually.

This way we can isolate if when running the container the second time it works (so your infrastructure is okay and we have something related to the configuration) or if you get control and you loose networking on the second run we need to tackle with infrastructure.

Even more, probably to narrow-down the problem, you probably don’t need python to make a ping test so if your dockerfile gets reduced to FROM alpine and nothing else you probably can first run the test on an alpine image without buiding your own.

So I suggest these tests:

  1. Run the alpine -it against it’s bash + do the ping. Exit. Repeat a second run. Do the ping. Does it work? If not, copy/paste session with commands and outputs.
  2. If it works make an image FROM alpine and nothing else. Run. Ping. Exit. Run second time. Ping. Does it work?
  3. If it works, make an image FROM alpine + add python without changing the command nor entrypoint (leave the bash). Run. Ping. Exit. Run. Ping. Does it work?

Tell us what test are you running (1, 2 or 3) and copy/paste the entire session so we can see commands and outputs. Keep the session to the bare minimum so we are not lost in details.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.