[英]How to run Docker container in it's own network
Today I switched from "Docker Toolbox" to "Docker for Mac", because Docker now has finally write-access to my User
directory (which doesn't worked with "Docker Toolbox") - Yay! 今天,我从“ Docker Toolbox”切换到“ Mac Docker”,因为Docker现在终于可以访问我的
User
目录(不适用于“ Docker Toolbox”)-是的!
But this change also includes that all containers now running under my localhost and not under Docker's IP as before (eg 192.168.99.100
). 但是此更改还包括所有容器现在都在我的本地主机下运行,而不是像以前一样在Docker的IP下运行(例如
192.168.99.100
)。
Since my localhost
listens to various ports by default (80, 443, ...) and I don't want to always add new created ports, that doesn't conflict with the standard one's, to my local dev domains (eg example.dev:8443
), I wonder how to run my containers as before. 由于默认情况下,我的
localhost
监听各种端口(80、443,...),并且我不想始终向本地开发域添加新创建的端口,因此与标准端口不会冲突(例如, example.dev:8443
),我想知道如何像以前一样运行我的容器。
I read about network
configs and tried a lot of things (creating a new host network, exposing ports with an IP in front of it, ...), but didn't got it working. 我阅读了有关
network
配置的信息,并尝试了很多方法(创建一个新的主机网络,使用IP暴露端口,...),但没有使它工作。
What kind of config do I need to run my app container with the IP 192.168.99.100
? 我需要哪种配置才能使用IP
192.168.99.100
运行我的应用程序容器? Thats my docker-compose.yml
so far. 到目前为止,这就是我
docker-compose.yml
。
version: '2'
services:
app:
build:
context: .
dockerfile: Dockerfile
depends_on:
- mysql
- redis
- memcached
ports:
- 80:80
- 443:443
- 22:22
- 3000:3000
- 3001:3001
volumes:
- ./app/:/app/
- /tmp/debug/:/tmp/debug/
- ./:/docker/
volumes_from:
- storage
# cap and privileged needed for slowlog
cap_add:
- SYS_PTRACE
privileged: true
env_file:
- etc/environment.yml
- etc/environment.development.yml
mysql:
build:
context: docker/mysql/
dockerfile: MariaDB-10
ports:
- 3306:3306
volumes_from:
- storage
volumes:
- ./data/mysql:/var/lib/mysql
- /tmp/debug/:/tmp/debug/
env_file:
- etc/environment.yml
- etc/environment.development.yml
redis:
build: docker/redis/
volumes_from:
- storage
env_file:
- etc/environment.yml
- etc/environment.development.yml
memcached:
build: docker/memcached/
volumes_from:
- storage
env_file:
- etc/environment.yml
- etc/environment.development.yml
storage:
build: docker/storage/
volumes:
- /storage
You need to declare "networks:" for each of your services: 您需要为每个服务声明“ networks:”:
eg 例如
version: '2'
services:
app:
image: xxxx:xxx
ports:
- "80:80"
networks:
- my-network
mysql:
image: xxxx:xxx
networks:
- my-network
networks:
my-network:
driver: bridge
Then from side your app configuration, you can use "mysql" as the hostname of database server. 然后从您的应用程序配置的侧面,您可以使用“ mysql”作为数据库服务器的主机名。
You can define a network in your compose file, then add any services to the network. 您可以在撰写文件中定义网络,然后将任何服务添加到网络。
https://docs.docker.com/compose/networking/ https://docs.docker.com/compose/networking/
But I would suggest you just use different ports now that you are running natively. 但是我建议您既然本机运行,就只使用不同的端口。 Ie 8080:80
即8080:80
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.