1
version: '2'

services:
  service1:
    build:
    image: company/service1:v1
    context: .
      - dockerfile: Dockerfile-service1

  service2:
    build: .
    image: company/service2:v1
    links:
      - service1

I have two Dockerfiles, one is called Dockerfile and one is called Dockerfile-service1 both in the same directory (/opt) as the docker-compose.yml.

I need to build both images and start the containers and link them. In order for me to do that now, I move the Dockerfile-service1 into another directory and rename it to Dockerfile and then run the build command like this: docker build -t company/service1:v1 .

Then I go to the /opt directory again and build the service2 image and start both containers: docker build . --no-cache -t company/service2:v1 && docker-compose up -d

There must be a better way so I can run one command that does both, I just don't know how.

How do I solve this problem?

0

2 Answers 2

3

Not really sure what are you trying to do, but you do not need to rename docker file. In docker-compose, you can have it like this:

build: 
  context: ./path_to_dockerfile 
  dockerfile: Dockerfile-service1

and then I run everything inside docker-compose.yml with following command:

docker-compose build
docker-compose up -d
Sign up to request clarification or add additional context in comments.

2 Comments

Prashanth Sams and @golobich, Worked beautifully. Thanks much!
If this is solved, then please accept the answer, so future users will know what help you to fix this issue. Thank you
1

This would do:

version: '2'

services:
  service1:
    container_name: service1
    build:
      context: ./
      dockerfile: ./Dockerfile-service1

  service2:
    container_name: service2
    build:
      context: ./
      dockerfile: ./Dockerfile
    links:
      - service1

or if you have the service-2 image already built, here it is:

version: '2'

services:
  service1:
    container_name: service1
    build:
      context: ./
      dockerfile: ./Dockerfile-service1

  service2:
    container_name: service2
    image: company/service2:v1
    links:
      - service1
docker-compose up -d

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.