Is there a way to permanently set a hostname and IP to a container in docker?
I want to create a stack of machines (containers) in one VM ideally talking to one another with hostname.
You can use the new networking feature available after Docker version 1.10.0
That allows you to connect to containers by their name, assign Ip addrees and host names.
When you create a new network, any container connected to that network can reach other containers by their name, ip or host-names.
i.e:
1) Create network
$ docker network create --subnet=172.18.0.0/16 mynet123
2) Create container inside the network
$ docker run --net mynet123 -h myhostname --ip 172.18.0.22 -it ubuntu bash
Flags:
--net connect a container to a network
--ip to specify IPv4 address
-h, --hostname to specify a hostname
--add-host to add more entries to /etc/hosts
You can use docker-compose tool to create a stack of containers with specific hostnames and addresses.
Here is the example docker-compose.yml with specific network config:
version: "2"
services:
host1:
networks:
mynet:
ipv4_address: 172.25.0.101
networks:
mynet:
driver: bridge
ipam:
config:
- subnet: 172.25.0.0/24
Source: Docker Compose static IP address in docker-compose.yml.
And here is the example of docker-compose.yml file with containers pinging each other:
version: '3'
services:
ubuntu01:
image: bash
hostname: ubuntu01
command: ping -c1 ubuntu02
ubuntu02:
image: bash
hostname: ubuntu02
command: ping -c1 ubuntu01
Run with docker-compose up.
Related
I am able to connect to another container's network stack by running this command:
docker run -it --net=container:<container name> <container image> bash
How something like that can be achieved in Docker Compose?
version: "3.8"
services:
client:
image: ubuntu
networks:
- mynet
attachedclient:
image: ubuntu
networks:
- <???>
networks:
mynet:
What should be added in ??? or somewhere else, so that the attachedclient container would connect to client container's network stack?
simply mynet, containers on the same network can communicate
I want to ask a static IP within the docker container.
I can assign an IP address if there is the IPAM configuration in the network.
version: "2.1"
services:
nginx:
image: ghcr.io/linuxserver/nginx
container_name: nginx
volumes:
- ./config:/config
ports:
- 443:443
restart: always
networks:
br-uplink:
ipv4_address: 192.168.1.2
networks:
br-uplink:
driver: bridge
name: br-uplink
ipam:
config:
- subnet: "192.168.1.0/24"
gateway: "192.168.1.1"
but if there is no IPAM, then this does not work.
$ docker compose up -d
[+] Running 1/2
⠿ Network br-uplink Created
⠋ Container nginx Creating
Error response from daemon: user specified IP address is supported only when connecting to networks with user configured subnets
So if I remove ipv4_address and IPAM configuration, then a random address is assigned.
and if I assign manually within the container, it works.
docker compose exec nginx bash
ip add flush dev eth0
ip add add 192.168.11.2/24 dev eth0
How can I make this possible automatically?
I don't want to create my own Dockerfile for this, it would be happy if this can be done within the docker-compose.yml.
Any ideas?
How do I dynamically add container ip in other Dockerfile ( I am running two container a) Redis b) java application .
I need to pass redis url on run time to my java arguments
Currently I am manually checking the redis ip and copying it in Dockerfile. and later creating new image using redis ip for java application.
docker run --name my-redis -d redis
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-redis
IN Dockerfile (java application)
CMD ["-Dspring.redis.host=172.17.0.2", "-jar", "/apps/some-0.0.1-SNAPSHOT.jar"]
Can I use any script to update the DockerFile or can use any environment variable.
you can assign a static ip address to your dokcer container when you run it, following the steps:
1 - create custom network:
docker network create --subnet=172.17.0.0/16 redis-net
2 - run the redis container to use the specified network, and assign the ip address:
docker run --net redis-net --ip 172.17.0.2 --name my-redis -d redis
by then you have the static ip address 172.17.0.2 for my-redis container, you don't need to inspect it anymore.
3 - now it is possible to run the java appication container but it must use the same network:
docker run --net redis-net my-java-app
of course you can optimize the solution, by using env variables or whatever you find convenient to your setup.
More infos can be found in the official docs (search for --ip):
docker run
docker network
Edit (add docker-compose):
I just find out that it is also possible to assign static ips using docker-compose, and this answer gives an example how.
This is a similar example just in case:
version: '3'
services:
redis:
container_name: redis
image: redis:latest
restart: always
networks:
vpcbr:
ipv4_address: 172.17.0.2
java-app:
container_name: java-app
build: <path to Dockerfile>
networks:
vpcbr:
ipv4_address: 172.17.0.3
depends_on:
- redis
networks:
vpcbr:
driver: bridge
ipam:
config:
- subnet: 172.17.0.0/16
gateway: 172.17.0.1
official docs: https://docs.docker.com/compose/networking/
hope this helps you find your way.
You should add your containers in the same network . Then at runtime you can use that name to refer to the container with its name. Container's name is the host name in the network. Thus at runtime it will be resolved as container's ip address.
Follow these steps:
First, create a network for the containers:
docker network create my-network
Start redis: docker run -d --network=my-network --name=redis redis
Edit java application's Dockerfile, replace -Dspring.redis.host=172.17.0.2" with -Dspring.redis.host=redis" and build again.
Finally start java application container: docker run -it --network=my-network your_image. Optionally you can define a name for the container, but it is not required as you do not access java application's container from redis container.
Alternatively you can use a docker-compose file. By default docker-compose creates a network for running services. I am not aware of your full setup, so I will provide a sample docker-compose.yml that illustrates the main concept.
version: "3.7"
services:
redis:
image: redis
java_app_image:
image: your_image_name
In both ways, you are able to access redis container from java application dynamically using container's hostname instead of providing a static ip.
My local enviroment has connection with a machine that has a BDD. When I ping this machine's IP I have a response.
When I sart my container with docker compose, this container doesn't have connection with this machine. If I enter the container with docker exec -it my-container sh and I ping the DBB machine I have no response.
I have docker installed in Windows 10 y my Docker Compose file looks like this:
version: '3.1'
services:
tomcat:
image: 'tomcat:7.0.91-jre8-alpine'
restart: always
volumes:
- ./warfiles:/home
- ./conf:/usr/local/tomcat/conf/Catalina/localhost
- ./context.xml:/usr/local/tomcat/conf/context.xml
- ./ik-report-config:/ik-report-conf
- ./lib/postgresql-9.3-1102-jdbc4.jar:/usr/local/tomcat/lib/postgresql-9.3-1102-jdbc4.jar
ports:
- 8070:8080
- 8000:8000
environment:
- REPORT_ENGINE_HOME=/ik-report-conf
Thank you very much for your help.
You should probably use network_mode: host.
From the Docker Network Guide:
host: For standalone containers, remove network isolation between the
container and the Docker host, and use the host’s networking directly.
What I had to use was network_mode: bridge
I am running a few Docker containers from my computer and they are linked from one to another like this:
Running the db docker container:
docker run -d -p 27019:27017 --name=docker-db1 docker_db
Running the app container:
docker run -d \
--publish=$PORT:80 \
--link=docker-db1:docker_db \
--hostname="docker-$APPNAME" \
--name=app-web \
abernix/meteord:base
Is there a way to define an IP address for Docker to run on
the same way I define the hostname?
Through the docker daemon
You first need to create a network:
docker network create --subnet=172.18.0.0/16 network_name
Then, when running a container, you can specify an IP address for it with the flags:
--net network_name --ip 172.18.0.XX
With docker-compose
As requested, I provide an example of static predefined IPs using docker-compose:
version: '2'
services:
myservice:
build: .
networks:
mynet:
ipv4_address: 172.25.0.XX
networks:
mynet:
driver: bridge
ipam:
config:
- subnet: 172.25.0.0/24