On my rpi4 i am running docker container with simple FastAPI app and running it with uvicorn, i set docker network to map the container traffic only to the ethernet port using macvlan. Now when i run my container using below command, i dont see the port being exposed, although i see the container running fine (using docker logs -f <container>
.
This is my dockerfile
FROM scratch
ADD ubuntu-focal-oci-arm64-root.tar.gz /
ENV TZ=America/Toronto
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
WORKDIR /app
COPY . .
RUN mkdir shared_volume
RUN apt-get update && apt-get install python3 python3-dev python3-pip firefox firefox-geckodriver -y --no-install-recommends
RUN python3 -m pip install -r requirements.txt
EXPOSE 8000
CMD ["uvicorn","updater:app", "--workers", "4", "--reload"]
and i build it normally with:
sudo docker build -t me/app:1.0 .
then i run it:
sudo docker run \
-p 18000:8000 \
-itd \
-e CONTAINER_NAME='app_1' \
--net=docker_macvlan_1 \
--ip=192.168.3.111 \
--mount source=shared_volume,target=/app/shared_volume \
--name app_1\
<image_id>
If i call it that way above, i dont see the ports expose to my host. If i remove --net and --ip, i see the port exposed.
Why?
When you use a macvlan
network, all container ports are exposed on the IP that you give to the container. In other words, when you use macvlan
it is the same as forward all connections to this IP to the container, where as when you use port forwarding it is like forward all connections to this host port to that container port.
Thus, just try connecting to the IP you gave the container and the port your application is listening on. You don't need port forwarding with macvlan
, port forwarding is for bridge
(the default and aka NAT) network driver, which is used when you do not specify --net=docker_macvlan_1
in docker run
command.