How to config networks in docker-compose? - docker

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

Related

docker-compose run with specified network name

I have a docker-compose file with three services (Solr, PostgreSQL and pgAdmin), all sharing a Docker network.
version: '2'
services:
solr:
image: solr:7.7.2
ports:
- '8983:8983'
networks:
primus-dev:
ipv4_address: 10.105.1.101
volumes:
- data:/opt/solr/server/solr/mycores
entrypoint:
- docker-entrypoint.sh
- solr-precreate
- primus
- /opt/solr/server/solr/configsets/sample_techproducts_configs
environment:
- SOLR_HEAP=2048m
logging:
options:
max-size: 5m
db:
image: "postgres:11.5"
container_name: "primus_postgres"
ports:
- "5432:5432"
networks:
primus-dev:
ipv4_address: 10.105.1.102
volumes:
- primus_dbdata:/var/lib/postgres/data
environment:
- POSTGRES_DB=primus75
- POSTGRES_USER=primus
- POSTGRES_PASSWORD=primstav
pgadm4:
image: "dpage/pgadmin4"
networks:
primus-dev:
ipv4_address: 10.105.1.103
ports:
- "3050:80"
volumes:
- /home/nils/docker-home:/var/docker-home
environment:
- PGADMIN_DEFAULT_EMAIL=nils.weinander#kulturit.se
- PGADMIN_DEFAULT_PASSWORD=dev
networks:
primus-dev:
driver: bridge
ipam:
config:
- subnet: 10.105.1.0/24
volumes:
data:
primus_dbdata:
This works just fine after docker-compose up (at least pgAdmin can talk to PostgreSQL).
But, then I have a script (actuall a make target, but that's not the point here), which builds, runs and deletes a container with docker-compose run:
docker-compose run -e HOME=/app -e PYTHONPATH=/app/server -u 0 --rm backend \
bash -c 'cd /app/server && python tools/reindex_mp.py -s -n'
This does not work as the reindex_mp.py cannot reach Solr on 10.105.1.101, as the one shot container is not on the same Docker network. So, is there a way to tell docker-compose to use a named network with docker-compose run? docker run has an option --network but that is not available for docker-compose.
You can create a docker network outside your docker-compose and use that network while running services in docker-compose.
docker network create my-custom-created-network
now inside your docker-compose, use this network like this:
services:
serv1:
image: img
networks:
my-custom-created-network
networks:
my-custom-created-network:
external: true
The network creation example creates a bridge network.
To access containers across hosts, use an overlay network.
You can also use the network created inside docker-compose and connect containers to that network.
Docker creates a default network for docker-compose and services which do not have any network configuration specified, will be using default network created by docker for that compose file.
you can find the network name by executing this command:
docker network ls
Use the network appropriate name while starting a container, like this
docker run [options] --network <network-name> <image-name>
Note: Containers in a same network are accessible using container names, you can leverage this instead of using ips

Why does docker-compose say a network is undefined, but "docker network ls" shows it?

I have created a network using:
docker network create my_network
And when I run docker network ls I see:
NETWORK ID NAME DRIVER SCOPE
80e99e7a8f98 bridge bridge local
ff48b8c6586b host host local
cdf5969b458d none null local
9bd1e13004b7 my_network bridge local
but when I try to create a redis node using that network, it says:
Service "redis" uses an undefined network "my_network"
docker-compose.yml
version: '2.1'
services:
redis:
image: redis
container_name: redis
environment:
- ALLOW_EMPTY_PASSWORD=yes
ports:
- 6379:6379
networks:
- my_network
Any idea why?
You need to define a pre-existing network in your docker-compose file, as described in the documentation.
networks:
my_network:
external:
name: my_network

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.

Docker Compose Flags

I don't how to run the docker-compose equivalent of my code
docker run -d --name=server --restart=always --net network --ip 172.18.0.5 -p 5003:80 -v $APP_PHOTO_DIR:/app/mysql-data -v $APP_CONFIG_DIR:/app/config webserver
I've done this:
version: '3'
services:
server:
image: app-dependencies
ports:
- "5003:80"
volumes:
- ./app:/app
command: python /app/app.py
restart: always
networks:
app_net:
ipv4_address: 172.18.0.5
Are you sure you need an IP address for container? It is not recommended practice, why do you want to set it explicitly?
docker-compose.yml
version: '3'
services:
server: # correct, this would be container's name
image: webserver # this should be image name from your command line
ports:
- "5003:80" # correct, but only if you need to communicate to service from ouside
volumes: # volumes just repeat you command line, you can use Env vars
- $APP_PHOTO_DIR:/app/mysql-data
- $APP_CONFIG_DIR:/app/config
command: ["python", "/app/app.py"] # JSON notation strongly recommended
restart: always
Then docker-compose up -d and that's it. You can access your service from host with localhost:5003, no need for internal IP.
For networks, I always include in the docker-compose file, the network specification. If the network already exists, docker will not create a new one.
version: '3'
services:
server:
image: app-dependencies
ports:
- "5003:80"
volumes:
- ./app:/app
command: python /app/app.py
restart: always
networks:
app_net:
ipv4_address: 172.18.0.5
networks:
app_net:
name: NETWORK_NAME
driver: bridge
ipam:
config:
- subnet: NETWORK_SUBNET
volumes:
VOLUME_NAME:
driver:local
And you will need to add the volumes separately to match the docker run command.

Assign static ip on docker 1.7

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

Resources