Chapter 6: PostgresSQL docker error

So I ran this command in terminal:

docker run --name postgres -e POSTGRES_DB=vapor \
-e POSTGRES_USER=vapor -e POSTGRES_PASSWORD=password \
-p 5432:5432 -d postgres

Then closed terminal and a day later I reopened terminal and ran the command above again, but this time I get an error:

docker: Error response from daemon: Conflict. The container name "/postgres" is already in use by container "945bd087501137a418eb8e0a7c7665fb2b7bb8c37e2afe22bf52e1200e89735e". You have to remove (or rename) that container to be able to reuse that name.

What does that mean? I’m not sure what to do next, is it ok to remove the container (how)?

Additionally, when I run xcode, I get this runtime error:

Thread 1: Fatal error: Error raised at top level: NIO.ChannelError.connectFailed(NIO.NIOConnectionError(host: "localhost", port: 5432, dnsAError: nil, dnsAAAAError: nil, connectionErrors: [NIO.SingleConnectionFailure(target: [IPv6]localhost/::1:5432, error: connection reset (error set): Connection refused (errno: 61)), NIO.SingleConnectionFailure(target: [IPv4]localhost/127.0.0.1:5432, error: connection reset (error set): Connection refused (errno: 61))]))

I’m not sure what it means.

It basically means that there is already a container called postgres running so it can’t create another one with that name. If you run docker ps -a you can see all your containers. Since you already have the container started, you have a few options:

  • start the container that is already created with - docker start postgres
  • delete the old container before trying to start it again - docker rm postgres. This is also a useful command if you want to reset your DB

The second error from Xcode basically says that it can’t connect to the DB on localhost:5432 - because the created Docker container is not started.

Hope that helps!

3 Likes

Thanks for the quick reply and on a Saturday!

It fixed the problem!

But does it mean that every time I close Terminal, I have to delete the container with docker rm postgres? I’ll have to reboot my computer at some point :slight_smile:

Maybe you could add this detail to the book?

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

The useful commands have been added to the second edition of the book! Does it definitely stop when you close the terminal? If you’re passing the -d flag it should stay alive.

You shouldn’t need to remove the container and start again when restarting your computer, you should just need to do docker start postgres and be good to go.

1 Like

I am using the -d flag, as shown in my original post. (I am copy pasting from the book to Terminal). However, it does NOT stay alive. I get this error again:

docker: Error response from daemon: Conflict. The container name "/postgres" is already in use by container "3bb129804d7ae62364fa128123df388afa7cc41149dbfafb8bbbaae02d29200d". You have to remove (or rename) that container to be able to reuse that name.

Running docker start postgres doesn’t seem to do anything.

If you do docker start postgres do you get an UUID printed out on Terminal? What does docker ps -a show?

I’m sorry, I spoke too soon. This is the output:

$ docker start postgres
postgres
$

And docker ps -a does print a UUID.

I added new routes to the code and forgot to deploy to vapor cloud that’s why my requests failed and I thought it was because docker wasn’t running.

After deploying to vapor cloud, all the requests are running properly.

Thanks again.

1 Like

Glad you got it working!

Thank you. You help me solve the problem!

1 Like