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?
CodePudding user response:
Add a container_name
to the service in your docker-compose.yml
file like so:
services:
...
postgres:
container_name: my-container
image: postgres:12.1-alpine
...
Then you can use the following docker
commands to do what you're looking for:
docker-compose ps
docker stop my-container
docker rm my-container
docker-compose up
CodePudding user response:
It should be enough to run
docker-compose up --build -d
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.