J.J. Beam
J.J. Beam

Reputation: 3059

Is it needed to build containers first before use docker compose?

Docker compose solution is created to up all dependent container at once, I read some articles explained how to rise multiple containers altogether at once:

version: '3'

services:
  postgresql:
    image: postgres:9.6.6
    ports:
      - 9932:5432
    expose:
      - "5432"
    environment:
      - POSTGRES_PASSWORD=pass
    restart: always
    volumes:
      - /data:/var/lib/postgresql/data

  myapp:
    image: myapp
    links:
      - postgresql
    depends_on:
      - "postgresql"
    restart: always
    ports:
      - "5000:5000

1) So do I need to build / run manually myapp and postgresql with docker build and docker run commands? (in case of postgresql possible can docker run - it pulls it from docker hub) and only after can use docker-compose up

2) If I add:

app:
    build: ./app

will it avoid me from 1)? (can use docker-compose up without docker build and docker run preparation steps)

Upvotes: 0

Views: 433

Answers (1)

Nguyen Lam Phuc
Nguyen Lam Phuc

Reputation: 1431

When you run the docker-compose up and:

  • If you provide the tag image and no tag build like your postgresql service in this case, the system will first look for the image name in your local machine (postgres:9.6.6 in this case). If it cannot find, it will try to pull from the Internet (default at Dockerhub)
  • If you provide the tag build, it will try to build your app image first based on the provided context and Dockerfile location and spin up a container based on that.
  • If you provide both build and image tag, it will build and save the image as the image name that you defined.

So to answer your questions:

  1. You can just use docker-compose up without first running docker run or docker pull. However, for a private registry, you will first need to login so that Docker will have permission to pull from.
  2. Yes as explained above.

Upvotes: 2

Related Questions