The docker build context is defined as either a path to a directory containing a Dockerfile, or a url to a git repository.
With the docker command, it's possible to build an image using
a local context and a local Dockerfile:
docker build -t foobar [-f Dockerfile.local] .
a remote context and a remote Dockerfile:
docker build -t foobar https://github.com/foobar/foobar [-f Dockerfile.remote]
a remote context and a local Dockerfile
docker build -t foobar https://github.com/foobar/foobar -f- < Dockerfile.local
The first two options easily translate to docker-compose:
services:
first:
build:
context: .
dockerfile: Dockerfile.local
second:
build:
context: https://github.com/foobar/foobar
dockerfile: Dockerfile.remote
The official documentation says nothing about injecting a local Dockerfile to a remote context with docker-compose. Is it possible?
Related
I am trying to build an image/container with docker compose. The container builds/runs successfully, but the image REPOSITORY and TAG both appear as <none> in the output for docker images and the container gets an auto-generated name (e.g. eloquent_wiles). I would for it to tag the image/container with the names specified in my config files (in this case I would like them to be named 'myservice' and the image to be tagged 'v2').
I have the following docker-compose.yml:
version: '3'
services:
myservice:
build: .
image: myservice:v2
container_name: myservice
ports:
- "1337:1337"
This is my Dockerfile:
FROM node:10
WORKDIR /usr/src/myservice
COPY . /usr/src/myservice
EXPOSE 1337/tcp
RUN yarn \
&& yarn transpile \
&& node ./build/grpc-server.js
docker -v gives Docker version 18.09.2, build 6247962
docker-compose -v gives docker-compose version 1.22.0, build f46880fe
And I am running docker-compose build. I get the same results using docker-compose version 2.
I don't suppose anyone can spot what I'm doing wrong?
Build a named image: docker build -t <repo>:<tag> . in the directory where the Dockerfile is.
Deploy a named service: docker stack deploy -c <your_yaml_file> <your_stack> --with-registry-auth in the directory where your YAML is.
That was very silly of me. The issue was with the last line of the Dockerfile where you can see I start the server as part of the build process instead of as an entrypoint, blocking the build from reaching the step where it tags the images.
Thanks to #Mihai for pointing out that the <none>:<none> image is an intermediate and not the result of my build.
docker build -t test2 -f tests/low_conviction_integration/Dockerfile .
If I need to copy a file from a directory ABOVE my Dockerfile, I can accomplish it by calling
docker build -t image_name -f path_to_docker_file/Dockerfile .
from that ABOVE directory
How can I accomplish the same behavior with docker-compose? I want docker-compose to exist in same directory as Dockerfile, to build with Dockerfile, but to ultimately call it as if it was in ABOVE directory, so that I can copy the correct file. I tried
docker-compose -f path_to_docker_file_and_compose/docker-compose.yml up
from ABOVE directory, but when building from the Dockefile, it complains the files in ABOVE directory are not there to copy when building (which means it is running the docker build from the same directory as the Dockerfile. I need it to run the build from the ABOVE directory).
This can be done like this, add build in docker-compose.yml and run the build from ABOVE directory:
version: '3.8'
services:
your_service:
build: # "context" and "dockerfile" fields have to be under "build"
context: .
dockerfile: <Below_Directory>/Dockerfile
You might want to use the -p flag
Usage:
docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file (default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name (default: directory name)
You can add your subdirectory in the context. Let's say you have the docker file inside a subdirectory docker, the context should be like the below.
version: '3'
services:
php:
build:
context: ./docker
dockerfile: ./php.dockerfile
I want to build via the docker-compose an image that uses my private key for cloning private git repos.
More or less the compose becomes as follows:
myservice:
build:
context: .
args:
RSA: ~/.ssh/id_rsa
The above does not work, neither the following:
myservice:
build:
context: .
args:
RSA: $(cat ~/.ssh/id_rsa)
The docker build command works just fine however in the form of
docker build --build-args RSA=$(cat ~/.ssh/id_rsa) -t myservice:latest
You can use the same syntax with docker-compose build:
docker-compose build --build-arg RSA="$(cat ~/.ssh/id_rsa)"
Unfortunately, you can't use the build-args option with compose up or start... So you will need to build and then run using the --no-build option
One way to do it that will work when building all the services and also with up is to pass the SSH key data as an environnement variable like this:
env RSA=$(cat ~/.ssh/id_rsa) docker-compose build
And then you set the build args in the compose file like this:
myservice:
build:
context: .
args:
RSA: |-
${RSA}
add ARGS in your Dockerfile like
ARGS RSA
For it to be read upon build
I've got the following dir tree:
project_root
|- build_script.sh
|- dir_1/Dockerfile and docker-compose.yml
|- tarball_dir/
I've got the build_script.sh which calls docker-compose like so:
docker-compose -f ./dir_1/docker-compose.yml build --build-arg BUILD_ID=$BUILD_ID dev
This is the Dockerfile:
FROM elixir:1.5.2
ARG BUILD_ID
ENV APP_HOME /app
RUN mkdir $APP_HOME
# Copy release tarball and unpack
~ ADD ../dir_1/${BUILD_ID}.tar.gz $APP_HOME/
CMD $APP_HOME/bin/my_app foreground
And this is the docker-compose-yml:
version: '2'
services:
common:
build:
context: .
dockerfile: ./Dockerfile
networks:
- default
dev:
extends:
service: common
env_file:
- ./dev.env
environment:
POSTGRES_HOST: "postgres.dev"
MIX_ENV: dev
ports:
- "4000:4000"
depends_on:
- postgres
I want to be able to ADD (so that it copies and unpacks the tarball) into the image. However, after trying endless combinations of Docker context directories and trying to include the tarball into the image, I always get one of two errors:
ERROR: Service 'dev' failed to build: ADD failed: Forbidden path outside the build context: ../tarball_dir/tarball.tar.gz ()
or
ERROR: Service 'dev' failed to build: ADD failed: stat /var/lib/docker/tmp/docker-builder887135091/tarball_dir/tarball.tar.gz: no such file or directory
I would like to keep this directory structure, if possible. I've managed to build and run the container with the following command:
docker build \
--build-arg BUILD_ID=$BUILD_ID \
-t my_app:$BUILD_ID \
-f dir_1/Dockerfile .
docker run \
--network my_app_network \
--env-file ./dir_1/$ENV.env \
my_app:$BUILD_ID
But I can't reproduce this same behaviour using docker-compose no matter what.
I'm on Mac OS Sierra and I'm using Docker Edge Version 17.10.0-ce-mac36 (19824) Channel: edge a7c7e01149
I try to register with gitlab a image:
docker build -t registry.gitlab.com/xxx/xxx compose/base
But I get:
Step 4/7 : COPY ./requirements /requirements lstat requirements: no
such file or directory
However build with compose work:
docker-compose -f dev.yml build python
services:
python:
build:
context: .
dockerfile: ./compose/base/Dockerfile
The file structure:
-Project/requirements
-Project/compose/base
How I replicate what compose do?
Your problem is in docker build context, i suggest you try this:
docker build -t registry.gitlab.com/xxx/xxx -f compose/base/Dockerfile .