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
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
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.
I need to connect FTP server from my_go_app container.
When I do it from it from docker compose, I can do it with:
apk add lftp
lftp -d ftp://julien:test#ftpd-server
and it connects well
but when I try to run my container via docker run, I cannot connect anymore to FTP server
Here the command I use:
docker run --name my_go_app --rm -v volume:/go my_go_app:exp --network=my_go_app_network --env-file ./test.env
Here is the working docker-compose.yml
version: '3'
services:
my_go_app:
image: my_go_app:exp
volumes:
- ./volume:/go
networks:
my_go_app_network:
env_file:
- test.env
ftpd-server:
container_name: ftpd-server
image: stilliard/pure-ftpd:hardened
ports:
- "21:21"
- "30000-30009:30000-30000"
environment:
PUBLICHOST: "0.0.0.0"
FTP_USER_NAME: "julien"
FTP_USER_PASS: "test"
FTP_USER_HOME: "/home/www/julien"
restart: on-failure
networks:
my_go_app_network:
networks:
my_go_app_network:
external: true
EDIT:
I added the network as external and created it manually with:
docker network create my_go_app_network
Now it appears that my_go_app is part of the default network:
my_go_app git:(tests) ✗ docker inspect my_go_app -f "{{json .NetworkSettings.Networks }}"
{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"62b2dff15ff00d5cd56c966cc562b8013d06f18750e3986db530fbb4dc4cfba7","EndpointID":"6d0a81a83cdf639ff13635f0a38eeb962075cd729181b7c60fadd43446e13607","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02","DriverOpts":null}}
➜ my_go_app git:(tests) ✗ docker network ls
NETWORK ID NAME DRIVER SCOPE
62b2dff15ff0 bridge bridge local
f33ab34dd91d host host local
ee2d604d6604 none null local
61a661c82262 my_go_app_network bridge local
What am I missing ?
Your network my_go_app_network should be declared as "external", otherwise compose will create a network called "project_name_my_go_app_network". Therefore your go app was not in the same network with the ftp server.
(I guess you have created my_go_app_network manually so your docker run did not throw any network not found error.)
EDIT
You put the arguments in the wrong order. Image name has to be the last one, otherwise they are considered as "commands" for the container. Try
docker run --name my_go_app --rm -v volume:/go --network=my_go_app_network --env-file ./test.env my_go_app:exp
In the end I'd like to have a pure IPv6 network deployed via compose or swarm mode. For now, I'd just like to have a single container deployed with IPv6 (only). I am not currently interested in routing (just container to container connectivity).
My setup:
OS: Centos 7
dockerd --ipv6 --fixed-cidr-v6=2001:db8:1::/64 --iptables=true --ip-masq=true --mtu=1600 --experimental=true
docker-engine-17.05.0.ce-1.el7.centos.x86_64.rpm
Host has IPv4 and IPv6 addresses. Forwarding is on for both (not that it matters for me).
I've tried what seems to be every combination (I'm only listing a couple)
Self-contained Docker stack with container and network:
version: '3'
networks:
app_net:
driver: overlay
driver_opts:
com.docker.network.enable_ipv6: "true"
ipam:
driver: default
config:
-
subnet: 172.16.238.0/24
-
subnet: 2001:3984:3989::/64
services:
app:
image: alpine
command: sleep 600
networks:
app_net:
ipv4_address: 0.0.0.0
ipv6_address: 2001:3984:3989::10
Result: Only IPv4 address in container, 0.0.0.0 is ignored.
Externally pre-created network
(as per https://stackoverflow.com/a/39818953/1735931)
docker network create --driver overlay --ipv6
--subnet=2001:3984:3989::/64 --attachable ext_net
version: '3'
networks:
ext_net:
external:
name: ext_net
services:
app:
image: alpine
command: ifconfig eth0 0.0.0.0 ; sleep 600
cap_add:
- NET_ADMIN
networks:
ext_net:
ipv4_address: 0.0.0.0
ipv6_address: 2001:3984:3989::10
Result: Both IPv4 and IPv6 addresses in container, but cap_add is ignored (not supported in Swarm Mode), and thus the ifconfig disable ipv4 attempt above does not work.
I don't currently have docker-compose installed, and will probably try that next, but is there a way to run pure IPv6 containers in Docker Swarm Mode?
Note: I am able to run and configure a few IPv6-only containers manually without swarm/compose:
(Create network as above or even just use the default bridge)
$ docker run --cap-add=NET_ADMIN --rm -it alpine
$$ ifconfig eth0 0.0.0.0
$$ ping6 other-container-ipv6-address # WORKS!
or shorthand:
$ docker run --cap-add=NET_ADMIN --rm -it alpine sh -c "/sbin/ifconfig eth0 0.0.0.0 ; sh"
I was able to hack it with docker-compose via severe ugliness. If you're desperate, here it is. (This method can never work for Swarm Mode due to privilege escalation).
The Plan
Grant containers rights to manage IP's
Remove IPv4 IP address from within each container on startup.
Use a volume to improvise a hosts file in place of DNS (DNS is IPv4-only in docker).
Steps
Enable IPv6 in Docker daemon.
Create a docker-compose.yml file that creates an ipv6 network, a volume for shared files, and two containers
Run an entrypoint script in each container that performs the aforementioned steps.
Files
docker-compose.yml
# Note: enable_ipv6 does not work in version 3!
version: '2.1'
networks:
app_net:
enable_ipv6: true
driver: overlay
ipam:
driver: default
config:
-
subnet: 172.16.238.0/24
-
subnet: 2001:3984:3989::/64
services:
app1:
build: ./server
hostname: server1
command: blablabla # example of arg passing to ipv6.sh
cap_add:
- NET_ADMIN
volumes:
- ipv6stuff:/ipv6stuff
networks:
- app_net
app2:
build: ./server
hostname: server2
command: SOMETHING # example of arg passing to ipv6.sh
cap_add:
- NET_ADMIN
volumes:
- ipv6stuff:/ipv6stuff
networks:
- app_net
volumes:
ipv6stuff:
server/Dockerfile
FROM alpine:latest
ADD files /
RUN apk --update add bash #simpler scripts
# Has to be an array for parameters to work via command: x in compose file, if needed
ENTRYPOINT ["/ipv6.sh"]
server/files/ipv6.sh
#!/bin/bash
# Optionally conditional logic based on parameters here...
# (for example, conditionally leave ipv4 address alone in some containers)
#
# Remove ipv4
ifconfig eth0 0.0.0.0
IP6=$(ip addr show eth0 | grep inet6 | grep global | awk '{print $2}' | cut -d / -f 1)
echo "Host $HOSTNAME has ipv6 ip $IP6"
# Store our entry in the shared volume
echo "$IP6 $HOSTNAME" > /ipv6stuff/hosts.$HOSTNAME
# Remove existing ipv4 line from /etc/hosts just to be thorough
# Docker does not allow removal of this file and thus simple sed -i isn't going to work.
cp /etc/hosts /tmp/1 ; sed -i "s/^.*\s$HOSTNAME//" /tmp/1 ; cat /tmp/1 > /etc/hosts
# Wait for all containers to start
sleep 2
# Put everyone's entries in our hosts file.
cat /ipv6stuff/hosts.* >> /etc/hosts
echo "My hosts file:"
cat /etc/hosts
# test connectivity (hardcoded)
ping6 -c 3 server1
ping6 -c 3 server2
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.