I'm using many docker containers. I want to assign a static ip to one of them and use that static ip in dockerfile
of an other one.
I want to use some sort of config files like .env
file and use it in both of dockerfile
and docker-compose.yml
.
I figure out a way. simply:
.env
file like ip_server=172.20.0.2
. .env
variables in docker-compose.yml
and pass them to docker image as argument like:
services:
client
build:
context: ./client
args:
- IP_SERVER=${ip_server}
dockerfile
like so
ARG IP_SERVER
ENV IP_SERVER=${IP_SERVER}
references:
- docker-compose env file
- docker-compose args
- dockerfile env
You probably don't want to do either of these things.
If you are running both containers in the same Docker Compose setup, then Compose will create a private Docker network for you, and one container can access the other using its container name (or the name of the block in the docker-compose.yml
) as a host name. That should eliminate the need for a static IP address.
Secondly, hard-coding host names or IP addresses like this makes it a lot harder to reuse your setup. As a very basic example, in development outside Docker, you might run the client and server on the same host and use localhost
as the host name, but in a Docker Compose setup they'll be in different containers and use different host names. This would also mean you can never docker push
your image to a registry, which is needed for some common deployment scenarios. An environment variable is a common way to pass this in from outside.
A docker-compose.yml
that accomplished this might look like:
version: '3'
services:
server:
build:
context: ./server
client:
build:
context: ./client
env:
SERVER_URL: 'http://server/'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.