10

I have my simple app in C# that connect with postgreSQL. I would like to create image with this app and just run with 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:

$ docker-compose build
$ docker-compose up

But is there any chance for use docker-compose for image that I built previous?

I would like to publish this image to my repo and somebody else from my team just download and run this image (app + database).

When I do compose-build and next compose run my_app I have exception during connecting to database:

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:

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
GrzesiekO
  • 1,179
  • 4
  • 21
  • 34
  • 2
    You should be able to create a docker compose config that uses your own images, sure. Either use the `build` or `image` attributes to build your images or use a predefined one (that you host in your repo): https://docs.docker.com/compose/compose-file/ – nwinkler Jan 05 '17 at 09:16
  • I have already use it in my docker-compose.yml file. – GrzesiekO Jan 05 '17 at 11:18
  • Then what is your question? – nwinkler Jan 05 '17 at 11:19
  • What I am doing wrong that my app can not connect with postres, when I do docker-compose build and docker run my_app – GrzesiekO Jan 05 '17 at 11:29
  • Then I suggest that you add that to your question, along with your Docker Compose file, and the command that you use to run... At the moment, it doesn't include any of that. – nwinkler Jan 05 '17 at 11:31
  • I have added some details. – GrzesiekO Jan 05 '17 at 11:37
  • Could you share postgresql's logs too please? – wilsonW Jan 06 '17 at 12:41
  • I have the same idea like you, but it seems we can't do that. What docker-compose did is to run multipile images together. Our idea is to have just one image. – hiveer Feb 27 '21 at 15:28

2 Answers2

3

you should build the image with this name: (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 :

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
wilsonW
  • 364
  • 3
  • 7
2

Yes, You can use previously created image from repository via docker compose.

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.

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.

Farhad Farahi
  • 35,528
  • 7
  • 73
  • 70
  • I have this docker-compose.yml file with similar structure. My app run, but there is an exception during connection to postres. My connection string is: "User ID=postgres;Password=password;Server=postgres;Port=5432;Database=POSTGRES_USER;Integrated Security=true;Pooling=true;" It works with two cases as I mention above. – GrzesiekO Jan 05 '17 at 10:06
  • from docker-compose, or docker build? docker-compose works fine but only when I run this command from application directory – GrzesiekO Jan 05 '17 at 10:37