I am quite new to docker but after watching a few tutorials I gathered that if I wanted my services to have common code I had to use docker-compose.
This is my directory structure:
- project
- docker-compose.yml
- packages
- common
- src
- eureka
- eureka-helper.js
- gateway
- Dockerfile
- src
- (some more directories and files)
- server.js
- users
- Dockerfile
- src
- app.js
- (some more directories and files)
version: '3'
services:
eureka:
image: springcloud/eureka
ports:
- "8761:8761"
gateway:
build:
context: ./
dockerfile: ./packages/gateway/Dockerfile
ports:
- "3000:3000"
links:
- eureka
users:
build:
context: ./
dockerfile: ./users/Dockerfile
ports:
- "3004:3004"
links:
- mongo
- eureka
mongo:
image: mongo
volumes:
- C:\Users\myUser\docker\mongodb-data:/data/db
ports:
- "27017:27017"
My server.js (inside gateway) and app.js (inside users) call a function inside eureka-helper that registers with Eureka.
eurekaHelper.registerWithEureka('gateway', eureka, port);
But it seems that server.js doesn't have a value for this eureka:
gateway_1 | /opt/app/gateway/server.js:27
gateway_1 | eurekaHelper.registerWithEureka('gateway', eureka, port);
gateway_1 | ^
gateway_1 |
gateway_1 | ReferenceError: eureka is not defined
As per this: https://docs.docker.com/compose/networking/, it doesn't seem that I really need the link in docker-compose:
Links allow you to define extra aliases by which a service is reachable from another service. They are not required to enable services to communicate - by default, any service can reach any other service at that service’s name.
... but gateway is not able to reach eureka anyways.
Any idea if I'm missing something.
When I do inspect in gateway:
"Networks": {
"*project*_default": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"gateway",
"a358b371de9d"
],
"NetworkID": "d986ee77bf6181e9dd59711650359dc49fd8ce3fc34c6772133a0049ab55bd3b",
"EndpointID": "9633a7affe435793345342d4f6d00d5bef43d2267676448dfde0f97d75d5f7af",
"Gateway": "172.19.0.1",
"IPAddress": "172.19.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:13:00:03",
"DriverOpts": null
}
}
When I do inspect in eureka:
"Networks": {
"*project*_default": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"eureka",
"17a218bf6e9e"
],
"NetworkID": "d986ee77bf6181e9dd59711650359dc49fd8ce3fc34c6772133a0049ab55bd3b",
"EndpointID": "43f4a9eb9efd4d30ad65d2c943065b8fb32ed50822bd553e07a2b0020c173fec",
"Gateway": "172.19.0.1",
"IPAddress": "172.19.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:13:00:02",
"DriverOpts": null
}
}
Related
I cannnot use network_mode: host in docker in windows 10. I try difference images, it cannot too.
I post my docker-compose.yaml below.
docker-compose.yaml
homeassistant2:
container_name: hass2
image: homeassistant/home-assistant:0.112.2
network_mode: host
But when set the port mapping,everthing is fine
ports:
- '8123:8123'
# network_mode: host
My environmet is windows 10
I get some information from command docker inspect
I post some of it about network settings below.
"NetworkSettings": {
"Bridge": "",
"SandboxID": "f084a1e7e4966f0bff678e4120c1bcbc7afdd1c55d5164b1dc4a606470c43bcc",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/default",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"host": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "656b532cdc48c9e34e616ec0a38ef944c5e52e2dfca506ca99e53b4b16206000",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
If I need provide something else to analyze the problem, please tell me.
When running Docker under Windows or MacOS, you're actually running Docker in a Linux virtual machine. When you set network_mode: host, you're connecting the container to the network environment of the virtual machine. This is seldom going to be useful.
network_mode: host is really only going to make sense when running Docker natively on Linux.
You're better off sticking with port publishing.
First step: I loaded the Images from a local drive:
docker load -i postgres10.tar
docker load -i drupaldrush1.tar
Second step: I started the containers:
docker run -p5432:5432 postgres:10
docker run -p8081:8081 drupaldrush:1
Third step: displaying containers:
docker ps
results in:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b77bcc79d599 drupaldrush:1 "docker-php-entrypoi…" 33 seconds ago Up 32 seconds 80/tcp, 0.0.0.0:8081->8081/tcp flamboyant_easley
97b9ba5f2779 postgres:10 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:5432->5432/tcp competent_fermat
BUT container is not available under: localhost:8081
Fourth step: inspecting container:
docker inspect flamboyant_easley
resulting in (among other Information):
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"8081/tcp": [
{
"HostIp": "",
"HostPort": "8081"
}
]
},
and
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "d5e552bf9c57050fe2debfc7d38a784580309fa0b72c4854a563e78295128912",
"EndpointID": "f61b02c5997b2e391add348686f658b4c596dd60495365cee0fee539743d4792",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
Problem: The container is not available under localhost:8081 and not under 172.17.0.3: 8081 or 172.17.0.1:8081
Question: What do I have to do to make it run under localhost:8081?
The problems came from Docker for Windows. Now I got a Linux Laptop and everything works just fine.
In order to make every container part of the default bridge, I added network_mode: bridge in every service. These became part of bridge but the containers are not getting attached with the hostnames. Below is the config.
docker-compose.yml
version: '2'
services:
elasticsearch:
build:
context: elasticsearch/
args:
ELK_VERSION: $ELK_VERSION
volumes:
- ./elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:ro
ports:
- "9200:9200"
- "9300:9300"
environment:
ES_JAVA_OPTS: "-Xmx256m -Xms256m"
network_mode: bridge
hostname: elasticsearch
logstash:
build:
context: logstash/
args:
ELK_VERSION: $ELK_VERSION
volumes:
- ./logstash/config/logstash.yml:/usr/share/logstash/config/logstash.yml:ro
- ./logstash/pipeline:/usr/share/logstash/pipeline:ro
ports:
- "5000:5000"
- "9600:9600"
environment:
LS_JAVA_OPTS: "-Xmx256m -Xms256m"
network_mode: bridge
depends_on:
- elasticsearch
kibana:
build:
context: kibana/
args:
ELK_VERSION: $ELK_VERSION
volumes:
- ./kibana/config/:/usr/share/kibana/config:ro
ports:
- "5601:5601"
network_mode: bridge
depends_on:
- elasticsearch
Docker compose up
$ docker-compose up -d
Creating docker-elk_elasticsearch_1 ... done
Creating docker-elk_kibana_1 ... done
Creating docker-elk_logstash_1 ... done
Docker network inspect
$ docker network inspect bridge
[
{
"Name": "bridge",
"Id": "f561a85fb2b22bbf251545c7021d57020cf152bd3a5c3c061c7d6b0cb4e267e5",
"Created": "2018-09-19T07:02:49.36259364Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"0aedc2ce900b8a51f028e58a85c8db9480fd2816874a608540a899a4daab32fd": {
"Name": "docker-elk_kibana_1",
"EndpointID": "df3af338e0accb880ccc44323e5581064ee8ef84574485f1928d12dc415b598e",
"MacAddress": "02:42:ac:11:00:05",
"IPv4Address": "172.17.0.5/16",
"IPv6Address": ""
},
"3f2088847bd8e958a047093b1af879c91c4071f57f0105bb7bf80fb8df832d41": {
"Name": "docker-elk_logstash_1",
"EndpointID": "6588b7eece43144833ae2f9ffe753e3cc6c70d0891a587c3e9a4e9ca84993532",
"MacAddress": "02:42:ac:11:00:06",
"IPv4Address": "172.17.0.6/16",
"IPv6Address": ""
},
"ace35bb6fadd50823f64e9075b5972e6e3b24e8b73273a41e7a48f9eeff89da1": {
"Name": "roach",
"EndpointID": "dd058e3e9f46b2459f14a2e5bdf96eae277e81dcf7ac2e6ac1c97d8220ead30d",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
},
"f90378063d2a0157110b77af39f2526347f1ea9634839e0d2c0c584fb14ff957": {
"Name": "docker-elk_elasticsearch_1",
"EndpointID": "294a2f67196788135f370bbf83526395ba4401afb25db9eb0b59fba7fd358912",
"MacAddress": "02:42:ac:11:00:04",
"IPv4Address": "172.17.0.4/16",
"IPv6Address": ""
},
"f954c218e5ab15c83c2a0e2c848549c18879613f6f46d07f7ebf71cc89b6e55b": {
"Name": "rabbitmq",
"EndpointID": "e675ddc6076fe2256553e8b367a82aa36f488457e06ae6cf969c2e04feeb9fb8",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
Docker inspect elasticsearch
$ docker inspect docker-elk_elasticsearch_1
"NetworkSettings": {
"Bridge": "",
"SandboxID": "31a438f8fcb3dd8efca37260e77d346f21239b36d8bb30f5f08db4b79880a5c9",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"9200/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "9200"
}
],
"9300/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "9300"
}
]
},
"SandboxKey": "/var/run/docker/netns/31a438f8fcb3",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "294a2f67196788135f370bbf83526395ba4401afb25db9eb0b59fba7fd358912",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:04",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "f561a85fb2b22bbf251545c7021d57020cf152bd3a5c3c061c7d6b0cb4e267e5",
"EndpointID": "294a2f67196788135f370bbf83526395ba4401afb25db9eb0b59fba7fd358912",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:04",
"DriverOpts": null
}
}
}
Kibana Logs where elastic search is inaccessible:
$ docker logs docker-elk_kibana_1
{"type":"log","#timestamp":"2018-09-20T05:27:05Z","tags":["warning","elasticsearch","admin"],"pid":1,"message":"Unable to revive connection: http://elasticsearch:9200/"}
{"type":"log","#timestamp":"2018-09-20T05:27:05Z","tags":["warning","elasticsearch","admin"],"pid":1,"message":"No living connections"}
However, everything works fine with below config where I haven't provided any network related config.
version: '2'
services:
elasticsearch:
build:
context: elasticsearch/
args:
ELK_VERSION: $ELK_VERSION
volumes:
- ./elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml:ro
ports:
- "9200:9200"
- "9300:9300"
environment:
ES_JAVA_OPTS: "-Xmx256m -Xms256m"
logstash:
build:
context: logstash/
args:
ELK_VERSION: $ELK_VERSION
volumes:
- ./logstash/config/logstash.yml:/usr/share/logstash/config/logstash.yml:ro
- ./logstash/pipeline:/usr/share/logstash/pipeline:ro
ports:
- "5000:5000"
- "9600:9600"
environment:
LS_JAVA_OPTS: "-Xmx256m -Xms256m"
depends_on:
- elasticsearch
kibana:
build:
context: kibana/
args:
ELK_VERSION: $ELK_VERSION
volumes:
- ./kibana/config/:/usr/share/kibana/config:ro
ports:
- "5601:5601"
depends_on:
- elasticsearch
Containers on the default bridge network cannot refer each other by host name, they can only refer each other by IP. You can find this in the docs here https://docs.docker.com/network/bridge/#differences-between-user-defined-bridges-and-the-default-bridge.
Containers on the default bridge network can only access each other by IP addresses, unless you use the --link option, which is considered legacy. On a user-defined bridge network, containers can resolve each other by name or alias.
The solution is to define your custom bridge network in the Compose file with networks as described here https://docs.docker.com/compose/compose-file/#networks and add every container to this user defined network. On this network containers can resolve each other by name.
Here is my docker-compose file:
version: '2'
services:
app:
image: mykiwi/ttrss
database:
image: postgres:10-alpine
environment:
- POSTGRES_USER=ttrss
- POSTGRES_PASSWORD=ttrss
volumes:
- database:/var/lib/postgresql/data
volumes:
database: ~
But when I inspect my main container, there is no ip address:
$ docker inspect 40f78d12c178
...
"NetworkSettings": {
"Bridge": "",
"SandboxID": "70479dc6e987e744e376f6e4222e8d26174b724b686da52924a7e539ef4d4f0c",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/70479dc6e987",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"none": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "2c6c546b04398c4b4c7b9e52fdb16b89aacfea33b9038b6aa90b4b44fbd99ac7",
"EndpointID": "ac4da8b1b38118732a6e9e99a0ba56b271e6810afd78975c5f329185cf047bf7",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
I run it through rancher, is it related ?
Rancher uses CNI drivers for networking and so the network is "none" as far as Docker is concerned and no IP addresses or port info appears in Docker inspect. You can get it from the metadata service.
I have a set of containers running in Windows 10 with Docker, this is the output of docker-compose ps:
> docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------------------
db_mysql docker-entrypoint.sh mysqld Up 3306/tcp
elk /usr/bin/supervisord -n -c ... Up 0.0.0.0:81->80/tcp
php71-fpm-nginx /config/bootstrap.sh Up 443/tcp, 0.0.0.0:80->80/tcp, 0.0.0.0:9001->9001/tcp
And this is the output of docker inspect:
> docker inspect php71-fpm-nginx
[
{
...
"NetworkMode": "anotherlampdocker_default",
"PortBindings": {
"80/tcp": [
{
"HostIp": "",
"HostPort": "80"
}
],
"9001/tcp": [
{
"HostIp": "",
"HostPort": "9001"
}
]
},
...
"Mounts": [
{
"Source": "/d/Development/www",
"Destination": "/data/www",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
},
...
"NetworkSettings": {
"Bridge": "",
"SandboxID": "b96cf4c0f2c17d65659c31982b9200a79cca6f1c214770d31938204c493a6720",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"443/tcp": null,
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "80"
}
],
"9001/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "9001"
}
]
},
...
"Networks": {
"anotherlampdocker_default": {
"IPAMConfig": null,
"Links": [
"db_mysql:db",
"db_mysql:db_mysql"
],
"Aliases": [
"php-fpm",
"248e8c254eee"
],
"NetworkID": "de1e10b63e6e5050809af59ac4d26b7cb691afd5805d1cf7f0492c702814f34d",
"EndpointID": "ccec71967c6100c5a9f3ad82d82bbb2a371f77e12c493bf05bfd15f2d188ce00",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:03"
}
}
}
}
]
I am trying to access http://localhost but I can't. I've tried also using the container IP meaning http://172.18.0.3 and doesn't work either.
When I say I can't it means the following message:
This site can’t be reached
172.18.0.3 took too long to respond.
The same container works perfectly in Linux.
This is the content of c:\Windows\System32\drivers\etc\hosts file:
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
The ports from the container are mapped to the host in the docker-compose.yml file:
services:
php-fpm:
container_name: "php71-fpm-nginx"
build: php-fpm
ports:
- 80:80
- 9001:9001
environment:
PHP_ERROR_REPORTING: 'E_ALL & ~E_DEPRECATED & ~E_NOTICE'
STATUS_PAGE_ALLOWED_IP: '127.0.0.1'
volumes:
- D:\Development\www\:/data/www
What I am missing here?
You can get the docker machine IP and access the application:
1. Using command docker-machine : docker-machine ip
2.or By login to the docker image which is created when you start the docker and getting the eth1 ip
Then try : [docker-machine ip]:[port]
Try login to the php's container and check if the httpd process is running.
Use docker-compose up without -d should help you to see the log from container or use docker-compose logs -f -t after docker-compose up -d