Assign static ip on docker 1.7 - docker

Is there a way for setting a static IP on docker 1.7? I am currently running RedHat 6 so can only use Docker version up to 1.7.
The problem I am having is that default IP Docker defaults to an IP and its clashing with the one my server uses. Is there a way to specifically tell Docker to use a certain IP 127.0.0.2 for example?

You can use docker-compose to start up your containers. In that file you can define ipv4_address which will assign an static IP to your container. Here is an example
version: "2"
services:
SERVICE1:
image: $IMAGE_NAME
container_name: $CONTAINER_NAME
ports:
- "8080:8080"
networks:
mynet:
ipv4_address: 172.25.0.100
networks:
mynet:
driver: bridge
ipam:
config:
- subnet: 172.25.0.0/24

Related

Expose docker container to the external network

I have started learning about Docker and containers, and have been given an assignment to "Host a docker container on the external network (the one that the host is connected to) with its own IP address that is valid for said network".
As far as my understanding goes, Docker containers allow to forward ports onto the host, without exposing the docker container to the outside network. Is there any way to expose the whole container, with all its ports and have its own IP onto the external network?
Here is a excerpt from a test docker-compose.yaml file:
env20:
build: ./env20
image: env20
container_name: env20
hostname: env20
ports:
- "22:22/tcp"
- "80:80/tcp"
depends_on:
- mysql
networks:
gnet:
ipv4_address: 10.10.11.30
restart: unless-stopped
#############################################################
# Netowrk setup
#############################################################
networks:
gnet:
name: gnet
driver: macvlan
ipam:
driver: default
config:
- subnet: 10.10.11.0/24
gateway: 10.10.11.1
Any help would be appreciated!

How to config networks in docker-compose?

In the host, I have created docker network using below command:
docker network create -d macvlan \
--subnet=192.168.2.0/24 \
--gateway=192.168.2.1 \
-o parent=eth0 pub_net
docker network shows below:
[root#192-168-2-70 shell]# docker network ls
NETWORK ID NAME DRIVER SCOPE
935ae8b52eb2 bridge bridge local
81577f72f606 host host local
846d54a2c7da none null local
158428b19c4d pub_net macvlan local
[root#192-168-2-70 shell]#
and docker container with specified IP can be started by command:
docker run --net=pub_net --ip=192.168.2.10 --name hadoop0 --hostname
hadoop0 -d -P hadoop-cluster-hadoop0:v1.1
but when I convert the above command to docker-compose file like below:
version: '3'
services:
hadoop-cluster-hadoop0:
container_name: hadoop0
restart: always
networks:
pub_net:
ipv4_address: 192.168.2.10
volumes:
- /tmp/hadoop0/logs:/tmp
extra_hosts:
- "hadoop0:192.168.1.10"
- "hadoop1:192.168.1.11"
- "hadoop2:192.168.1.12"
- "dbus-n1:192.168.2.81"
image: 'hadoop-cluster-hadoop0:v1.1'
networks:
put_net:
external: true
the following error occured.
[root#192-168-2-70 shell]# docker-compose up -d
WARNING: Some networks were defined but are not used by any service: put_net
ERROR: Service "hadoop-cluster-hadoop0" uses an undefined network "pub_net"
[root#192-168-2-70 shell]#
Can anybody please help me on how to correct the docker-compose file?
How docker network can be created in docker-compose file, instead created beforehand?
Appreciate your help very much!!!
Look closely, you named your network put_net but instead referenced it as pub_net in your service definition.
Moreover, when you define the network as external it means that it should already be created. If you want docker-compose to do that for you, you can do this instead.
networks:
put_net:
driver: bridge
ipam:
driver: default
config:
-
subnet: 192.168.2.0/24
If you would like to exactly re-create the network you specified in the question then you would have to downgrade the version from '3' to '2'; This is because some functionality was removed in version '3' and one of these options was the ability to specify gateway (at least to the best of my knowledge).
Here is how your docker-compose file would look like. (Notice the change in version from '3' to '2')
version: '2'
services:
hadoop-cluster-hadoop0:
container_name: hadoop0
restart: always
networks:
put_net:
ipv4_address: 192.168.2.10
volumes:
- /tmp/hadoop0/logs:/tmp
extra_hosts:
- "hadoop0:192.168.1.10"
- "hadoop1:192.168.1.11"
- "hadoop2:192.168.1.12"
- "dbus-n1:192.168.2.81"
image: 'hadoop-cluster-hadoop0:v1.1'
networks:
put_net:
driver: macvlan
driver_opts:
parent: eth0
ipam:
config:
-
subnet: 192.168.2.0/24
gateway: 192.168.2.1

docker containers with mac-vlan network show wrong ip after being restarted?

Hi everyone i have create a network with mac-vlan type in docker because i wanted my containers to be on the same LAN as host.Now the strange thing which i have noticed is that when i stop and then restart a container with docker start command the container gets started but the IP assigned to it is the one that was assigned before the container was shutdown. doesn't IP change when containers are restarted furthermore the container is now not reachable because the IP its showing as its own has now been reassigned to another machine on the network from what i have read that the container is assigned the same IP as before but if the container couldn't get the IP it fails to start but my container is starting just fine. What am i missing here? on ubuntu version 17.10 docker version 17.11.0-ce Api version 1.34 (both client and server)
You should not use static IP's in docker unless you are working with something that allows routing from outside to the inside container, like in you're case macvlan. DNS is already there for service discovery inside of the container network and supports container scaling. And outside the container network, you should use exposed ports on the host.
That being said, you can achieve the above using docker-compose like below :
services:
mysql:
container_name: backend-database
image: mysql:latest
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
ports:
- "3306:3306"
networks:
mynetwork:
ipv4_address: 10.5.0.5
apache-tomcat:
container_name: apache-tomcat
build: tomcat/.
ports:
- "8080:8080"
- "8009:8009"
networks:
mynetwork:
ipv4_address: 10.5.0.6
depends_on:
- mysql
networks:
mynetwork:
driver: bridge
ipam:
config:
- subnet: 10.5.0.0/16
gateway: 10.5.0.1

Docker communicate with docker using static IP

Is that possible to specify a static IP for docker container using docker-compose?
eth-java:
image:
registry-intl.ap-southeast-1.aliyuncs.com/einnity/coin-ethereum:1.0
container_name:
eth-java
ports:
- "8002:8198"
networks:
my-network:
ipv4_address: 192.168.1.21
And this container will communicate with
eth:
image:
ethereum/client-go
container_name:
eth
ports:
- "8545:8545"
- "30303:30303"
networks:
my-network:
ipv4_address: 192.168.1.17
volumes:
- /storage/eth/rinkeby:/root/.ethereum/rinkeby/
and the network settings is
networks:
my-network:
driver: bridge
ipam:
driver: default
config:
- subnet: 192.168.1.0/24
I type docker exec -it eth-java /bin/bash. Then I type curl and call RPC on 192.168.1.17:8545, it doesnt work. If don't hardcode the IP and use the dynamic IP, this will works. I just hate using dynamic IP because everytime when restarting the docker container, another IP will be given and I need to change my DB value every time.

Unable to listen to a service running inside a docker container from remote docker container

I have two machines: machine-A and machine-B. Both are on different networks. I create a docker container on machine-A using docker-compose.yml and run litecoind process within it on port 12345. I have forwarded port 12345 to the port 80 of the host machine-A.
version: '3'
services:
node1:
build: .
cap_add:
- ALL
command: litecoind -regtest -server -rpcuser=rpc -rpcpassword=x -rpcport=10340 --datadir=/root/litecoind-simnet/ -port=12345
networks:
vpcbr:
ipv4_address: 10.9.0.11
ports:
- 80:12345
networks:
vpcbr:
driver: bridge
ipam:
config:
- subnet: 10.9.0.0/16
Now on machine-B, I can directly connect to the above process with -addnode option of litecoin and can see the blockchains syncing.
Problem arises when I create a container on machine-B and try to connect to the same above process with -addnode by using the docker-compose.yml file on machine-B. In this case, the litecoind process remains invisible and the blockchains do not sync.
version: '3'
services:
node1:
build: .
cap_add:
- ALL
command: litecoind -regtest -addnode=<x.x.x.x:80> -rpcuser=rpc -rpcpassword=x -rpcport=10340 --datadir=/root/litecoind-simnet/ -port=12345
networks:
vpcbr:
ipv4_address: 10.8.0.11
ports:
- 90:12345
networks:
vpcbr:
driver: bridge
ipam:
config:
- subnet: 10.8.0.0/16
I want the above two separate containers on two separate remote machines to communicate with each other. What am I missing? Help please. Thanks.
The possible solutions are
Use a single docker-compose file to deploy both the containers on same node.
If your requirement is to absolutely deploy the containers on two different nodes then you need to create a swarm cluster if you are using compose.
If you want to create two different compose file on same node this is the answered here

Resources