How to kill a specific docker container using docker-compose? - docker-compose

How to kill a specific docker container using docker-compose, I have tried the below but its didn't work appreciate your help.
root#docker:/opt/dockercompose# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
792663a9f2de nginx "nginx -g 'daemon of…" About an hour ago Up 6 minutes 0.0.0.0:8005->80/tcp dockercompose_webapp2_1
1f94ff0e70cf nginx "nginx -g 'daemon of…" About an hour ago Up 6 minutes 0.0.0.0:8000->80/tcp dockercompose_webapp1_1
root#docker:/opt/dockercompose# docker-compose kill 792663a9f2de
ERROR: No such service: 792663a9f2de
root#docker:/opt/dockercompose#

All of the docker-compose commands take the service names as specified in the docker-compose.yml file. The docker ps output you show could be created from a docker-compose.yml file like:
version: '3.8'
services:
webapp1:
image: nginx
ports: ['8000:80']
webapp2:
image: nginx
ports: ['8005:80']
If you want to kill off a specific Compose-managed container, you can docker-compose kill webapp2; it will find it in the docker-compose.yml and match it up with some hidden container metadata.
For most practical things, if you're in a Compose-managed environment, you can use exclusively docker-compose commands: docker-compose ps to list the containers, docker-compose logs to see a container's output, and so on. All of these again take the Compose service name, not the Docker container name or ID.

Below command works-
root#docker:/opt/dockercompose# docker-compose -f docker-compose.yaml -p 792663a9f2de kill

Related

Connect Spring application in Docker to Mongo

In order to connect spring boot to mongoDB, i am using docker. And i created docker images for mongo and springboot application.
In the beginning running container is:
C:\Users\ASUS>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6dc2aa34ff8f mongo:latest "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:27017->27017/tcp mongodbproject
C:\Users\ASUS>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springboot-mongodb 1.0 5a1cf26b0e0b 5 minutes ago 550MB
mongo latest d98599fdfd65 2 days ago 696MB
However for making connection for them i used below command.
C:\Users\ASUS>docker run -p 8080:8080 --name springboot-mongodb --link mongodbproject:mongo -d springboot-mongodb:1.0
36120b50f09ae07c7c88ca10b1f478d726a1a5c318ee0c9d0f0fc3fb9eff5750
After that i can not see springboot-mongodb in running containers:
C:\Users\ASUS>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6dc2aa34ff8f mongo:latest "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:27017->27017/tcp mongodbproject
After that when i check Docker application it says status for springboot-mongodb as exited(1). when i was trying to run it again, it stops again.
Your Spring application is dying. Don't use -d, then look at the logs, and fix the error that is shown. Or use docker ps -a, then get the ID of the killed container, and run docker logs <id>
Ideally, you'd use Docker Compose, anyway. The --link option is deprecated.
x-mongo: &mongo-opts
MONGODB_USERNAME: mongoUser
MONGODB_PASSWORD: mongoPass
MONGODB_DATABASE: example
version: '2'
services:
web:
image: springboot-mongodb:1.0
ports:
- "8080:8080"
environment:
<<: *mongo-opts
MONGODB_PROTOCOL: mongodb # Example variables. Added in case you wanted to change to mongo+srv://
MONGODB_HOST: mongodb:27017
depends_on:
- mongodb
mongodb:
image: mongo:latest
ports:
- "27017:27017"
environment:
<<: *mongo-opts
MONGODB_ROOT_PASSWORD: l0c#l

volumes_from tried to find a service despite container: is specified

I am running Win10, WSL and Docker Desktop. I have the following test YML which errors out:
version: '2.3'
services:
cli:
image: smartsheet-www
volumes_from:
- container:amazeeio-ssh-agent
➜ ~ ✗ docker-compose -f test.yml up
no such service: amazeeio-ssh-agent
Why does it try to find a service when I specified container: ?
The container exists, runs and has a volume.
docker inspect -f "{{ .Config.Volumes }}" amazeeio-ssh-agent
map[/tmp/amazeeio_ssh-agent:{}]
docker exec -it amazeeio-ssh-agent /bin/sh -c 'ls -l /tmp/amazeeio_ssh-agent/'
total 0
srw------- 1 drupal drupal 0 Apr 1 03:54 socket
Removing the volumes_from and the following line starts the cli service just fine.
After a bit of searching, I finally found https://github.com/docker/compose/issues/8874 and https://github.com/pygmystack/pygmy-legacy/issues/60#issue-1037009622 this fixes it.
uncheck this

docker-compose external_link mongo network not reachable

I am having a strange situation where I can not connect to my running mongo DB in my docker compose. My compose file:
version: '3'
services:
app:
image: myimage:latest
ports:
- "8080:8080"
external_links:
- myname:mongo
environment:
- MONGO_URL=mongodb://myname:27017/test
I have found a few infos on that that all did not solve my issue. I.e. I tried:
1)
Create a custom network:
docker network create mongonet
Then start mongo with the --network mongonet flag and add to the compose:
networks:
default:
external:
name: mongonet
Got nothing there either.
I looked into my /etc/hosts file on my compose, and it did not list any DNS entry.
If i do a docker inspect and grab the mongo IP and add it to my compose, that is fine and works like a charm.
I start mongo like this:
docker run -d -p 27017:27017 -v ~/mongo_data:/data/db mongo
I am really rather confused as I believed this to be a out-of-the-box kind of thing. Strangely I can't make it work. I have found examples on internal links (vs external_link) but that does not work for me as I have many services that I would like to run like this and not all of them should run at the same time.
I start my docker compose as this:
docker-compose up --force-recreate
My versions are:
docker-compose version 1.17.1, build 6d101fb
Docker version 17.05.0-ce, build 89658be
My question: How do I successfully link a running mongo container as an external link into my application containers such that they can connect to them?
My docker PS:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5cf6e08d6fde mongo "docker-entrypoint..." About an hour ago Up About an hour 0.0.0.0:27017->27017/tcp gallant_feynman
Links are deprecated, use networks instead.
Notes:
If you’re using the version 2 or above file format, the
externally-created containers must be connected to at least one of the
same networks as the service which is linking to them. Links are a
legacy option. We recommend using networks instead.
The network way should work. I think you are missing some pieces. Make sure to give the mongo container a name, and make sure to attach the app container to the network in the compose file:
docker network create mongonet
docker run -d -p 27017:27017 --network mongonet --name mongo -v ~/mongo_data:/data/db mongo
version: '3'
services:
app:
image: myimage:latest
ports:
- "8080:8080"
environment:
- MONGO_URL=mongodb://mongo:27017/test
networks:
- mongonet
networks:
default:
external:
name: mongonet

docker-compose exit depends_on service after tests

How do I get a service container to exit once the dependent container has finished?
I have test suite running in the app_unittestbot container that depends_on a postgresql db server (postgres:9.5-alpine) running in separate container. Once the test suite exits, I want to check the return code of the test suite and halt the database container. With the docker-compose.yml below, the db service container never halts.
docker-compose.yml
version: '2.1'
services:
app_postgresql95:
build: ./postgresql95/
ports:
- 54321:5432
app_unittestbot:
command: /root/wait-for-it.sh app_postgresql95:5432 --timeout=60 -- nose2 tests
build: ./unittestbot/
links:
- app_postgresql95
volumes:
- /app/src:/src
depends_on:
- 'app_postgresql95'
You can run docker-compose up --abort-on-container-exit to have compose stop all the containers if any one of them exits. That will likely solve your use case.
For something a little more resilient, I'd probably split this into two compose files so that an abort on postgresql doesn't get accidentally registered as a successful test. Then you'd just run those files in the order you need:
docker-compose -f docker-compose.yml up -d
docker-compose -f docker-compose.test.yml up
docker-compose -f docker-compose.yml down

docker-compose - unable to attach to containers

Using below docker-compose.yml file if I run "docker-compose up" or "docker-compose up -d" command then I see both containers status as exited however when I run docker restart <postgres-containerId> then its up and running but when I try to run docker restart <java8-containerId> then its restarting and again exiting.
Could you please suggest what parameter I need to specify to make these containers up and running after docker-compose up command and how do I attach to java container I tried with docker attach <java8-containerId> command but was not able to attach ?
docker-compose.yml file -
postgres:
image: postgres:9.4
ports:
- "5430:5432"
javaapp:
image:java8:latest
volumes:
- /pgm:/pgm
working_dir: /pgm
links:
- postgres
command: /bin/bash
docker-compose ps results -
Name Command State Ports
--------------------------------------------------------------------
compose_javaapp_1 /bin/bash Exit 0
compose_postgres_1 /docker-entrypoint.sh postgres Exit 0
To see available containers:
docker ps -a
To open container shell:
docker exec -it <container-name> /bin/bash