So I have an app deployed with docker but I’m tired to delete and rebuild the image everytime so i used wrote a script to do everything for me:
#!/usr/bin/env sh
docker-compose ps # lists all services (id, name)
docker-compose stop dee506ab283a #this will stop only the selected container
docker-compose rm dee506ab283a # this will remove the docker container permanently
docker-compose up
The problem is that the container wont have the same id everytime so this only works once. How can I get the new generated id so I can just run this script everytime?
2
Answers
Add a
container_name
to the service in yourdocker-compose.yml
file like so:Then you can use the following
docker
commands to do what you’re looking for:It should be enough to run
without doing any of the other steps you describe. This will rebuild any image you mention with a
build:
description in the Compose file, and then delete and recreate any containers whose configuration or images have changed.If you do need any of the other commands, all of the imperative
docker-compose
commands accept the Compose service names as arguments;docker-compose stop app
for example. You do not need to look up the container ID or manually specify the container name.