When I tried to connect to a new version of DB by upgrading the middleware, the port was taken by the container built in the previous version and I could not connect to mysql.
WARNING: Image for service hoge was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Recreating hoge_mysql ... done
Creating hoge_apache_php74 ...
Creating hoge_apache_php74 ... error
ERROR: for hoge_apache_php74  Cannot start service web74: driver failed programming external connectivity on endpoint hoge_apache_php74 (f9dfceb538d7f688ad0d26269810a7d889): Bind for 0.0.0.0:8081 failed: port is already allocated
ERROR: for web74  Cannot start service web74: driver failed programming external connectivity on endpoint hoge_apache_php74 (f9dfceb538d7f841d4b2a7d26269810a7d889): Bind for 0.0.0.0:8081 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
Bind for 0.0.0.0:8081 failed: port is already allocated
The error message says that the port has already been assigned. You can change the port of the new container, but since it is unified in the project, you do not touch the Dockerfile I decided to delete the existing container and rebuild it with a clean state.
docker % docker ps -a   //List of containers including when stopped
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
d13d68d47e2c        docker_web74        "/sbin/init"             About an hour ago   Created                                      hoge_apache_php74
bb1e84be87ce        docker_mysql        "docker-entrypoint.s…"   About an hour ago   Up About an hour    0.0.0.0:3306->3306/tcp   hoge_mysql
5a06cd43f761        docker_web          "apache2-foreground"     2 hours ago         Up 2 hours          0.0.0.0:8081->80/tcp     hoge_apache_php
docker % docker-compose top  //docker-Process confirmation of working container created by compose
hoge_mysql
PID    USER   TIME   COMMAND
----------------------------
2889   999    1:56   mysqld
docker % docker-compose stop //docker-Stopping services created with compose
Stopping hoge_mysql ... done
docker % docker-compose kill //docker-Forced stop of container created by compose
docker % docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                  NAMES
d13d68d47e2c        docker_web74        "/sbin/init"             About an hour ago   Created                                           hoge_apache_php74
bb1e84be87ce        docker_mysql        "docker-entrypoint.s…"   About an hour ago   Exited (0) 2 minutes ago                          hoge_mysql
5a06cd43f761        docker_web          "apache2-foreground"     2 hours ago         Up 2 hours                 0.0.0.0:8081->80/tcp   hoge_apache_php
docker % docker-compose down //docker-Stop the container created by compose and delete the container and network
WARNING: Found orphan containers (hoge_apache_php) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Removing hoge_apache_php74 ... done
Removing hoge_mysql        ... done
Removing network docker_default
ERROR: error while removing network: network docker_default id 46a29f96ef988d2366e0af50d1eb18690bff5db87e7b7cacee8e91d07097702e has active endpoints
docker % docker-compose top //docker-Make sure there are no running containers created with compose
docker % docker-compose ps -a
Name   Command   State   Ports
------------------------------
docker % docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                  NAMES
5a06cd43f761        docker_web          "apache2-foreground"   2 hours ago         Up 2 hours          0.0.0.0:8081->80/tcp   hoge_apache_php
docker % docker ps -aq | xargs docker rm //Delete all containers
Error response from daemon: You cannot remove a running container //Cannot delete all if there is a running container 5a06cd43f76166e8dc9c3517c3f6d8aeda7315f134b4d35865d893e85fa8ac79. Stop the container before attempting removal or force remove
docker % docker stop 5a06cd43f761 //Stop the operation of the container
5a06cd43f761
docker % docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES
5a06cd43f761        docker_web          "apache2-foreground"   2 hours ago         Exited (0) 4 seconds ago                       hoge_apache_php
docker % docker ps -aq |xargs docker rm ...//Delete all containers again
5a06cd43f761
docker % docker ps -a //Make sure all containers have been deleted
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
docker % docker-compose up //Up after being built
The container created by docker-compose was stopped by the stop, down, and kill commands, and the container and network were deleted. And for containers that don't use docker-compose If I checked with docker ps -a and it was running, I stopped it with docker stop {docker ID}. Finally, docker ps -aq | xargs docker rm was used to delete all the containers, leaving them completely empty. When I tried using docker ps -a and docker-compose ps -a while checking the status of the container, the container remained and the network was not disconnected, so I executed stop and kill quite persistently. If you cannot execute the command you want to do, you should check the status of the container frequently and execute the command.
https://qiita.com/etaroid/items/88ec3a0e2d80d7cdf87a https://qiita.com/nimusukeroku/items/72bc48a8569a954c7aa2 https://qiita.com/tettsu__/items/302a11fe848dc8fe1f55
Recommended Posts