67.5k views
5 votes
How to use local image in docker compose

User Jigme
by
7.3k points

1 Answer

4 votes

Final answer:

To use a local image in Docker Compose, create a Dockerfile, build the image locally, and then reference the build context and dockerfile in your docker-compose.yml file.

Step-by-step explanation:

To use a local image in Docker Compose, you need to specify the path to the Dockerfile of your local image within the docker-compose.yml file. Here's a step-by-step explanation:

  1. Create a Dockerfile for your application if you haven't already.
  2. Build your Docker image locally using the docker build command.
  3. In your docker-compose.yml file, under the service you wish to use the local image, specify the context and dockerfile keys.

Here is an example snippet of a docker-compose.yml file using a local image:

version: '3'
services:
myservice:
build:
context: ./directory_of_dockerfile
dockerfile: Dockerfile

This configuration tells Docker Compose to build the image from the Dockerfile located in ./directory_of_dockerfile. Make sure that your build context and Dockerfile are correctly referenced relative to the location of your docker-compose.yml file.

User Ben Grossmann
by
8.1k points