Run container with network settings

Hello!
Using docker compose, I created container:

   version: '3.8'
   services:

        server:
          build:
              context: ./server/
         container_name: performances-app
         depends_on:
            db:
             condition: service_healthy
         ports:
             - "8083:5000"
         restart: on-failure
         networks:
              custom_net:
                  ipv4_address: 172.18.0.5

    networks:
      custom_net:
         driver: bridge
         ipam:
            config:
              - subnet: 172.18.0.0/16
                gateway: 172.18.0.1

(Its not a matter of question, but just in case I’ll note, that I use static ip in configuration with proxy container, which redirect requests from browser to server, and without explicit static ip, it redirect it each time to different addresses: for example, 172.18.0.1, 172.18.0.2, 172.18.0.3).

I create image from this container and successfully push it to Docker hub.

I’m interested: when I pull this image to another device and create a container, will the network settings be retained in the new container?

Container, created by docker compose, has following network settings:

    "Networks": {
		"theater-app-client-server_custom_net": {
			"IPAMConfig": {
				"IPv4Address": "172.18.0.5"
			},
			"Links": null,
			"Aliases": [
				"performances-app",
				"server"
			],
			"MacAddress": "62:da:8d:99:28:03",
			"DriverOpts": null,
			"GwPriority": 0,
			"NetworkID": "ec898733f5d18430af465038316a03623fc27d809ead541ed38bb2ddb0ae2481",
			"EndpointID": "5cec80296e02684dae68c62a3b78ee739a7353f84aaa8e6b014104e4e369517a",
			"Gateway": "172.18.0.1",
			"IPAddress": "172.18.0.5",
			"IPPrefixLen": 16,
			"IPv6Gateway": "",
			"GlobalIPv6Address": "",
			"GlobalIPv6PrefixLen": 0,
			"DNSNames": [
				"performances-app",
				"server",
				"e33e049aa861"
			]
		}
	}

As I understand, bridge network is used by default, when container created.
But in my case container will work correctly, only if it has network settings with “IPAddress”: “172.18.0.5”

I doubt, that after image pull and container created in new device, this network settings will be saved. As I understand, only default bridge settings will be available.
If that’s the case, what is the best way to create a container in a new environment without using Docker Compose? As variant, I I’ll have to create custom network before container creation in new device? For example, by script:

 docker network create 
       --driver=bridge
       --subnet=172.18.0.0/16
       --gateway=172.18.0.1
       custom_net

And run container with script:
docker run -d --network=custom_net --ip=172.18.0.5 --name performances-app

Or maybe I don’t understand how it works?

No it won’t.

Generally it is not advised to bake environment specific configuration inside an image.

With docker run the default bridge network is used, if no user defined network is specified. Docker compose on the other hand always creates a user defined network. User defined networks allow service discover based on service or container names, a container’s hostname, or a network alias.

Network configuration is not part of the container image. It’s a runtime configuration.

Yes, you need to configure it on every device. But why do you try to avoid docker compose? It makes things much easier.

Have you considered using a containerized reverse proxy that hooks into the docker event stream to add/remove reverse proxy rules, like Traefik or nginx-proxy? It could be possible that you try to solve a problem that doesn’t exist if the right tools are used.

Yes, I didn’t realize, that I could use docker-compose not only on the stage of image creation, but also on the stage of pulling image with container creation in new device ))
Maybe it’s more convenient
Concerning of proxy, I used in my project the following approach:
in Dockerfile for proxy service:

  FROM nginx:1.25
  COPY default.conf /etc/nginx/conf.d

And default.conf:

  server {
     listen 80;
     server_name localhost;

    location / {
          proxy_pass http://frontend:3000/;
    }

    location /api/ {
         proxy_pass http://server:8083/;
       }
   }

In docker compose:

     proxy:
         build: ./docker/proxy
         ports:
           - "3001:80"
         depends_on:
             - frontend
             - server
         networks:
           - custom_net

Is it what you mean, when you said about nginx-proxy ?

It is not. This is not a solution that uses the docker event stream to add/remove proxy rules. Those do not share the problem your manual solution has: they will not cache resolved hostnames indefinitely, but instead will remove the rule when the container is stopped, and add it when the container is started - this will guaranty that the current container ip is used..

Check nginx-proxy with companion (doc) or simple Traefik example instead of manual proxy configuration.

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