用docker-compose运行docker镜像
我有我的简单的应用程序在C#中,连接到PostgreSQL。 我想创build这个应用程序的形象,只是与docker运行。
我使用时一切正常:
$ docker build $ docker run postgres $ docker run my_app
此外,当我使用从应用程序目录撰写时,一切都可以:
$ docker-compose build $ docker-compose up
但是有没有机会使用docker-compose为我以前build造的图像?
我想发布这个图像到我的回购和我的团队的其他人只是下载并运行这个图像(应用程序+数据库)。
当我编写构build和下一步撰写运行my_app我连接到数据库时有exception:
dbug: Npgsql.NpgsqlConnection[3] Opening connection to database 'POSTGRES_USER' on server 'tcp://postgres:5432'. Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.AggregateException: One or more errors occurred. (No such device or address) ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: No such device or address
我目前的docker-compose.yml文件:
version: '2' services: web: container_name: 'postgrescoreapp' image: 'postgrescoreapp' build: context: . dockerfile: Dockerfile volumes: - .:/var/www/postgrescoreapp ports: - "5001:5001" depends_on: - "postgres" networks: - postgrescoreapp-network postgres: container_name: 'postgres' image: postgres environment: POSTGRES_PASSWORD: password networks: - postgrescoreapp-network networks: postgrescoreapp-network: driver: bridge
是的,您可以通过docker compose
使用以前创build的图像repository
。
例:
version: '2' services: app: image: farhad/my_app ports: - "80:80" networks: - testnetwork postgres: image: postgres:latest networks: - testnetwork networks: testnetwork: external: true
示例说明:
我从我的资源库创build一个名为app
的容器,另一个名为postgres
容器通过库postgres
映像。
请注意,您需要先创build一个自定义图像到存储库。
我在这里使用user-defined
networking,您需要在testnetwork
之前创buildtestnetwork
。
您应该使用以下名称构build映像:(registryName:RegistryPort)/ imagename:version
$ docker build -t myRegistry.example.com:5000/myApp:latest . $ docker build -t myRegistry.example.com:5000/myDb:latest .
现在将这些行添加到docker-compose文件中:
Myapp: image: myRegistry.example.com:5000/myApp:latest MyDb: image: myRegistry.example.com:5000/myDb:latest
然后推它:
$ docker push myRegistry.example.com:5000/myApp:latest $ docker push myRegistry.example.com:5000/myDb:latest
你的伴侣现在应该能够现在拉
$ docker pull myRegistry.example.com:5000/myApp:latest $ docker pull myRegistry.example.com:5000/myDb:latest