Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement --publish in docker-compose run #916

Merged
merged 1 commit into from
Apr 28, 2024

Conversation

beledouxdenis
Copy link
Contributor

Documentation

docker-compose run options:
https://docs.docker.com/reference/cli/docker/compose/run/#options

-p, --publish Publish a container's port(s) to the host

Explanations

Using the below docker-compose.yml:

version: '3.8'

services:
  netcat-server:
    image: alpine:latest
    command: sh -c "apk add --no-cache netcat-openbsd && nc -lk -p 12345"

The below docker-compose run command:

docker-compose run -p 12345:12345 netcat-server

publishes the port 12345 from the container to the host:

$ docker ps -a
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                           NAMES
0adac614c107   alpine:latest   "sh -c 'apk add --no…"   29 seconds ago   Up 29 seconds   0.0.0.0:12345->12345/tcp, :::12345->12345/tcp   src-netcat-server-run-79dad3a4579c

while the same using podman-compose run:

podman-compose run -p 12345:12345 netcat-server

does not publish the port:

$ podman ps -a
CONTAINER ID  IMAGE                            COMMAND               CREATED        STATUS        PORTS       NAMES
acc4cd67b10a  docker.io/library/alpine:latest  sh -c apk add --n...  7 seconds ago  Up 8 seconds              src_netcat-server_tmp59130

The changes in this pull request aims to add the implementation of the --publish options in podman-compose run, which was completely forgotten. --publish was added to the parser arguments:

parser.add_argument(
"-p",
"--publish",
action="append",
help="Publish a container's port(s) to the host (can be used multiple times)",
)

but then never used later (no occurences of args.publish).

These changes allow the expected behavior:

$ podman ps -a
CONTAINER ID  IMAGE                            COMMAND               CREATED       STATUS       PORTS                     NAMES
60e58363e0c0  docker.io/library/alpine:latest  sh -c apk add --n...  1 second ago  Up 1 second  0.0.0.0:12345->12345/tcp  src_netcat-server_tmp20781

The new block if args.publish: is stategically put after the block if not args.service_ports:, which is emptying cnt['ports'] when --service-ports is not passed.
Otherwise the ports added by --publish would be immediately emptied when --service-ports is not passed, which is not what is expected :D.

@beledouxdenis beledouxdenis force-pushed the main-run-implement-publish branch from 186bd4f to 72a94d5 Compare April 27, 2024 22:54
Copy link
Collaborator

@p12tic p12tic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@p12tic p12tic merged commit 12d46ca into containers:main Apr 28, 2024
8 checks passed
@beledouxdenis
Copy link
Contributor Author

Thanks to you !

beledouxdenis added a commit to beledouxdenis/docker-odoo-ready that referenced this pull request Oct 27, 2024
To be able to forward the ports from the container to the host
e.g. using `-p 8069:8069`
There is a need to use a version of podman-compose including
containers/podman-compose#916
The first version available with this revision is 1.2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants