Documentation: docker.running "port_bindings" vs "ports"
Pre salt 2015.2.0, the docker.running state required that port_bindings be used to define port mappings for docker. This was confusing because the documentation referenced ports and port_bindings (but ports in more places than port_bindings) and showed examples of both a dictionary and an ordered dictionary in the examples. See #21572 for references on the confusion.
After salt 2015.2.0 port_bindings are now ignored and ports must be used instead. This is a much nicer key name to use, but the documentation still shows two different keys and two different dictionary formats. This, coupled with the many references to port_bindings you get with a quick google, makes things really confusing.
It would be great if the documentation was made consistent to just say ports.
P.S. While the change makes sense, the lack of backwards compatibility for those of us with 20+ docker states in different formulas is kinda rough. A warning message of future deprecation, with continued support, would really be appreciated for things like this.
@tkent-xetus, thanks for the report. We have a deprecation mechanism, which these changes you have mentioned should have been using. I'm not sure how they got past review, but we should definitely update the documentation.
@jfindlay thanks for the quick response, appreciate!
got hit by this one
I'm not entirely sure what the correct solution is, here.
My state looks like this:
waynetest:
docker.running:
- container: waynetest
- image: "waynew/py34"
- ports:
- "8000/tcp":
HostIp: "127.0.0.1"
HostPort: "5454"
- publish_all_ports: True
- volumes:
- /var/www/test:
bind: /www
ro: True
- command: -m www.server
running 2015.5.3 and 2015.5.4 on my master & minion respectively, with docker-py==1.0.0 (not sure what the max docker-py version is I can run before the API breaks the docker.running state).
The problem is that the container is not created with the ports parameter, ports=[1111,2222] in their example:
container_id = c.create_container(
'busybox', 'ls', ports=[1111, 2222],
host_config=docker.utils.create_host_config(port_bindings={
1111: 4567,
2222: None
})
)
What I was able to do is hack /usr/local/lib/python2.7/dist-packages/salt/states/dockerio.py and add the last line here in def running:
if isinstance(ports, list):
for port in ports:
if isinstance(port, dict):
container_port = list(port.keys())[0]
# Doubtless there is a better way to do this. But it worked for me
exposeports.append(container_port.split('/')[0])
This pulls out the "8000" part of 8000/tcp and adds that to the exposeports which is used when creating the container, and then the bindings work.
@waynew Any chance you can use the docker.installed state to setup a new container of the same image? It seems like you already know the main cause, being that the docker container wasn't installed exposing the ports you want to bind to.
When a docker image we're using fails to expose some port we'd like to bind to, we usually get around the issue by using the docker.installed state's ports parameter to override the dockerfile directives. This works like a charm without needing to add a monkeypatch into dockerio.py.
At any rate, it's a frustrating problem I've seen many times, but it's not quite this issue. This issue is just about the mis-matched documentation - what you're running into is a whole different animal.
On a final note, if you can't use the docker.installed state, we have an example of a monkeypatch we've used for the dockerio module. Monkeypatches are ugly, but at least you'll have a working example of one you can distribute through your environment.
Another thing to consider is that in 2015.8, the dockerio exec and state module have been deprecated by the dockerng exec and state module, which is a rewrite of salt's docker support.
@tkent just tried that... but none of the variations actually worked. Got a "bad status line" exception raised when requests tried to poll docker for information about the container.
@jfindlay I may try moving to that again today, though there's a bug somewhere in the master that pegs out my CPU polling a socket. I think I filed a bug on that.
@tkent docker.installed and docker.running dosn't make a difference as i had to figure out in https://github.com/saltstack/salt/issues/21572#issuecomment-161691558 - ports are only published if they are mentioned within the Dockerfile as EXPOSE
@Deshke That may be true in the version of Docker you're running, but it's not the case with at least Docker 1.5.x or 1.6.1 (and I suspect many others).
We actually make extensive use of the ability to expose ports NOT specified by an EXPOSE statement in the Docker build file. By way of example, all of our HAProxy containers and Jenkins containers use the technique I mentioned to @waynew.
One thing that may have stopped you up is that the docker.installed state doesn't do anything if the container has already been created. That means that you have to remove the container you're working on before the docker.installed state is ran to see any changes.
below my docker state. as you can see i do remove it before i start another :) and it don't matter if i run docker.running or docker.installed it ends up on the same side.
btw. i'm currently running docker 1.9.1 with docker-py 1.6.0 (what is as of 2015-12-04 the current version https://github.com/docker/docker-py/releases)
# mem_limit: !!null // until https://github.com/saltstack/salt/issues/25492 is fixed
# docker images -q |xargs docker rmi:
# cmd.run:
# - order: 0
stop-old-container:
docker.absent:
- name: vimage
{% if grains['environment'] == 'live' %}
vimage-live-image:
docker.pulled:
- tag: 11c1913-1510281533
- name: registry_host.tld:5043/user/vimage_live
{% elif grains['environment'] == 'preview' %}
vimage-preview-image:
docker.pulled:
- tag: 11c1913-1510290916
- name: registry_host.tld:5043/user/vimage_preview
{% elif grains['environment'] == 'stage' %}
{% elif grains['environment'] == 'dev' %}
{% endif %}
vimage-preview-container:
docker.running:
- container: vimage
{% if grains['environment'] == 'live' %}
- image: registry_host.tld:5043/user/vimage_live:11c1913-1510281533
{% elif grains['environment'] == 'preview' %}
- image: registry_host.tld:5043/user/vimage_preview:11c1913-1510290916
{% elif grains['environment'] == 'stage' %}
{% elif grains['environment'] == 'dev' %}
{% endif %}
- mem_limit: !!null
- restart_policy:
MaximumRetryCount: 3,
Name: always
- ports:
- "80/tcp":
HostIp: ""
HostPort: "80"
- "1935/tcp":
HostIp: ""
HostPort: "1935"
- "443/tcp":
HostIp: ""
HostPort: "443"
- publish_all_ports: true
@tkent docker EXPOSE is used to LINK containers eg. mysql to php to nginx NOT for publishing ports to use from external services/clients
from http://docs.docker.com/engine/reference/builder/#expose
The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. EXPOSE does not make the ports of the container accessible to the host. To do that, you must use either the -p flag to publish a range of ports or the -P flag to publish all of the exposed ports. You can expose one port number and publish it externally under another number.
To set up port redirection on the host system, see using the -P flag. The Docker network feature supports creating networks without the need to expose ports within the network, for detailed information see the overview of this feature).
so - publish_all_ports: true is only working becouse docker-py handles it as "-P" (publish all exposed ports)
@Deshke The state SLS you showed is not using the docker.installed state, which is almost certainly why you're not getting the results you're expecting. I also think this is the heart of the confusion here.
The ports that a docker.running state can configure for forwarding are fixed on container creation. And, since you're not using the docker.installed state, you're not getting the chance to specify ports not exposed by default (that whole EXPOSE thing plays in here, use docker's inspect for some more insight there).
I think a working example would be best here. This SLS will expose ports in the way I described above. Just do be sure to remove the container manually before running the state (or modify the state to do the removal first, either way). Best of luck, and hopefully you can avoid using publish_all_ports unnecessarily!
{% set image_name = "jenkins" %}
{% set container_name = "jenkins" %}
{% set data_dir = salt['pillar.get']('jenkins:data_dir', '/jenkins-data') %}
{% set bind_ip = salt['pillar.get']('jenkins:server_ip') %}
{% set portMap = { "443": "8443/tcp" } %}
{% set volumeMap = { "/var/jenkins_home" : data_dir } %}
#
# Pull/install the image from the repository
#
{{ image_name }}-pulled:
docker.pulled:
- name: {{ image_name }}
{{ image_name }}-installed:
require:
- docker: {{ image_name }}-pulled
docker.installed:
- name: {{ container_name }}
- image: {{ image_name }}
- hostname: {{ salt['pillar.get']('jenkins:fqdn') }}
- volumes:
{% for cPath, hPath in volumeMap.items() %}
- "{{ hPath }}:{{cPath}}"
{% endfor %}
- ports:
- "8443/tcp"
- environment:
JAVA_TOOL_OPTIONS: "-Dfile.encoding=UTF-8"
JENKINS_OPTS: >
--httpsPort=8443
--httpsKeyStore=/var/jenkins_home/ssl/keystore
--httpsKeyStorePassword={{ salt['pillar.get']('jenkins:keystore_pass') }}
JAVA_OPTS: >
-Djavax.net.ssl.trustStore=/var/jenkins_home/ssl/keystore
-Djavax.net.ssl.trustStorePassword={{ salt['pillar.get']('jenkins:keystore_pass') }}
#
# Copy the keystore from the pillar and set the appriopriate permissions
#
{{ data_dir }}/ssl/:
file.directory:
- mode: 700
- makedirs: True
{{ data_dir }}/ssl/keystore.base64:
require:
- file: {{ data_dir }}/ssl
file.managed:
- mode: 600
- contents_pillar: jenkins:keystore_base64
{{ data_dir }}/ssl/keystore:
require:
- file: {{ data_dir }}/ssl/keystore.base64
cmd.wait:
- name: base64 -d {{ data_dir }}/ssl/keystore.base64 > {{ data_dir }}/ssl/keystore
- watch:
- file: {{ data_dir }}/ssl/keystore.base64
file.managed:
- replace: False
- mode: 600
{{ data_dir }}-ownership:
require:
- file: {{ data_dir }}/ssl/keystore
cmd.run:
- name: chown -R 1000:1000 "{{ data_dir }}"
{{ image_name }}-running:
require:
- docker: {{ image_name }}-installed
docker.running:
- container: {{ container_name }}
- image: {{ image_name }}
- volumes:
{% for cPath, hPath in volumeMap.items() %}
{{ hPath }}:
bind: {{ cPath }}
ro: False
{% endfor %}
- ports:
{% for hport, cport in portMap.items() %}
{{ cport }}:
HostIp: {{ bind_ip }}
HostPort: {{hport}}
{% endfor %}
#
# Install the MySQL client so CI shell scripts that use MySQL are possible
# Note - amazingly enough, nsenter runs commands as root!
#
mysql-client-installed:
require:
- docker: {{ image_name }}-running
docker.run:
- cid: {{ container_name }}
- name: >
bash -c 'apt-get update && apt-get install -y mysql-client-5.5'
- unless: >
docker exec {{ container_name }} dpkg -l | grep -q "mysql-client-5.5"
@jfindlay @rallytime
Like #23610, I think we close this. The module and docs are all different now.