How to ping Docker container from host and vice versa? Which network would be best to use?

I am new to networking but my target is to be able to ping a Docker container from the host and vice versa.

I know there are different types of networking in Docker (bridge,host,macvlan, [two types of ipvlan],none, overlay), however, I am not sure of the best approach for my situation. I think I wouldn’t need the host, bridge and none. Am I right?

My ifconfig for the host is the following:

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:9e:c2:de:e3  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.110  netmask 255.255.252.0  broadcast 192.168.3.255
        ether 00:15:5d:02:15:5f  txqueuelen 1000  (Ethernet)
        RX packets 5199747  bytes 1163346264 (1.1 GB)
        RX errors 0  dropped 7198  overruns 0  frame 0
        TX packets 123732  bytes 272159622 (272.1 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 46761  bytes 11941315 (11.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 46761  bytes 11941315 (11.9 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

in addition, I would like to start my IP address after 110. Is that even possible and can I expose ports as well?

to further elaborate on why I asked the question, I have a project that host communicates with hardware (Microcontrollers like Raspberry Pi, beaglebones, etc) and it uses IP addresses with ports (TCP and udp) to be able to transmit messages. I was thinking of using the Docker container as if these hardware were containers and thought of using 192.168. but set them on one separate network but have a route for the container to communicate with the host (192.168.3.110).

Use the default bridge driver unless you have a good reason not to. That is the easiest and you cans till make ports available by forwarding ports from the host to containers.

If you can’t ping containers, that means you either use Docker Desktop and containers are in a VM including their networks, or you are using MacVLAN as in your other topic

Thank you for your reply @rimelek

Well, I am using Linux containers in Linux OS VM as a host. In the Linux OS VM, I installed docker and docker-dekstop. I tried using the docker0 network where the containers have IP address 172.17.0.3 I tried to ping the host but couldn’t ping the container from the host or vice versa. I tried to look into the MacVLAN but I can’t seem to also ping each device in either way.

I tried to follow the steps from the docs and followed the steps in the video below,

Docker Network Tutorial

Still I could not ping the container from the host and vice versa.

… and Docker Desktop runs in its Docker Engine in a utility vm.

https://docs.docker.com/desktop/networking/#there-is-no-docker0-bridge-on-the-host

The direction container to host can be done like explained in https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host.

Depending whether the application in the container:

  • requires clients to communicate with the application using a fixed port (only a problem if more than one container with the application should be running at the same time)
  • requires that multicast/broadcast traffic reaches the application

then macvlan on docker-ce is probably the way to go. If none of both things are required you can use either Docker Desktop or docker-ce with containers attached to bridge networks and published port to forward host ports to container ports.

Thank you for your reply @meyay

Can you elaborate more on that? Does it mean that I wouldn’t need to install docker-desktop if I already have docker engine and rest? (from the installation steps)

I am creating a service that would be sending messages by knowing the IP address of the receiver and port. The receiver would be a containers.

Additional question:

What would happen if I converted the docker container to LXD?

Docker Desktop always uses a utility vm, regardless of the host OS, to provide the same experience across all platforms. I am not sure what the second sentence means. On Linux, you don’t have to use Docker Desktop, unless you need the extra functionality it provides, and can live with the restrictions it has with network or passing through devices. The differences have been discussed a lot in the forum, and I am sure the forum search will yield plenty of topics about it.

If your service can reach the target using the ip and port, then you can run multiple containers with the same application, and bind different host ports to reach different containers.

I leave this question for someone else to answer.

Thank you for you reply.

I just have a question. I managed to use the host IP to ssh into the container, my question is how can I make debian user to have same access level as root and access to the commands as I am trying to do ifconfig while I am in debian but keep on getting

debian@debian:~$ ifconfig
-bash: ifconfig: command not found
debian@debian:~$

Docker File:

# Use the official Debian base image
FROM debian:buster

# Install required packages
RUN apt-get update && apt-get install -y \
    openssh-server \
    sudo \
    net-tools

# Create a new user 'debian' and set the password
RUN useradd -m -s /bin/bash debian
RUN echo 'debian:temppwd' | chpasswd

# Allow password authentication and root login via SSH
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config

# Remove existing SSH host keys and generate new ones
RUN rm -f /etc/ssh/ssh_host_*key && \
    ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' && \
    ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N '' && \
    ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' && \
    ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''

# Add debian user to sudo group
RUN usermod -aG sudo debian

# Configure sudoers file to allow sudo without password prompt for users in the sudo group
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# Ensure PATH includes directories for net-tools for debian user
RUN echo "export PATH=$PATH" >> /home/debian/.bashrc

CMD ["/bin/bash"]