Docker Compose Links not working with Host mode - docker

Having issues getting docker links to work if I use "host" mode. How do you access the other linked docker container if it is using "host" mode?
An example:
If I use these 2 compose files below, I can run the following:
$ docker-compose up
$ docker exec -it [CONTAINER ID OF REDIS1] bash
$ redis-cli -h redis2 [OR redis-cli -h redis2-alias]
$ PING => you will get back PONG from redis2
docker-compose.yml
version: "2"
services:
redis1:
image: "redis"
ports:
- "6379"
links:
- redis2:redis2-alias
redis2:
extends:
file: docker-compose.redis2.yml
service: redis
docker-compose.redis2.yml
version: "2"
services:
redis:
image: "redis"
ports:
- "6379"
however, if you change docker-compose.redis2.yml to use host mode. when you try to connect to redis2 (from redis1)...it just hangs and never connects
docker-compose.redis2.yml
version: "2"
services:
redis:
image: "redis"
network_mode: "host"
$ docker-compose up
$ docker exec -it [CONTAINER ID OF REDIS1] bash
$ redis-cli -h redis2 => this just hangs...never connects to redis2
how do you connect to redis2 (when it is in host mode) from redis1?

Links are not supported with --net=host. Links are also deprecated (in philosophy) now. Prefer using a custom network.

Looks like Docker chose not to support this use case due to its complexity, see the GitHub issue here.

Related

how to use docker container Options inside docker-compose

i am using akka http server in my app and mongodb as a backed database, akka http uses standard input to keep running the server,
here is how i am binding it
val host = "0.0.0.0"
val port = 8080
val bindingFuture = Http().bindAndHandle(MainRouter.routes, host, port)
log.info("Server online ")
StdIn.readLine()
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
i need to dockerize my app docker closes the standard input by default when it starts the container, to keep it running we need to provide -i option with the container like this
docker run -p 8080:8080 -i imagename:tag
now the problem is i need to use docker-compose to start my app with mongo
here is my docker-compose.yml
version: '3.3'
services:
mongodb:
image: mongo:4.2.1
container_name: docker-mongo
ports:
- "27017:27017"
akkahttpservice:
image: app:0.0.1
container_name: docker-app
ports:
- "8080:8080"
depends_on:
- mongodb
how can i provide the -i option with docker-app container
Note after doing docker-compose up
docker exec -it containerid sh
did not worked for me
Any help would be appreciated

Is it possible to curl across docker network via docker-compose between 2 docker-compose.yaml?

I have 2 application run with a different network and it uses separate docker-compose.yaml. So I trying to call an request from app A to app B, but it not works.
docker exec -it app_a_running curl http://localhost:8012/user/1
So I got an error
cURL error 7: Failed to connect to localhost port 8012
docker-compose-app-a.yaml
version: "3"
services:
app:
build: go/
restart: always
ports:
- 8011:8011
volumes:
- ../src/app:/go/src/app
working_dir: /go/src/app
container_name: app-a
command: sleep 72000
networks:
- app-a-network
networks:
app-a-network:
docker-compose-app-b.yaml
version: "3"
services:
app:
build: go/
restart: always
ports:
- 8012:8012
volumes:
- ../src/app:/go/src/app
working_dir: /go/src/app
container_name: app-b
command: sleep 72000
networks:
- app-b-network
networks:
app-b-network:
Questions:
Is it possible to do this?
If the first question is possible, Please suggest me :)
You can use curl on docker containers. The reason why your curl command didn't work is probably that you did not publish your docker container's port. For example, try:
docker run -d -p 8080:8080 tomcat
instead of
docker run -d tomcat
This will forward the port 8080 of your machine to the port 8080 of your container.
If you have a shell to your container, you can use the service name or the container's name to curl a container on your Docker network, provided your target exists with the same network.

Connecting FTP container works with docker-compose and not with docker run

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

Interact with redis container started with docker compose

I have a docker compose file that links my server to a redis image:
version: '3'
services:
api:
build: .
command: npm run dev
environment:
NODE_ENV: development
volumes:
- .:/home/node/code
- /home/node/code/node_modules
- /home/node/code/build/Release
ports:
- "1389:1389"
depends_on:
- redis
redis:
image: redis:alpine
I am wondering how could I open a redis-cli against the Redis container started by docker-compose to directly modify ke/value pairs. I tried with docker attach but it does not open any shell.
Use docker exec -it your_container_name /bin/bash to enter into redis container, then execute redis-cli to modify key-value pair.
See https://docs.docker.com/engine/reference/commandline/exec/
Install the Redis CLI on your host. Edit the YAML file to publish Redis's port
services:
redis:
image: redis:alpine
ports: ["6379:6379"]
Then run docker-compose up to redeploy the container, and you can run redis-cli from the host without needing to directly interact with Docker.
Using /bin/bash as the command (as suggested in the accepted solution) doesn't work for me with the latest redis:alpine image on Linux.
Instead, this worked:
docker exec -it your_container_name redis-cli

Connect to Redis Docker container from Vagrant machine

We're making move to Docker from Vagrant.
Our first aim is to move some services out first. In this case I'm trying to host a redis server on a docker container and connect to it from my vagrant machine.
On the vagrant machine there is an apache2 webserver hosting a Laravel App
It's the connection part I'm struggling with, currently I have
Dockerfile.redis
FROM redis:3.2.12
RUN redis-server
docker-compose.yml (concatenated)
version: '3'
services:
redis:
build:
context: .
dockerfile: Dockerfile.redis
working_dir: /opt
ports:
- "6379:6379"
I've tried various way to connect to this:
Attempt 1
Using the host ip 10.0.2.2 in the config in Laravel. Results in a "Connection refused"
Attempt 2
Set up a network in the docker compose
redis:
build:
context: .
dockerfile: Dockerfile.redis
working_dir: /opt
network:
- app_net:
ipv4_address: 172.16.238.10
ports:
- "6379:6379"
networks:
app_net:
driver: bridge
ipam:
driver: default
- subnet: 172.16.238.0/24
This instead results in timeouts. Most solutions seem to require a gateway configured on the network, but this isn't configurable in docker compose 3. Is there maybe a way around this?
If anyone can give any guidance that would be great, most guides talk about connect to dockers in a vagrant rather than from one.
FYI - this is using Docker for Mac and version 3 of docker compose
We were able to get this going use purely docker compose and not having a dockerfile for redis at all:
redis:
image: redis
container_name: redis
working_dir: /opt
ports:
- "6379:6379"
Once done like this, able to connect to redis from within the vagrant file using
redis-cli -h 10.0.2.2
Or as the following in laravel, although we're using environment variables to set these)
'redis' => [
'client' => 'phpredis',
'default' => [
'host' => '10.0.2.2',
'password' => null,
'port' => 6379,
'database' => 0,
]
]
Your Attempt 1 should work actually. When you create a service without defining a network, docker-compose automatically creates a bridge network. For example:
When you run docker-compose up on this:
version: '3'
services:
redis:
build:
context: .
dockerfile: Dockerfile.redis
working_dir: /opt
ports:
- "6379:6379"
docker-compose creates a bridge network named <project name>_default, which is docker_compose_test_default in my case, as shown below:
me#myshell:~/docker_compose_test $ docker network ls
NETWORK ID NAME DRIVER SCOPE
6748b1ea4b85 bridge bridge local
4601c6ea30c3 docker_compose_test_default bridge local
80033acaa6e4 host host local
When you inspect your container, you can see that an IP has already been assigned to it:
docker inspect e6b196f952af
...
"Networks": {
"bridge": {
...
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.2",
You can then use this IP to connect from the host or your vagrant box:
me#myshell:~/docker_compose_test $ redis-cli -h 172.18.0.2 -p 6379
172.18.0.2:6379> ping
PONG

Resources