Running all containers in detached mode - docker-compose

Iv'e recently started to work with docker-compose and I wondered how can I start all containers in my docker-compose file in detached mode.
After searching on the web for a bit I found a solution which was:
add the following properties to a container tty: true and stdin_open: true and then I attach to container and detach with ctrl + p and ctrl + q without killing the container.
The simple ang ugly solution it to add the properties to all containers, but I wonder if there is s nicer solution for this, maybe something with the docker-compose up that can somehow start all containers in detached mode

To start containers in detached mode (as already mentioned above) you need to use the docker-compose up -d.
If you need to manually get in and out of the running container once it's started to perform some additional tasks, you can use docker-compose exec.

Related

docker-compose - issue restarting single service

I am using docker-compose for a development project. I have 6 services defined in my docker compose file. I have been using the below script to rebuild the images whenever I make a change.
#!/bin/bash
# file: rebuild.sh
docker-compose down
docker-compose build
docker-compose up
I am looking for a way to reduce the build time as building and restarting all the services seems unnecessary as I am usually only changing one module. I see in the docker-compose docs you can run commands for individual services by specifying the service name after e.g. docker-compose build myservice.
In another terminal window I tried docker-compose build myservice && docker-compose restart myservice while leaving the other ./rebuild.sh command open in the original terminal. In the ./rebuild.sh terminal window I see all the initialization messages being reprinted to the stdout so I know it is restarting that service but the code changes aren't there. What am I doing wrong? I just want to rebuild and restart a single service.
Try:
docker-compose up -d --force-recreate --build myservice
Note that:
-d is for Detached mode,
-force-recreate will recreate containers even is your code did not change,
-build is for build your images before starting containers.
At least the name of your service.
Take a look here.

can we remove docker-compose down from steps?

Is it necessary to use "docker-compose down" before "docker-compose up". I dont want my application go down. I am using docker-compose at this point of time and having no plan to move to kubernetes etc.
If we remove "docker-compose down" then how it will handle the orphan-volumes and images?
Any pointer is highly appreciated.
Thanks,
Sanjiv
No, you don't necessarily have to use docker-compose down before a docker-compose up. If you use docker-compose up on a running service stack, docker-compose will just recreate the services that have been changed. Changed either through:
a changed docker-compose.yml, or
updated images (either because you pulled new images, or rebuild them yourself).
To remove orphaned volumes, you have to issue a special flag --remove-orphans , see docker-compose up. But that behavior is the same with docker-compose down.
Also images are not changed with neither command. The difference is that with docker-compose down & docker-compose up, all running containers are removed and recreated from their images. So in case data was written inside the container, that data will be lost.

How to Start Cron / Crond inside the Official Postgres Container

The crond is not running by default in the official postgres alpine image. How could I define my Dockerfile to make sure that the daemon runs in the background? I want that it is running by default, if possible even when the container gets restarted.
I tried to add CMD ["/usr/sbin/crond"] to my Dockerfile but I didn't succeed. Any thoughts how to run this in combination with postgres?
Update
I have added the answer of tianon:
[...]
If you must run crond inside a container, I'd recommend instead using
a separate container which runs nothing but crond (and thus Docker can
both track its lifecycle, and restart it when/if it fails, the machine
restarts, etc). You should be able to connect to the PostgreSQL
instance from a second container, but if absolutely necessary, one
could use things like --network container:some-postgres in order to
join the network namespace of the database container directly.
pg_cron must be added to shared_preload_libraries. Per the docs:
# add to postgresql.conf:
shared_preload_libraries = 'pg_cron'
and you must then restart PostgreSQL.

docker-compose up, down, stop start difference

I cant find more information about those.
Should we use docker stop for containers which we started with docker start
Or same for docker-compose up?
What is the difference between stop and down?
In docker-compose help
stop Stop services
down Stop and remove containers and networks (optionally images and volumes as well)
# Stop services only
docker-compose stop
# Stop and remove containers, networks..
docker-compose down
# Down and remove volumes
docker-compose down --volumes
# Down and remove images
docker-compose down --rmi <all|local>
Following are the differences among various docker-compose command options:
docker-compose up - start and restart all the services defined in docker-compose.yml
docker-compose down - command will stop running containers, but it also removes the stopped containers as well as any networks that were created. You can take down one step further and add the -v flag to remove all volumes too. This is great for doing a full blown reset on your environment by running docker-compose down -v.
docker-compose start - command will only restart containers stopped previously
docker-compose stop - command will stop running containers but won’t remove them
Just to answer the other part of the question:
Use docker-compose up to start or restart all the services defined in a docker-compose.yml.
The docker-compose start command is useful only to restart containers that were previously created, but were stopped. It never creates new containers.
The docker-compose run command is for running “one-off” or “adhoc” tasks.
For further information visit this page.

why docker not executing init as lxc

Docker is not running init. So services are not started during startup. Lxc runs init during lxc-start.Since Docker is using lxc why it is not running init. What are the advantages of not running init and depending on supervisord for daemonization?
I think that running /sbin/init is just default behaviour in lxc-start, it awaits a command to be run. There is no default command parameter for run command in docker.
You can run init explicitly in docker:
docker run ubuntu /sbin/init
Personally, I like this behaviour - I prefer to use container for my few apss related processes and I do not need init to be started.
The advantage simply is to keep your container light-weight. You decide which processes to run, and no more than that. That way, docker can start a container really really fast.
By the way, you don't depend on supervisord, as you could for instance write a complicated shell script which you put in your command.
One of the applications of docker is to set it up as an executable. E.g. you can make images that run unit or integration tests. Now, you wouldn't want each of those to run several dozens of services that you don't use, right?