简体   繁体   English

使用docker-compose运行docker image

[英]Run docker image with docker-compose

I have my simple app in C# that connect with postgreSQL. 我在C#中使用我的简单应用程序与postgreSQL连接。 I would like to create image with this app and just run with docker. 我想用这个应用程序创建图像,然后用docker运行。

Everything is ok when I use: 我使用时一切正常:

$ docker build
$ docker run postgres
$ docker run my_app

Additionally, there is everything ok, when I use compose from application directory: 另外,当我从应用程序目录使用compose时,一切都还可以:

$ docker-compose build
$ docker-compose up

But is there any chance for use docker-compose for image that I built previous? 但有没有机会使用docker-compose为我之前建立的图像?

I would like to publish this image to my repo and somebody else from my team just download and run this image (app + database). 我想将此图像发布到我的repo,我的团队中的其他人只需下载并运行此图像(app + database)。

When I do compose-build and next compose run my_app I have exception during connecting to database: 当我进行compose-build和next compose运行my_app时,我在连接数据库时遇到异常:

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

My current docker-compose.yml file: 我当前的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

Yes, You can use previously created image from repository via docker compose . 是的,您可以通过docker composerepository使用以前创建的映像。

Example: 例:

version: '2'
services:
  app:
    image: farhad/my_app
    ports:
      - "80:80"
    networks:
      - testnetwork
  postgres:
    image: postgres:latest
    networks:
      - testnetwork
networks:
  testnetwork:
    external: true

Example Explaination: 示例说明:

I'm creating a container named app from my repository and another container named postgres via library postgres image. 我正在从我的存储库创建一个名为app的容器,并通过库postgres图像创建另一个名为postgres容器。

Please note you need to create an push your custom image to repository first. 请注意,您需要先创建将自定义映像推送到存储库。

I'm using user-defined networks here, You need to create testnetwork before docker-compose up . 我在这里使用user-defined网络,你需要在testnetwork docker-compose up之前创建testnetwork

you should build the image with this name: (registryName:RegistryPort)/imagename:version 您应该使用以下名称构建映像:(registryName:RegistryPort)/ imagename:version

$ docker build -t myRegistry.example.com:5000/myApp:latest .
$ docker build -t myRegistry.example.com:5000/myDb:latest .

Now add these lines to the docker-compose file : 现在将这些行添加到docker-compose文件中:

Myapp:                                                    
  image: myRegistry.example.com:5000/myApp:latest 

MyDb:                        
  image: myRegistry.example.com:5000/myDb:latest

And then push it : 然后推它:

$ docker push myRegistry.example.com:5000/myApp:latest
$ docker push myRegistry.example.com:5000/myDb:latest

Your mate should now be able to pull it now 你的伙伴现在应该能够拉它

$ docker pull myRegistry.example.com:5000/myApp:latest
$ docker pull myRegistry.example.com:5000/myDb:latest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM