What is the docker compose equivalent of running a docker container with network of container === host network ?
docker run --rm -d --network host --name my_nginx nginx
Basically, how to specify the --network host inside docker-compose
Currently, I am running this by just specifying the port mappings from host to docker container, where I use the same port on both
eg :
version: '3.8'
services:
api-server:
build: '.'
ports:
- "8080:8080"
You need to specify the network mode inside your service at docker-compose.yml. Add it like this:
version: '3.8'
services:
network_mode: "host"
api-server:
build: '.'
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
With plain docker I can use network mode container like this:
docker run -d --name container-b --network container:container-a <image>
Can this be achieved using docker-compose?
services:
service-b:
container_name: container-b
network_mode: "container:container-a"
Leads to:
ERROR: Please provide 'network_mode: "bridge"' or 'network_mode: "host"' in your docker-compose.yaml
I have a docker-compose file with version '2.1', but I am able to do this:
services:
service-b:
container_name: container-b
network_mode: service:container-a
This makes all of my container-b traffic flow through container-a
From https://docs.docker.com/compose/compose-file/compose-file-v3/#network_mode
Note
This option is ignored when deploying a stack in swarm mode.
network_mode: "host" cannot be mixed with links.
It won't work if your docker-compose "wrapper" uses docker stack deploy under the hood.
You would have to use something like this:
docker network create some-other-network -d bridge --attachable --scope swarm
... where some-other-network is bound to the target container.
And then:
version: "3.9"
services:
my-service:
image: ...
networks:
- some-other-network
...
networks:
some-other-network:
external: true
My local enviroment has connection with a machine that has a BDD. When I ping this machine's IP I have a response.
When I sart my container with docker compose, this container doesn't have connection with this machine. If I enter the container with docker exec -it my-container sh and I ping the DBB machine I have no response.
I have docker installed in Windows 10 y my Docker Compose file looks like this:
version: '3.1'
services:
tomcat:
image: 'tomcat:7.0.91-jre8-alpine'
restart: always
volumes:
- ./warfiles:/home
- ./conf:/usr/local/tomcat/conf/Catalina/localhost
- ./context.xml:/usr/local/tomcat/conf/context.xml
- ./ik-report-config:/ik-report-conf
- ./lib/postgresql-9.3-1102-jdbc4.jar:/usr/local/tomcat/lib/postgresql-9.3-1102-jdbc4.jar
ports:
- 8070:8080
- 8000:8000
environment:
- REPORT_ENGINE_HOME=/ik-report-conf
Thank you very much for your help.
You should probably use network_mode: host.
From the Docker Network Guide:
host: For standalone containers, remove network isolation between the
container and the Docker host, and use the host’s networking directly.
What I had to use was network_mode: bridge
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
I'm using this docker image https://github.com/moodlehq/moodle-docker and it works as advertised. Among other things it exposes web server on localhost:8000 address. What I would like is to bind it to the host's ip instead.
Using raw docker something like that is accomplished with
docker run --network=host [container]
What should be placed in the yml file for docker-compose as documentation is a bit confusing for me.
You can use network_mode in compose files -
network_mode: "host"
Sample compose -
version: '3'
services:
api:
image: 'node:6-alpine'
network_mode: host
environment:
- NODE_ENV=production
command: "tail -f /dev/null"
Ref - https://docs.docker.com/compose/compose-file/#network_mode