Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments for Dockerfiles using Docker compose

I'm trying to use parametrize my dockerfiles on build phase and use arguments in Docker-compose. For example in Docker compose I have defined one service called bpp as following:

bpp:
  build:
    context: .
    dockerfile: Dockerfile.bpp
    args:
      gp : 8080
  image: serv/bpp
  restart: always
  depends_on:
    - data
  links:
    - data

I'm trying to pass argument named gp to Dockerfile.bpp, where I'm using argument when starting a Python application, exposing a port etc. For example in dockerfile.bpp in trying to expose port gp as following:

EXPOSE gp

However, when building docker file by command docker-compose build I get following error: ERROR: Service 'bpp' failed to build: Invalid containerPort: gp

It seems that argument gp is not visible in dockerfile. Any suggestions?

like image 344
lipponen Avatar asked Jun 27 '16 10:06

lipponen


People also ask

Do you need Dockerfiles with docker compose?

Both the Dockerfile and docker-compose are important resources in the development and deployment of cloud-native applications. But knowing the difference between docker-compose and the Dockerfile is important. The Dockerfile is used to build images, while docker-compose helps you run them as containers.

Is it possible to pass environment variables using Dockerfiles?

Passing Environment Variables Into a DockerfileDockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.

How do I pass arg in Dockerfile?

ARG instruction defines a variable that can be passed at build time. Once it is defined in the Dockerfile you can pass with this flag --build-arg while building the image. We can have multiple ARG instruction in the Dockerfile. ARG is the only instruction that can precede the FROM instruction in the Dockerfile.

Where should I put Dockerfiles?

Traditionally, the Dockerfile is called Dockerfile and located in the root of the context. You use the -f flag with docker build to point to a Dockerfile anywhere in your file system. $ docker build -f /path/to/a/Dockerfile .


2 Answers

You need to add ARG gp to your Dockerfile.

...
ARG gp

EXPOSE $gp
...

https://docs.docker.com/engine/reference/builder/#arg

Worth mentioning that this isn't going to expose the port when you're running it via compose though, you would need to add a ports instruction to your docker-compose.yml for that.

like image 113
johnharris85 Avatar answered Oct 12 '22 16:10

johnharris85


First, the syntax is:

build:
  args:
    buildno: 1
    user: someuser

build:
  args:
    - buildno=1
    - user=someuser

So try with gp: 8080, not gp : 8080.

Second, make sure you are using version 2 of the docker-compose.yml format.

Third, try

EXPOSE $gp
like image 24
VonC Avatar answered Oct 12 '22 16:10

VonC