7

I am new to docker and I'm trying to get a permanent installation of Rancher started. To create the docker container I run the following command:

docker run -d --name rancher-server -p 8080:8080 rancher/server

Note that I want to forward the container's 8080 port to my hosts' 8080, since 80 is occupied by nginx on my host.

Now, when I stop the above container and try to start it again using docker start <Container ID> I get the following error:

Error response from daemon: driver failed programming external connectivity on endpoint rancher-server (c18940f957ed1f737fd5453ea29755adea762d758643a64984d5e3ce8bd3fdbe): Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use Error: failed to start containers: c93794a8c0ad

I know that this happens since nginx is using port 80, so my question is how do I start my existing container and tell it to forward its ports?

Running docker start -d -p 8080:8080 c93794a8c0ad gives me the following error: unknown shorthand flag: 'd' in -d

So how do I start a container with forwarded ports? Thank you!

Lucas P.
  • 4,282
  • 4
  • 29
  • 50

4 Answers4

4

The problem might be that two programs are working on the same port. You can change the port settings when you are running the docker run command. For instance, you can bind port 8080 of the container with an arbitrary port on your computer, like 8081:

docker run -d --name rancher-server -p 8081:8080 rancher/server

The left-hand port number is the docker host port - your computer - and the right-hand side is the docker container port.

You CAN modify the ports

You can change the ports of a docker container without deleting it. The way quin452 puts it - with minor revision:

  1. Get the container ID:

    docker ps -a

  2. Stop the container:

    docker stop [container name]

  3. Edit the container hostconfig.json file, found at

    var/lib/docker/containers/[container ID]/hostconfig.json

  4. Within the PortBindings section, either edit the existing HostPort to the port you would like, or add them yourself (see below)

  5. Save and exit the config file

  6. Restart docker:

    sudo systemctl restart docker

  7. Start up the container:

    docker start [container name]

An example config file:

"PortBindings": {
    "3306/tcp": [
        {
            "HostIp": "",
            "HostPort": "23306"
        }
    ],
    "443/tcp": [
        {
            "HostIp": "",
            "HostPort": "2443"
        }
    ],
    "80/tcp": [
        {
            "HostIp": "",
            "HostPort": "280"
        }
    ]
}
Pedram
  • 921
  • 1
  • 10
  • 17
  • As of Docker 20.10, this approach won't work for me unless I add `"ExposedPorts":{"3306/tcp":{},"443/tcp":{},"80/tcp":{}}` to the container's `config.v2.json` before restarting `dockerd`. – ternary Apr 13 '22 at 08:55
  • Hey there @ternary , feel free to edit the answer as you see fit :) – Pedram Aug 25 '22 at 15:54
1

I deleted the container and created a new one with the command the Rancher docs recommend sudo docker run -d --restart=unless-stopped -p 8080:8080 rancher/server and now stopping and starting the container work as expected, on the correct ports. I don't know what the problem was before, but it now works.

Lucas P.
  • 4,282
  • 4
  • 29
  • 50
0

To change port mappings, you need to delete and recreate the container. So docker rm your existing container then docker run it with the new port settings.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Thanks for the answer! I do not want to change the mappings, I want to run my container with the same mappings, but when I try to start it suing `docker start `, I get the error described in my question. Any ideas? – Lucas P. Jul 22 '18 at 19:47
  • That error message says to me that you had previously launched it with `docker run -p 80:8080`. `docker ps` will tell you for sure. – David Maze Jul 22 '18 at 20:19
  • When I run `docker ps` there are no running containers, as I have made sure I've run `docker stop` before (ore rebooted my server, which is actually the case I'm trying to fix here). Should the container have already forwarded its ports after it is created? If so why am I unable to start it again? I repeat, running `docker ps` shows that there are no containers running. – Lucas P. Jul 22 '18 at 22:39
  • 1
    You can't change the port mappings on an extant container. `docker ps -a` will show stopped containers too. – David Maze Jul 22 '18 at 23:05
  • Ok I guess something else is going on then, since I cannot start my stopped container. I do not want to change the ports, I just want to start it again. But I get the error described above (that port 80 is already in use), so I'm guessing the container is not using the port 8080 as intended when I first created it – Lucas P. Jul 23 '18 at 07:52
0

Try to use the following:

$docker run -d --name rancher-server -p 8080:80 rancher/server
Pang
  • 9,564
  • 146
  • 81
  • 122
Badrn
  • 1
  • 1