简体   繁体   中英

Reference Dockerfile from docker-compose.yml?

I have a web service that uses a MySQL as its backing store. I want to Dockerize this service as well as its MySQL DB. For the service I have the following Dockerfile:

FROM openjdk:8-jdk-alpine as cce

COPY build/libs/my-service.jar my-service.jar

EXPOSE 9200

ENTRYPOINT [ \
    "java", \
    "-Ddb.hostAndPort=my-service-db:3306", \
    "-Ddb.name=my_service_db_local", \
    "-Ddb.username=my-service-user", \
    "-Ddb.password=abc123", \
    "-jar", \
    "my-service.jar" \
]

If I understand the Docker ecosystem correctly, it sounds like I can write a Docker Compose file to spin up the MySQL container instance as well as the web service container instance. So I have a docker-compose.yml file started that looks like so:

version: "3.7"
services:
  my-service-db:
    image: mysql:8
    container_name: my-service-db
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: r00tdud3
      MYSQL_DATABASE: my_service_db_local
      MYSQL_USER: my-service-user
      MYSQL_PASSWORD: abc123
    volumes:
      - ./my-service-db-data:/var/lib/mysql
  my-service:
    ??? how to specify local Dockerfile here ???
    depends_on:
      - my-service-db

From my own tinkering I'm confident the MySQL container is configured the way I want it and correctly. Now I'm just trying to figure out how to tell docker-compose.yml file (which will live in the root directory of my project, right alongside the Dockerfile) to use the service's Dockerfile, and that it depends on the MySQL container first being started/running. I think I'm close but I'm having a hard time crossing the finish line.

Can anyone help me configure docker-compose.yml to create a service called my-service that uses the local Dockerfile for its config?

You can create an image from the dockerfile:

docker build - < Dockerfile

Then you should tag you image with a proper name.

After creating the image reference it in the docker-compose.yml file:

my-service: 
   image: ${image_name}

Another option is to simply write:

my-service: 
   build: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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