Why stop or rm with profiles stop all service? - docker

I'm trying to stop or rm services by passing a profile, but this stops all services instead of just the one with this profile.
The up command seems to work fine
This is my docker-compose.yaml
version: '3.9'
services:
registry:
container_name: registry
image: registry:latest
pull_policy: missing
profiles:
- registry_service_profile
...
portainer:
container_name: portainer
image: localhost:5000/my-portainer
pull_policy: missing
profiles:
- other_services_profile
...
pihole:
container_name: pihole
image: localhost:5000/my-pihole
pull_policy: missing
profiles:
- other_services_profile
...
bitwarden:
container_name: bitwarden
image: localhost:5000/my-bitwarden
profiles:
- other_services_profile
pull_policy: missing
...
homeassistant:
container_name: homeassistant
image: localhost:5000/my-homeassistant
pull_policy: missing
profiles:
- other_services_profile
...
duplicati:
container_name: duplicati
image: localhost:5000/my-duplicati
pull_policy: missing
profiles:
- other_services_profile
...
Running the stop command with both profiles stops all services, not just the ones with the provided profile
user#pc:/opt/docker$ sudo docker compose --profile registry_service_profile stop
[+] Running 6/6
⠿ Container pihole Stopped 4.6s
⠿ Container portainer Stopped 0.7s
⠿ Container registry Stopped 0.4s
⠿ Container homeassistant Stopped 5.0s
⠿ Container bitwarden Stopped 0.5s
⠿ Container duplicati Stopped 4.5s
user#pc:/opt/docker$ sudo docker compose --profile registry_service_profile up --detach
[+] Running 1/1
⠿ Container registry Started 0.4s
user#pc:/opt/docker$ sudo docker compose --profile other_services_profile up --detach
[+] Running 5/5
⠿ Container portainer Started 0.7s
⠿ Container homeassistant Started 0.2s
⠿ Container bitwarden Started 0.6s
⠿ Container pihole Started 0.9s
⠿ Container duplicati Started 0.9s
user#pc:/opt/docker$ sudo docker compose --profile other_services_profile stop
[+] Running 6/6
⠿ Container portainer Stopped 0.4s
⠿ Container pihole Stopped 4.5s
⠿ Container registry Stopped 0.6s
⠿ Container duplicati Stopped 4.4s
⠿ Container homeassistant Stopped 5.0s
⠿ Container bitwarden Stopped 0.5s
user#pc:/opt/docker$ docker --version
Docker version 20.10.17, build 100c701
Am I doing something wrong?
Or I should just run docker compose up command and this should update the services to the latest pulled version?

Looks like you want to stop only the services that contains the profile other_services_profile and not all the containers
I think you can't and this is still an issue of docker compose
https://github.com/docker/compose/issues/8139
NB: I tried to reproduce it, for me this behavior happens when using
docker compose --profile other_services_profile stop
but not when using
docker compose --profile other_services_profile down, in this case it remove all containers with only that profile (you will also get a message of it trying to delete the default network aswell but cannot as long as there is one container running still using it)
(I use docker 20.10.17 on windows)

This is an implementation choice. The profiles only work when creating or starting services. Take a look at this thread

Related

Docker compose stops multiple services

I hope I didn't miss anything simple from the manual.
The structure is:
/home/user
/foo1/bar1/docker/
Dockerfile
docker-compose.yml
/foo2/bar2/docker/
Dockerfile
docker-compose.yml
docker-compose.yml
version: '3.9'
services:
foo1-bar1:
build:
context: .
args:
DOCKER_SERVER_ROOT_DIR: ${DOCKER_SERVER_ROOT_DIR}
dockerfile: Dockerfile
image: foo1-bar1:v1
container_name: foo1-bar1-v1
The same is for foo-bar-v2.
Both of them I successfully run as:
cd /foo1/bar1/docker/
docker-compose up -d
[+] Running 1/1
⠿ Container foo1-bar1-v1 Started
cd /foo2/bar2/docker/
docker-compose up -d
[+] Running 1/1
⠿ Container foo2-bar2-v1 Started
The question is, why does it stop both of them when I try to stop only 1? Service names, container names, image names are different...
user#vm:~/foo1/bar1/docker$ docker-compose stop
[+] Running 2/2
⠿ Container foo1-bar1-v1 Stopped
⠿ Container foo2-bar2-v2 Stopped
docker-compose has the concept of projects. Run docker-compose --help and you will see:
--project-directory string Specify an alternate working directory
(default: the path of the, first specified, Compose file)
-p, --project-name string Project name
So in your case, both your services belong to the same project named docker.
You can actually run docker-compose -p docker ps and you will see both your services.
You can also override this by specifying your own project name independent of the directory name.
My version of docker-compose (Docker Compose version v2.10.2 MacOS) does warn me that there are orphan containers in this project when I replicate your setup. Also it doesn't automatically stop "orphan" services and gives a warning that the network could not be removed either.
This is also another interesting fact: both services run on the same network (docker_default) only because the project name (folder name) is the same.
I hope this explains it.
You have to specify the service to stop. Otherwise it will stop all services.
docker compose stop [OPTIONS] [SERVICE...]
here : docker-compose stop foo1-bar1

Docker-compose volume not storing files

I'm having trouble demonstrating that data I generate on a shared volume is persistent, and I can't figure out why. I have a very simple docker-compose file:
version: "3.9"
# Define network
networks:
sorcernet:
name: sorcer_net
# Define services
services:
preclean:
container_name: cleaner
build:
context: .
dockerfile: DEESfile
image: dees
networks:
- sorcernet
volumes:
- pgdata:/usr/share/appdata
#command: python run dees.py
process:
container_name: processor
build:
context: .
dockerfile: OASISfile
image: oasis
networks:
- sorcernet
volumes:
- pgdata:/usr/share/appdata
volumes:
pgdata:
name: pgdata
Running the docker-compose file to keep the containers running in the background:
vscode ➜ /com.docker.devenvironments.code $ docker compose up -d
[+] Running 4/4
⠿ Network sorcer_net Created
⠿ Volume "pgdata" Created
⠿ Container processor Started
⠿ Container cleaner Started
Both images are running:
vscode ➜ /com.docker.devenvironments.code $ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
oasis latest e2399b9954c8 9 seconds ago 1.09GB
dees latest af09040befd5 31 seconds ago 1.08GB
and the volume shows up as expected:
vscode ➜ /com.docker.devenvironments.code $ docker volume ls
DRIVER VOLUME NAME
local pgdata```
Running the docker container, I navigate to the volume folder. There's nothing in the folder -- this is expected.
vscode ➜ /com.docker.devenvironments.code $ docker run -it oasis
[root#049dac037802 opt]# cd /usr/share/appdata/
[root#049dac037802 appdata]# ls
[root#049dac037802 appdata]#
Since there's nothing in the folder, I create a file in called "dog.txt" and recheck the folder contents. The file is there. I exit the container.
[root#049dac037802 appdata]# touch dog.txt
[root#049dac037802 appdata]# ls
dog.txt
[root#049dac037802 appdata]# exit
exit
To check the persistence of the data, I re-run the container, but nothing is written to the volume.
vscode ➜ /com.docker.devenvironments.code $ docker run -it oasis
[root#1787d76a54b9 opt]# cd /usr/share/appdata/
[root#1787d76a54b9 appdata]# ls
[root#1787d76a54b9 appdata]#
What gives? I've tried defining the volume as persistent, and I know each of the images have a folder location at /usr/share/appdata.
If you want to check the persistence of the data in the containers defined in your docker compose, the --volumes-from flag is the way to go
When you run
docker run -it oasis
This newly created container shares the same image, but it doesn't know anything about the volumes defined.
In order to link the volume to the new container run this
docker run -it --volumes-from $CONTAINER_NAME_CREATED_FROM_COMPOSE oasis
Now this container shares the volume pgdata.
You can go ahead and create files at /usr/share/appdata and validate their persistence

How to prevent docker compose building the same image multiple times for docker compose V2?

This question builds off of the question asked here: How to prevent docker-compose building the same image multiple times?
In version 1 of docker compose, if you have multiple services which depend on the same dockerfile, you can prevent the dockerfile from being built twice by specifying the build tag once and referring the the image in dependent services:
version: '2'
services:
abc:
image: myimage
command: abc
build:
context: .
dockerfile: Dockerfile
xyz:
image: myimage
depends_on:
- abc
command: xyz
The above code runs properly after disabling version 2 of docker-compose:
docker-compose disable-v2
docker-compose up
However if you run docker-compose v2.3.3, it gives the following error:
[+] Running 0/2
⠿ abc Error 1.3s
⠿ xyz Error 1.3s
Error response from daemon: pull access denied for myimage, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
What is the proper way to have multiple services use one dockerfile in docker-compose version 2?

docker-compose up doesn't map ports on centos 7

I'm trying to docker-compose an existing application. Now I struggle on centos on a basic task.
Given docker-compose.yml:
version: '3.8'
services:
nginx:
image: nginx:latest
ports:
- "1024:80"
When run with docker-compose up localhost:1024 is not accessible. When I add the started container to the default bridge network (docker network connect <id of bridge> <id of container>), localhost:1024 is accessible.
When run with docker run -p 1024:80 nginx:latest localhost:1024 is accessible.
docker-compose version 1.26.2, build eefe0d31
Docker version 19.03.12, build 48a66213fe
CentOS Linux release 7.6.1810
When attempting to docker-compose up the same file on macOS localhost:1024 is accessible.
EDIT #1
docker ps PORTS output: 0.0.0.0:1024->80/tcp
new interface added ifconfig: br-da0abb61196d
Same behavior on different ports e.g. 8080

Running a ubuntu container in background using docker compose

I am able to run a docker container using following docker command:
docker run -it ubuntu /bin/bash
Now I am trying to do it by using docker-compose:
version: "3"
services:
ubuntu:
container_name: ubuntu
image: ubuntu
restart: on-failure
command: "/bin/bash"
Now when I do :
docker-compose up -d
Can see docker container starting and exiting immediately.
I tried looking at the logs :
docker logs b8 //b8 is container id
But there are no error logs.
How do I keep ubuntu container running in background using docker.
( I am using docker on windows , linux version)
This is normal.
You are starting an ubuntu container with bash as the command (thus the root process). The thing is to keep bash alive you need to attach it with a terminal. This is why when you want to get a bash in a container, you're using -ti with your command :
docker container exec -ti [my_container_id] bash
So if you want to keep your ubuntu container alive and don't want to attach it to a terminal, you'll have to use a process that will stay alive for as long as you want.
Below is an example with sleep infinity as your main process
version: "3"
services:
ubuntu:
container_name: ubuntu
image: ubuntu
restart: on-failure
command: ["sleep","infinity"]
With this example, you container will stay running indefinitely.

Resources