Run DockerCompose File in Background with the specific configuration(yaml) file - docker

I need docker-compose to run all the containers in the background.
I know, if i have one docker-compose.yaml. Then, it is go to the directory in which the configuration file is present and run:
docker-compose up -d
But, In my case, i have 3 docker-compose.yaml files in the configuration folder. And whenever, i run
docker-compose -d docker-compose.yaml up
It doesn't executes/ runs the container.
But, if i run docker-compose -f docker-compose.yaml up. Then, the docker containers will start executing on the foreground terminal.
I want these dockers to run detached from the terminal. How can i achieve that?

Docker has -d option for detached or you can use the usual & at the end of any linux command
docker-compose -f docker-compose.yaml up -d
P.s: It is a good idea to give a name to your container (specially when using -d) using the --name={some_name} so that you can easily get back to that container later using the name

Run below to command to specify docker-compose.yml file including build and send in background;
docker-compose -f docker-compose-1.yml up -d --build
This will also build new image if there are any changes in the project else it will run the same container.

Related

how to reopen a docker container window again

I was able to run a docker container but if I do sudo docker-compose up -d but how to reopen/watch the screen again if I need and close again. I am using ubuntu.
Thanks
In order to follow the logs of all of the containers that are included in the docker-compose.yml file, run the command docker-compose logs -f (probably with sudo in your case) in the same directory in which you already ran sudo docker-compose up -d. You can find more information on the command here.
You are probably looking for docker attach (documentation). Usage is:
docker attach [OPTIONS] CONTAINER

How to uninstall a Docker Image

I ran this command docker-compose up -d inside a directory called hosting.
How can I uninstall that image it created, so I can reinstall it.
You can remove the image by using the command:
docker rmi <image-name>:<image-version>
To list all images you can use:
docker images
You can simply again go to the hosting directory and run this:
docker-compose down
Or via -f argument specify the docker-compose file path:
docker-compose -f /path_to/hosting/docker-compose.yml down
When you use this, it stops the created containers and removes them.
Also if you want to update it, just do your work in your project (update the codes, the files and so on), then run this command to build the container:
docker-compose up -d --build

docker compose exec changing my parameter value [duplicate]

I was trying to open a second terminal un a docker with docker-compose.
First run the container with
docker-compose run my-centos bash
And when I try to open a second terminal
docker-compose exec my-centos bash
I get the message
ERROR:No container found for my_centos_1
If I search the name of running container I get
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34a95b44f0a2 centos6 "bash" 9 minutes ago Up 9 minutes docker_my-centos_run_1
why docker-compose exec search docker_my_centos_1 and not docker_my-centos_run_1?
docker-compose is meant to run multi-container applications and is supposed to be used with docker-compose up. When you use docker-compose run, you make a special container that's not really meant for normal use.
Since docker-compose is just a wrapper around docker, you can still access this special container via the normal docker command:
docker exec docker_my-centos_run_1 bash
Otherwise I'd suggest start your container with docker-compose up. This makes it so that you can run the second bash in the way that you specified:
docker-compose exec my-centos bash
Note: I don't know if you can attach a TTY directly with docker-compose up, so you might need to run an extra docker-compose exec my-centos bash to get two TTYs.
you need to do "docker-compose up -d"
then try ur commands
I think you may be having trouble with the project name, because you're not telling Docker with project are you referring to.
I was facing this problem and it can be fixed adding the project name like:
docker-compose -p myprojectname ps
or
docker-compose -p myprojectname exec php_service composer install
This post explain it: https://codereviewvideos.com/blog/how-i-fixed-docker-compose-exec-error-no-container-found-for/
I solved this issue by first running
sudo systemctl restart docker
if you are using docker-compose run this command
docker-compose run web python manage.py makemigrations

Running Command "docker-compose run web ..." creates a new container in Kitematic container list

Each time I run the command "docker-compose run web ..." it results in another container being added to Kitematic, so that I have a list of containers like "image-name_web_run_1" and so on with each run of docker-compose.
Is this expected behaviour? If so, how do I work around it?
It is expected. You can use docker-compose run --rm to have the container removed when it exits.
You can clean up these old containers using docker-compose down or docker-compose rm -a.
Do you mean new container? That is the expected behavior for docker-compose run, see: https://docs.docker.com/compose/reference/run/
Did you mean to use docker-compose exec?
e.g., docker-compose exec web /bin/sh
See: https://docs.docker.com/compose/reference/exec/

How to rebuild docker container in docker-compose.yml?

There are scope of services which are defined in docker-compose.yml. These services have been started. I need to rebuild only one of these and start it without up other services.
I run the following commands:
docker-compose up -d # run all services
docker-compose stop nginx # stop only one. but it is still running !!!
docker-compose build --no-cache nginx
docker-compose up -d --no-deps # link nginx to other services
At the end I get the old nginx container.
Docker-compose doesn't kill all running containers!
docker-compose up
$ docker-compose up -d --no-deps --build <service_name>
--no-deps - Don't start linked services.
--build - Build images before starting containers.
With docker-compose 1.19 up
docker-compose up --build --force-recreate --no-deps [-d] [<service_name>..]
Without one or more service_name arguments all images will be built if missing and all containers will be recreated.
From the help menu
Options:
-d, --detach Detached mode: Run containers in the background,
print new container names. Incompatible with
--abort-on-container-exit.
--no-deps Don't start linked services.
--force-recreate Recreate containers even if their configuration
and image haven't changed.
--build Build images before starting containers.
Without cache
To force a rebuild to ignore cached layers, we have to first build a new image
docker-compose build --no-cache [<service_name>..]
From the help menu
Options:
--force-rm Always remove intermediate containers.
-m, --memory MEM Set memory limit for the build container.
--no-cache Do not use cache when building the image.
--no-rm Do not remove intermediate containers after a successful build.
Then recreate the container
docker-compose up --force-recreate --no-deps [-d] [<service_name>..]
This should fix your problem:
docker-compose ps # lists all services (id, name)
docker-compose stop <id/name> #this will stop only the selected container
docker-compose rm <id/name> # this will remove the docker container permanently
docker-compose up # builds/rebuilds all not already built container
As #HarlemSquirrel posted, it is the best and I think the correct solution.
But, to answer the OP specific problem, it should be something like the following command, as he doesn't want to recreate ALL services in the docker-compose.yml file, but only the nginx one:
docker-compose up -d --force-recreate --no-deps --build nginx
Options description:
Options:
-d Detached mode: Run containers in the background,
print new container names. Incompatible with
--abort-on-container-exit.
--force-recreate Recreate containers even if their configuration
and image haven't changed.
--build Build images before starting containers.
--no-deps Don't start linked services.
Maybe these steps are not quite correct, but I do like this:
stop docker compose: $ docker-compose down
WARNING: The following prune -a will delete all images, you may not want this as it could effect other projects. you can read more here
remove the container: $ docker system prune -a
start docker compose: $ docker-compose up -d
docker-compose stop nginx # stop if running
docker-compose rm -f nginx # remove without confirmation
docker-compose build nginx # build
docker-compose up -d nginx # create and start in background
Removing container with rm is essential. Without removing, Docker will start old container.
For me it only fetched new dependencies from Docker Hub with both --no-cache and --pull (which are available for docker-compose build.
# other steps before rebuild
docker-compose build --no-cache --pull nginx # rebuild nginx
# other steps after rebuild, e.g. up (see other answers)
The problem is:
$ docker-compose stop nginx
didn't work (you said it is still running). If you are going to rebuild it anyway, you can try killing it:
$ docker-compose kill nginx
If it still doesn't work, try to stop it with docker directly:
$ docker stop nginx
or delete it
$ docker rm -f nginx
If that still doesn't work, check your version of docker, you might want to upgrade.
It might be a bug, you could check if one matches your system/version. Here are a couple, for ex:
https://github.com/docker/docker/issues/10589
https://github.com/docker/docker/issues/12738
As a workaround, you could try to kill the process.
$ ps aux | grep docker
$ kill 225654 # example process id
Simply use :
docker-compose build [yml_service_name]
Replace [yml_service_name] with your service name in docker-compose.yml file. You can use docker-compose restart to make sure changes are effected. You can use --no-cache to ignore the cache.
You can use:
docker-compose build
And if you are using a docker profile:
docker-compose --profile profile_name build
Only:
$ docker-compose restart [yml_service_name]

Resources