How to build specific service using docker-compose? - docker

How do I target my build using docker-compose to one of the services I have in docker-compose.yml?
My docker-compose.yml for example:
version: '3'
services:
admin:
image: 8205037.dkr.ecr.us-east-2.amazonaws.com/admin:latest
build:
context: ../
dockerfile: ./.docker/Dockerfile-admin
www:
image: 233232037.dkr.ecr.us-east-2.amazonaws.com/www:latest
build:
context: ../
dockerfile: ./Dockerfile-www
And how to build only the www without change the file? I want to run something like this:
docker-compose -f .docker/docker-compose.yml build --target www

Just remove the target :)
docker-compose -f .docker/docker-compose.yml build www

docker-compose -f **(path docker-compose file)** build **(container name)**
If it's the first time.
docker-compose -f **(path docker-compose file)** up -d **(container name)**

Related

docker build command doesn't refresh my source files

I made a typo in my source code and I noticed it after i run docker-compose up in my cli. I tried rebuilding the project but didn't change my index.js cached code.
This is my Dockerfile
FROM node:alpine
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
CMD [ "npm","start" ]
docker-compose.yml
version: '3'
services:
redis-server:
image: 'redis'
node-app:
build: .
ports:
- "4001:8081"
I recreated the image
$ sudo docker build -t phillalexakis/visits:latest .
and run
docker-composer up
It didn't change the source code at all, what have I completely missed? (I'm new with docker)
Docker-compose is looking for an image named [folder_name]_node-app, but the image you've built is tagged phillalexakis/visits.
Change your node-app service in docker-compose.yml file :
node-app:
build: .
image: node-app
And use docker-compose to build the images:
docker-compose build or docker-compose up --build

Docker compose on ecr: ERROR: No such service: --build-arg

I try to pass arg into the docker build process in this command:
docker-compose -f .docker/docker-compose.ecr.yml build my-app --build-arg BUILD_VERSION=5.0.0
But I got error:
ERROR: No such service: --build-arg
According to the docs docker-compose have build-arg parameter.
The yml file:
version: '3'
services:
my-app:
image: 3144.dkr.ecr.us-east-2.amazonaws.com/my-app:latest
build:
context: ../
dockerfile: ./.docker/Dockerfile
What can be the problem?
The services should be last in the command line.
$ docker-compose build --help
Usage: build [options] [--build-arg key=val...] [SERVICE...]
So:
$ docker-compose -f .docker/docker-compose.ecr.yml build --build-arg BUILD_VERSION=5.0.0 my-app
I had a similar issue but it was Visual Studio 2019 that was trying to publish and putting the build my-app before the 'build-arg' parameter.
All I had to do is to update VS 2019 to the latest version 16.11.0 that solved this error during the publish from the IDE.

Docker Compose: How to specify path to docker file while building as if in different directory?

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

If docker-compose.yml and Dockerfile both specify env vars which has precedence?

If I have Dockerfile:
FROM ubuntu:18.04
ENV NAME=Dockerfile-wins
CMD echo $NAME
and a docker-compose.yml with:
version: '3'
services:
myservice:
build: .
container_name: myservice
environment:
- NAME=docker-compose-wins
when I run docker-compose up myservice which one will win out and why?
docker-compose wins out as it overrides whats in the Dockerfile. What is specified in the Dockerfile is a default. and can be overridden by using the docker command which is what docker-compose does.
This is documented in the docs here
under the covers essentially docker-compose is running something like
docker run -e VARIABLE=VALUE

docker-compose args from shell

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

Resources