Unique directory name of mount volume per Docker container - docker

Current docker-compose.yml:
version: "3.5"
services:
dev_service:
image: my-image
build:
context: .
target: my-image
extra_hosts:
- "host.docker.internal:host-gateway"
ports:
- "6080:80"
- "6022:22"
volumes:
- './workspace/logs:/var/log/peimp'
This is fine for a single container instance, but not many.
Logs will be overwritten.
Is there any way to automatically create unique directories that will be shared on the host system?
e.g.
./workspace/logs_1,
./workspace/logs_2

You could use an ENV variable like this:
version: "3.5"
services:
dev_service:
image: my-image
build:
context: .
target: my-image
extra_hosts:
- "host.docker.internal:host-gateway"
ports:
- "6080:80"
- "6022:22"
volumes:
- './workspace/logs${LOG_NUM}:/var/log/peimp'
And then run the docker compose like this:
export LOG_NUM=1 && docker-compose up

Related

Docker Compose ports are being ignored on up

This is my Docker Compose file containing 4 api containers:
version: '3.4'
services:
api1.api:
image: ${DOCKER_REGISTRY-}api1
build:
context: .
dockerfile: Api1.Api/Dockerfile
networks:
- dev-network
ports:
- "62000:443"
- "62001:80"
api2.api:
image: ${DOCKER_REGISTRY-}api2
build:
context: .
dockerfile: Api2.Api/Dockerfile
networks:
- dev-network
ports:
- "62002:443"
- "62003:80"
api3.api:
image: ${DOCKER_REGISTRY-}api3
build:
context: .
dockerfile: Api3.Api/Dockerfile
networks:
- dev-network
ports:
- "62004:443"
- "62005:80"
api4.api:
image: ${DOCKER_REGISTRY-}api4
build:
context: .
dockerfile: Api4.Api/Dockerfile
networks:
- dev-network
ports:
- "62006:443"
- "62007:80"
networks:
dev-network:
name: dev-network
I have set the ports but when I run docker-compose up -d, the results are that either 1 or 2 of the apis will have the specified ports only. The rest will be randomly generated or even blank.
I have to manually restart that container before it generates a random port.
Am I missing a command or step? How can I get my docker-compose to follow the ports that have been set?

How to swap env file for another docker service

I have a docker-compose.yml
services:
nextjs:
container_name: next_app
build:
context: ./
restart: on-failure
command: npm run dev
volumes:
- ./:/app
- /app/node_modules
- /app/.next
ports:
- "3000:3000"
cypress:
image: "cypress/included:9.4.1"
depends_on:
- next_app
environment:
- CYPRESS_baseUrl=http://nextjs:3000
working_dir: /e2e
volumes:
- ./e2e:/e2e
I want to change env_file for next_app from cypress service. I found solution like this
cypress:
image: "cypress/included:9.4.1"
depends_on:
- next_app
environment:
- CYPRESS_baseUrl=http://nextjs:3000
working_dir: /e2e
volumes:
- ./e2e:/e2e
next_app:
env_file: .env.test
But this solution does not work. Is it even possible ?
Try something like cp .env #docker/.env
No. In Compose (or Docker, or even more generally in Linux/Unix) there is no way for one container (process) to specify environment variables for another.
You can think of a docker-compose.yml file as a set of instructions only for running containers. If you need a specific set of containers for a specific context – you don't normally need to run Cypress in production, but this is an integration-test setup – it's fine to write a separate Compose file just for that setup.
# docker-compose.cypress.yml
# Used only for integration testing
version: '3.8'
services:
nextjs:
build: .
restart: on-failure
ports:
- "3000:3000"
env_file: .env.test # <-- specific to this test-oriented Compose file
cypress:
build: ./e2e
depends_on:
- nextjs
environment:
- CYPRESS_baseUrl=http://nextjs:3000
docker-compose -f docker-compose.cypress.yml up --build
This can also be a case where using multiple Compose files together can be a reasonable option. You can define a "standard" Compose setup that only defines the main service, and then an e2e-test Compose file that adds the Cypress container and the environment settings.
# docker-compose.yml
version: '3.8'
services:
nextjs:
image: registry.example.com/nextjs:${NEXTJS_TAG:-latest}
restart: on-failure
ports:
- '3000:3000'
# docker-compose.e2e.yaml
version: '3.8'
services:
nextjs:
# These add to the definitions in the base `docker-compose.yml`
build: .
env_file: .env.test
cypress:
# This is a brand new container for this specific setup
depends_on: [nextjs]
et: cetera # copy from question or previous Compose setup
docker-compose \
-f docker-compose.yml \
-f docker-compose.e2e.yml \
up --build

How to use single docker compose to build containers in different directories

I want to spin up two containers when I do docker-compose up
I have two folders API and front. Each has a Dockerfile and docker-compose file. currently, I have to do docker-compose up for each app.
like below
- api
- docker-compose.yml
- Dockerfile
- front
- docker-compose.yml
- Dockerfile
I want to have one docker-compose.yml to manage two containers like below.
- docker-compose.yml
- api
- Dockerfile
- front
- Dockerfile
api docker-compose
version: '3'
services:
api:
build: .
command: pipenv run start
image : data-tracker-backend
volumes:
- .:/api/
ports:
- "8000:8000"
front docker-compose
version: '3'
services:
web:
build: .
command: npm start
image : data-tracker-front
volumes:
- .:/front/
ports:
- "5000:5000"
I want to have something like
version: '3'
services:
api:
build: .
command: pipenv run start
image : data-tracker-backend
volumes:
- .:/api/
ports:
- "8000:8000"
front:
build: .
command: npm start
image : data-tracker-front
volumes:
- .:/front/
ports:
- "5000:5000"
help to access the command from the different working directories.
You can change the build directory and the volumes one.
version: '3'
services:
api:
build: ./api
volumes:
- ./api:/api
# the rest of your commands...
front:
build: ./front
volumes:
- ./front:/front
# the rest of your commands...
My suggestion though is to read the guidelines. It's plenty of information and tutorials there.
You can try to use build context and dockerfile as shown below
- docker-compose.yml
- api
- Dockerfile
- front
- Dockerfile
version: '3'
services:
api:
build:
context: api
dockerfile: Dockerfile
command: pipenv run start
image : data-tracker-backend
volumes:
- .:/api/
ports:
- "8000:8000"
front:
build:
context: front
dockerfile: Dockerfile
command: npm start
image : data-tracker-front
volumes:
- .:/front/
ports:
- "5000:5000"

Volume defined in docker-compose not mounting

I have the following docker-compose.yaml:
web:
build: web
volumes:
- .:/data
And web/Dockerfile is like this:
FROM ubuntu
RUN start_some_service.sh
But that data volume isn't being mounted. docker inspect says so:
"Volumes": null
Any idea what I'm doing wrong here?
Also tried:
volumes:
- ".:/app"
But the problem persists.
docker-compose -f docker-compose.yaml config shows the following:
services:
web:
build:
context: /Users/zoran/code/test/web
network_mode: bridge
volumes:
- /Users/zoran/code/test:/app:rw
version: '2.1'
It is necessary to specify both build and image options for the volumes to be mounted:
web:
build: web
image: my-web-image
volumes:
- .:/data

Docker compose with two files

I have two docker-compose.yml files in separate folders.
I'd like to run the two of them in the same command, in order for the services from both to be able to talk to each other.
However, when I go to the lowest common path ancestor and try to run docker-compose with both files, here is what happens:
$ docker-compose -f ./api-folder/docker-compose.yml -f ./front-folder/docker-compose.yml up -d
ERROR: build path /projects/front-folder/api either does not exist, is not accessible, or is not a valid URL.
$ docker-compose -f ./front-folder/docker-compose.yml -f ./api-folder/docker-compose.yml up -d
ERROR: build path /projects/api-folder/app either does not exist, is not accessible, or is not a valid URL.
Here are the two docker-compose.yml files:
/projects/front-folder/docker-compose.yml
version: '2'
services:
app:
restart: always
build: ./app
environment:
NODE_ENV: 'dev'
ports:
- "4400:4400"
volumes:
- ./app:/usr/src/app
nginx:
restart: always
build: ./nginx
volumes:
- ./logs:/usr/local/var/log/nginx
links:
- app
ports:
- "80:80"
/projects/api-folder/docker-compose.yml
version: '2'
services:
api:
restart: always
build: ./api
expose:
- "4600"
volumes:
- ./api:/usr/src/app
- ./logs:/logs
nginx:
restart: always
build: ./nginx
volumes:
- ./logs:/usr/local/var/log/nginx
links:
- api
ports:
- "81:80"
networks:
- hackerz
And the directory structure:
- /projects
- /front-folder
- /app
Dockerfile
- /nginx
Dockerfile
docker-compose.yml
- /api-folder
- /api
Dockerfile
- /nginx
Dockerfile
docker-compose.yml
I'm guessing the problem is with the build paths, but what I don't understand is:
Why Docker insists on searching build: ./api in /front-folder or the other way around?
How to circumvent this problem and be able to run both files together?
DOCKERFILE
Alternate Dockerfile.
Compose uses an alternate file to build with. A build path must also be specified.
service3:
build:
context: .
dockerfile: Dockerfile-alternate
docker compose build giving custom file
This isn't how compose works (by design). See my comment here: https://github.com/docker/compose/issues/3530#issuecomment-222490501.

Resources