I want to remove a container defined in docker-compose.yml file when we run in composition/override with another file docker-compose.prod.yml, by example:
# docker-compose.yml
version: 2
services:
www:
image: php56
db_for_development:
image: mariadb
override with:
# docker-compose.prod.yml
version: 2
services:
www:
image: php70
db_for_development:
[control: override-and-remove] # hypothesis
Then, when running:
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
docker-compose -f docker-compose.yml -f docker-compose.prod.yml ps
Actually, i have www and db_for_development together.
I want only www container, not others.
You may have to switch to version: 3 to do this, I believe on version: 2 you can use the "scale" parameter but I'm not 100% sure.
Anyways, you can override the "replicas" parameter like this:
# docker-compose.prod.yml
version: "3"
services:
db_for_development:
deploy:
replicas: 0
You're going about this backwards
docker-compose.yml -> specify all services that will be always running
docker-compose.override.yml -> gets picked up automatically, usually used for development
docker-compose.*.yml -> special cases
So, in your case:
You don't remove a container defined in docker-compose.yml, you add it by override with another file or customize it with docker-compose.prod.yml, by example:
docker-compose.yml -> this is the base
version: 2
services:
www:
image: php56
docker-compose.override.yml -> this is dev
version: 2
services:
db_for_development:
image: mariadb
docker-compose.production.yml -> this is prod
version: 2
services:
www:
environment:
- APP_ENV=production
env_file:
- /home/ubuntu/production-app
docker-compose.admin.yml -> this is for the dba
version: 2
services:
adminer:
image: adminer
restart: always
ports:
- 8080:8080
instructions:
For development, docker-compose.yml and docker-compose.override.yml will be used just by running
$ docker-compose up
Production, manually specify both files
$ docker-compose -f docker-compose.yml -f docker-compose.production.yml up --remove-orphans
If you want to bring also bring adminer up (not recommended for production, but sometimes needed anyways)
$ docker-compose -f docker-compose.yml -f docker-compose.production.yml -f docker-compose.admin.yml up
Lastly, when you're done with adminer, just run the production command again this will leave adminer running as an orphan, and you do not want that. That's why the flag --remove-orphans is for
Let's say you want to remove (disable) a service that's defined in your compose file
Contents of docker-compose.yml
version: "3.4"
services:
app:
restart: always
image: "rasa/rasa-x-demo:${RASA_X_DEMO_VERSION}"
expose:
- "5055"
depends_on:
- rasa-production
Contents of docker-compose.override.yml
version: "3.4"
services:
app:
image: alpine:latest
command: "true"
entrypoint: "true"
Done. Now your container will still launch but it's disabled using an empty image
This is not possible. Your only real option would be to specify the services (selectively) when running docker-compose up.
Related
I have a docker compose file that looks like this
version: '3'
services:
mongodb:
container_name: mongodb
restart: always
image: mongo:4.2.22
networks:
- mynetwork
ports:
- 27017:27017
webapp:
container_name: webapp
image: webapp:1.0
build:
context: .
dockerfile: WebApp
I can run the command docker-compose up -d --build and read/write from the Mongo db. But if I change the DockerFile to be
version: '3'
services:
mongodb:
container_name: mongodb
restart: always
image: mongo:4.2.22
networks:
- mynetwork
ports:
- 27017:27017
webapp:
container_name: webapp
image: webapp:2.0
build:
context: .
dockerfile: WebApp
and run the aforementioned command, the build completes successfully but I do get the error
ERROR: for webapp
Cannot start service webapp: container <container-id> encountered an error during hcsshim::System::CreateProcess:
failure in a Windows system call: The system cannot find the file specified. (0x2)
The code between v1 and v2 is very similar except in v2 I create a couple of new databases. If I switch my docker-compose file to use v1 the containers come up just fine. Curious as to what is causing the error and was wondering if anyone had some suggestions.
docker logs <containerid> does not show anything either.
Thank you
I can't seem to get docker/Maria to use my named docker volume. The host docker volumes directory is empty. But, there is a new container id right next to my named volume that looks like it has all of the MariaDB parts in it. The question is why?
My docker compose file:
version: "3.7"
#
# [Volumes]
#
volumes:
data-mysql:
#
# [Services]
#
services:
mariadb:
volumes:
- data-mysql:/var/lib/mysql
image: linuxserver/mariadb
container_name: mariadb
environment:
- PUID=1000
- GUID=1000
- MYSQL_ROOT_PASSWORD=<snipped>
- TZ=Etc/UTC
ports:
- 3306:3306
restart: unless-stopped
I've tried moving the volume part before and after the services line with no difference. When I do a docker-compose up, it does say it's creating the volume: mariadb_data-mysql, but when I shut down docker, there is nothing in the folder.
Thanks for any insight!
Nick
The data folder for MARIADB image you are using (linuxserver/mariadb) is /config/databases/ and not /var/lib/mysql. Replace this in your docker-compose.yml and it will work.
Also, the order in your docker-compose.yml does not matter: docker-compose will compile it and order everything alphabetically anyway before processing.
So i currently can use "docker-compose up test" which only runs my database and my testing scripts. I want to be able to us say docker-compose up app" or something like that that runs everything besides testing. That way Im not running unnecessary containers. Im not sure if theres a way but thats what I was wondering. If possible Id appreciate some links to some that already do that and I can figure out the rest. Basically can I only run certain containers with a single command without running the others.
Yaml
version: '3'
services:
webapp:
build: ./literate-app
command: nodemon -e vue,js,css start.js
depends_on:
- postgres
links:
- postgres
environment:
- DB_HOST=postgres
ports:
- "3000:3000"
networks:
- literate-net
server:
build: ./readability-server
command: nodemon -L --inspect=0.0.0.0:5555 server.js
networks:
- literate-net
redis_db:
image: redis:alpine
networks:
- literate-net
postgres:
restart: 'always'
#image: 'bitnami/postgresql:latest'
volumes:
- /bitnami
ports:
- "5432:5432"
networks:
- literate-net
environment:
- "FILLA_DB_USER=my_user"
- "FILLA_DB_PASSWORD=password123"
- "FILLA_DB_DATABASE=my_database"
- "POSTGRES_PASSWORD=password123"
build: './database-creation'
test:
image: node:latest
build: ./test
working_dir: /literate-app/test
volumes:
- .:/literate-app
command:
npm run mocha
networks:
- literate-net
depends_on:
- postgres
environment:
- DB_HOST=postgres
networks:
literate-net:
driver: bridge
I can run docker-compose up test
Which only runs the postgres. Though I'd like to be able to just run my app without having to run my testing container.
Edit
Thanks to #ideam for the link
I was able to create an additional yaml file for just testing.
For those that dont want to look it up simply create a new yaml file like so
docker-compose.dev.yml
replace dev with whatever you like besides override which causes docker-compose up to automatically run that unless otherwise specified
To run the new file simply call
docker-compose -f docker-compose.dev.yml up
The -f is a flag for selecting a certain file to run. You can run multiple files to have different enviornments set-up
Appreciate the help
docker-compose up <service_name> will start only the service you have specified and its dependencies. (those specified in the dependends_on option.)
you may also define multiple services in the docker-compose up command:
docker-compose up <service_name> <service_name>
note - what does it mean "start the service and its dependecies"?
usually your production services (containers) are attached to each other via the dependes_on chain, therefore you can start only the last containers of the chain. for example, take the following compose file:
version: '3.7'
services:
frontend:
image: efrat19/vuejs
ports:
- "80:8080"
depends_on:
- backend
backend:
image: nginx:alpine
depends_on:
- fpm
fpm:
image: php:7.2
testing:
image: hze∂ƒxhbd
depends_on:
- frontend
all the services are chained in the depends_on option, while the testing container is down bellow the frontend. so when you hit docker-compose up frontend docker will run the fpm first, then the backend, then the frontend, and it will ignore the testing container, which is not required for running the frontend.
Starting with docker-compose 1.28.0 the new service profiles are just made for that! With profiles you can mark services to be only started in specific profiles:
services:
webapp:
# ...
server:
# ...
redis_db:
# ...
postgres:
# ...
test:
profiles: ["test"]
# ...
docker-compose up # start only your app services
docker-compose --profile test up # start app and test services
docker-compose run test # run test service
Maybe you want to share your docker-compose.yml for a better answer than this.
For reusing docker-compose configurations have a look at https://docs.docker.com/compose/extends/#example-use-case which explains the combination of multiple configuration files for reuse of configs for different use cases (test, production, etc.)
I'm new to docker.
Here is my simple docker-compose file.
version: '3.4'
services:
web:
image: 'myimage:latest'
build: .
ports:
- "5265:5265"
environment:
- NODE_ENV=production
To run this, I usually use docker-compose up command.
Can I change the NODE_ENV variable to anything while running docker-compose up?
For example:
docker-compose up -x NODE_ENV=staging
Use docker-compose run, you can manage services but not the complete stack. Useful for one-off commands.
$ docker-compose run -d -e NODE_ENV=staging web
Ref - https://docs.docker.com/compose/reference/run/
OR
Best way i could see as if now is to use shell & export the environment variable before doing a docker-compose up as below -
$ export NODE_ENV=staging && docker-compose up -d
Where your docker-compose will look something as below -
version: '3.4'
services:
web:
image: 'myimage:latest'
build: .
ports:
- "5265:5265"
environment:
- NODE_ENV=${NODE_ENV}
I am trying to have my own network name for my docker-compose files (server.yml and test.yml), as test.yml gets only started from time to time, but needs access to some services in the server.yml. I can make it work with docker-compose -p nameofproject up, but not with COMPOSE_PROJECT_NAME.
server.yml
version: '2'
networks:
mynetwork:
driver: bridge
services:
app1:
networks:
- mynetwork
environment:
POSTGRES_PASSWORD: somepassword
COMPOSE_PROJECT_NAME: serverstack
app2:
networks:
- mynetwork
environment:
COMPOSE_PROJECT_NAME: serverstack
depends_on:
- app1
My expectation is that when the container is starting I should see
Creating serverstackmynetwork_app_1
Creating serverstackmynetwork_app_2
the network should be named (docker network ls)
serverstack_mynetwork
just like when I do the following, which actually works
docker-compose -p serverstack up
And then I can connect just by using docker-compose up with the second file (which works just fine when using the -p option on the server.yml)
testing.yml
version: '2'
networks:
testapp_network:
external:
name: serverstack_mynetwork
services:
testapp:
networks:
- testapp_network
But using it without -p serverstack on the server.yml I see directories as names
Creating directoryofapp1_app1_1
Creating directoryofapp2_app2_1
so COMPOSE_PROJECT_NAME is being omitted and I also cannot connect to the server service though serverstack_mynetwork
I did add the COMPOSE_PROJECT_NAME: serverstack after building the image, but I would expect it should work anyhow. What am I missing?
I solved this by creating the ".env" file containing
COMPOSE_PROJECT_NAME=myprojectname