Docker Compose does not bind ports

I have the following Docker file for my container:

FROM centos:centos7 # Install software RUN yum -y update && yum clean all RUN yum install -y tar gzip wget && yum clean all # Install io.js RUN mkdir /root/iojs RUN wget https://iojs.org/dist/v1.1.0/iojs-v1.1.0-linux-x64.tar.gz RUN tar -zxvf iojs-v1.1.0-linux-x64.tar.gz -C /root/iojs RUN rm -f iojs-v1.1.0-linux-x64.tar.gz # add io.js to path RUN echo "PATH=$PATH:/root/iojs/iojs-v1.1.0-linux-x64/bin" >> /root/.bashrc # go to /src WORKDIR /src CMD /bin/bash 

I create this container and run the image using docker run -i -t -p 8080:8080 -v /srv/source:/usr/src/app -w /usr/src/app --rm iojs-dev bash . Docker associates port 8080 with host port 8080 so that I can access the iojs application from my client. Everything is working fine.

Now I want to start my container using docker-compose using the following docker-compose.yml file

 webfrontend: image: iojs-dev links: - db command: bash -c "iojs test.js" ports: - "127.0.0.1:8080:8080" volumes: - /srv/source:/usr/src/app - /logs:/logs db: image: mariadb environment: MYSQL_ROOT_PASSWORD: 12345 

When I run docker-compose run webfrontend bash , I cannot access port 8080 on my host. The port was not tied. The result of docker ports empty, and the result of docker inspect empty in the port settings:

 "NetworkSettings": { "Bridge": "docker0", "Gateway": "172.17.42.1", "IPAddress": "172.17.0.51", "IPPrefixLen": 16, "MacAddress": "02:42:ac:11:00:33", "PortMapping": null, "Ports": { "8080/tcp": null } }, "HostConfig": { "Binds": [ "/srv/source:/usr/src/app:rw", "/logs:/logs:rw" ], "CapAdd": null, "CapDrop": null, "ContainerIDFile": "", "Devices": null, "Dns": null, "DnsSearch": null, "ExtraHosts": null, "Links": [ "/docker_db_1:/docker_webfrontend_run_34/db", "/docker_db_1:/docker_webfrontend_run_34/db_1", "/docker_db_1:/docker_webfrontend_run_34/docker_db_1" ], "LxcConf": null, "NetworkMode": "bridge", "PortBindings": null, "Privileged": false, "PublishAllPorts": false, "RestartPolicy": { "MaximumRetryCount": 0, "Name": "" }, "SecurityOpt": null, "VolumesFrom": [] }, 
+7
docker docker-compose fig
source share
2 answers

This is intentional behavior for fig run .

Run the one-time command in the service.

One-time commands run in new containers with the same configuration as the regular container for this service, so volumes, links, etc. will be created as expected. The only thing that differs from a regular container is that the command will be overridden by the one specified, and ports will not be created if they collide .

source .

fig up is probably the command you are looking for, it (re) will create all containers based on your fig.yml and run them.

+4
source share

This is the intentional behavior for docker-compose run , as per the documentation :

When using run there are two differences from creating a container:

  • ...

  • ports will not be created by default if they encounter ports that are already open.

One way to overcome this is to use up instead of run , which:

Creates, (re) creates, starts, and attaches to containers for the service.

Another way if you are using version 1.1.0 or later is to pass the --service-ports parameter:

Run the command with the service ports enabled and map to the host.

PS I tried to edit the original answer, got a deviation, twice. Stay cool, SO.

+6
source share

All Articles