How to solve when you cannot connect to DB with a new container because the port is assigned to the existing docker container

Thing you want to do

I want to assign a port to a new container because it is already assigned to an existing container

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

Construction completed

Summary

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.

reference

https://qiita.com/etaroid/items/88ec3a0e2d80d7cdf87a https://qiita.com/nimusukeroku/items/72bc48a8569a954c7aa2 https://qiita.com/tettsu__/items/302a11fe848dc8fe1f55

Recommended Posts

How to solve when you cannot connect to DB with a new container because the port is assigned to the existing docker container
How to deal with `Cannot connect to the Docker daemon at unix: /// var/run/docker.sock. Is the docker daemon running?`
[Docker] How to build when the source code is bind-mounted on the container
[Docker] How to see the contents of Volumes. Start a container with root privileges.
[Solution] What to do if you get a Docker error'ERROR: Cannot connect to the Docker daemon at unix: ///var/run/docker.sock. Is the docker daemon running?'
How to connect a container created later to an existing network
[Docker] When you enter the db container and look at the data, Japanese is displayed as ???
How to solve the problem that you can not pull image from docker hub with Minikube
When requested access to the resource is denied when pushing with Docker
How to build an environment with Docker, which is the minimum required to start a Rails application
Let's create a Docker container that can connect to CentOS 8 with the minimum configuration by SSH
[Spring Dtata JPA] How to deal with the problem that DB change cannot be detected when you want to process API synchronously with a single thread in Spring Boot.
Pg_resetwal can be used to start the PostgreSQL Docker container when WAL is broken and cannot be started.
You cannot change the project facet version from a dynamic web module to x.x. How to deal with
[Docker] Does not connect to MySQL workbench ... DB container port forwarding settings
How to start a Docker container with a volume mounted in a batch file
What to do when is invalid because it does not start with a'-'
How to get a heapdump from a Docker container
How to build a Jenkins server with a Docker container on CentOS 7 of VirtualBox and access the Jenkins server from a local PC
How to delete the tweet associated with the user when you delete it at the same time
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
[Docker] How to update using a container on Heroku and how to deal with Migrate Error
[Rails] How to solve the error "undefined method` visit'" when using Capybara with Rspec
A warning is displayed when trying to use a huge integer with the special variables $ 1, $ 2, $ 3 ...
[Rails] What to do when rails db: migrate cannot be done because there is no table referenced by the foreign key
[Java] How to start a new line with StringBuilder
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
How to write in Model class when you want to save binary data in DB with PlayFramework
How to solve the problem when the value is not sent when the form is disabled in rails and sent
Solution for the error "no basic auth credentials" when pushing a Docker container to Heroku
What to do if you get a "Cannot Pull Container Error" when starting ECS ​​Fargate
How to reduce the load on the program even a little when combining characters with JAVA
[Rails] What to do when the view collapses when a message is displayed with the errors method
Wait for the container service to start with docker healthcheck
How to specify db when creating an app with rails
[Docker] Delete only the volume associated with a specific container
Time is wrong with the application launched on the Docker container
docker-compose.yml when you want to keep mysql running with docker
[Docker] How to access the host from inside the container. http://host.docker.internal:
How to execute action attribute processing only when the confirmation dialog with h: commandButton is OK
I can no longer connect to a VM with a Docker container that can be connected via SSH
I was angry with proc_open (): fork failed when trying to composer update inside a Docker container
What to do if the app is not created with the latest Rails version installed when rails new