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

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]

Related

Docker compose: remove container without interaction

For a deployment pipeline I need to remove Docker container without user interaction.
When removing a Docker container using
$ docker compose rm myapp
docker compose wants a confirmation and only continues when y was entered.
How to tell docker compose to remove volumes continue without typing in something?
My Docker version is 20.10.21
There's an option to do that:
Usage: docker compose rm [OPTIONS] [SERVICE...]
Removes stopped service containers
[...]
Options:
-f, --force Don't ask to confirm removal
-s, --stop Stop the containers, if required, before removing
-v, --volumes Remove any anonymous volumes attached to containers
So the solution is
$ docker compose rm myapp -f

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

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

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.

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

After docker-compose build the docker-compose up run old not updated containers

I use docker-compose and find following problem:
When I change my code and want to rebuild dockers I use
docker-compose stop
docker-compose build
And then I want to run system by:
docker-compose up
But no new version of code/containers are run but old ones. What to do?
You could use, docker-compose up --build or docker-compose up --build --force-recreate
I have a helper function to nuke everything so that our Continuous blah, cycle can be tested, erm... continuously. Basically it boils down to the following:
To clear containers:
docker rm -f $(docker ps -a -q)
To clear images:
docker rmi -f $(docker images -a -q)
To clear volumes:
docker volume rm $(docker volume ls -q)
To clear networks:
docker network rm $(docker network ls | tail -n+2 | awk '{if($2 !~ /bridge|none|host/){ print $1 }}')
I generally don't require old containers, volumes and networks, so to clear them all I made a bash script which runs to clean up docker environment before each build. And to rebuild the docker using updated code, I use docker-compose up --build
Credits to marcelmfs and borrowed from Source
In this case first we should remove old containers (by rm -f). So we can deploy new code by:
docker-compose build
docker-compose stop
docker-compose rm -f
docker-compose up
Above sequence is not coincidence - when first instruction build image, the old images running - but when building is finish then old container is stopped, deleted and exchange by new builded one.
I put above commands in handy copy-paste oneliner:
docker-compose build && docker-compose stop && docker-compose rm -f && docker-compose up

Resources