How to pass current folder as the context to docker-compose.yml and then Dockerfile?

I have a central Dockerfile. It's locate in ~/base/Dockerfile. Let's say it only builds a simple debian image.

FROM debian
COPY test.js .

I also have a central docker-compose.yml file that uses this Dockerfile. It is located in ~/base/docker-compose.yml.

version: "3.9"
services: 
  test:
    build: ~/base/Dockerfile
    ports:
      - "5000:5000"

I also have a bash file that calls this docker-compose.yml from another directory. For example:

mkdir temp
cd temp
setup

setup is a bash file that is registered in the /etc/bash.bashrc as a global alias.

It contains these lines:

docker-compose -f ~/base/docker-compose.yml build
docker-compose up -d
docker-compose logs --f test

I can run setup from inside any folder, and it should build a container based on that debian image. And it does.

However, it shows me that the name of the container is base_test_1, which is the default convention of docker.

This shows that it uses ~/base/. as the context.

How can I pass my current directory as the context?

Upvotes: 0

Views: 1549

Answers (1)

abhishek phukan
abhishek phukan

Reputation: 791

Created a docker-compose.yml in the same location . Added a context and the value used is a environment variable.

~/base$ cat docker-compose.yml
version: "2.2"
services:
  test:
    build:
      context: ${contextdir}
      dockerfile: /home/myname/base/Dockerfile
    ports:
      - "5000:5000"

~/base$ cat Dockerfile
FROM python:3.6-alpine

COPY testfile.js .


Before triggering the docker-compose.yml build command, export the current working directory.

~/somehere$ ls
testfile.js


~/somehere$ export contextdir=$(pwd)

~/somehere$ docker-compose -f ~/base/docker-compose.yml build
Building test
Step 1/2 : FROM python:3.6-alpine
 ---> 815c1103df84
Step 2/2 : COPY testfile.js .
 ---> Using cache
 ---> d0cc03f02bdf
Successfully built d0cc03f02bdf
Successfully tagged base_test:latest

my composefile and dockerfile are located in ~/base/ while the testfile.js is located in ~/somehere/ (which i am assuming as the current working directory)

Upvotes: 1

Related Questions