docker コンテナ作成時に、同じ名前のコンテナがすでにある場合の対応 docker

コンテナ作成時に、同じ名前のコンテナがすでにある場合の対応
前のが不要な場合はそれを、事前に削除すればいい

■エラー内容
λ docker-compose up -d 0.1s WARNING: Image for service service_hoge_db was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating hoge_db ... error

ERROR: for hoge_db Cannot create container for service service_hoge_db: Conflict. The container name /hoge_db is already in use by container d4a4d3f2421a9fec2a820f3fd73c6d48f5215f879965a430b8301fcf159b17a4 . You have to remove (or rename) that container to be able to reuse that name.
ERROR: for service_hoge_db Cannot create container for service service_hoge_db: Conflict. The container name /hoge_db is already in use by container d4a4d3f2421a9fec2a820f3fd73c6d48f5215f879965a430b8301fcf159b17a4 . You have to remove (or rename) that container to be able to reuse that name.
ERROR: Encountered errors while bringing up the project.


■対応
・docker ps でコンテナIDを確認
λ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4a4d3f2421a postgres docker-entrypoint.s… 21 hours ago Up 21 hours 5432/tcp 0.0.0.0:9010->9010/tcp :::9010->9010/tcp hoge_db
→この名前が同じで、(不要なので)削除

・コンテナ削除
docker rm --force <コンテナID>
λ docker rm --force d4a4d3f2421a

・再度起動でok
docker-compose up -d