Home > database >  Docker: how to use entrypoint CMD together
Docker: how to use entrypoint CMD together

Time:04-23

I know the difference between ENTRYPOINT and CMD but cannot solve myself my issue.

This is my Dockerfile for use ansible without install it.

FROM python:3.10.4-slim-buster
# Update and upgrade
RUN apt-get update -y && apt-get upgrade -y
# Install requirements
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends openssh-client sshpass
RUN pip install pip --upgrade
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY entrypoint.sh /entrypoint.sh
RUN chmod a x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
CMD ["--version"]

And this is the entrypoint.sh

#!/usr/bin/env sh
set -e

cp -pr /ssh /root/.ssh
chown -R root:root /root/.ssh/config
ansible-playbook

So, I'm expecting that launching my Docker with

docker run \
    --rm -it \
    -v $(TOPDIR)/playbook:/playbook:ro \
    -v ~/.ssh:/ssh:ro \
    sineverba/ansible:latest

(so, without arguments or CMDs) I get the version of ansible in console. But nothing is returned or printed.

Neither if I add the right usage, that I thought it overwrites the CMD instruction

docker run \
    --rm -it \
    -v $(TOPDIR)/playbook:/playbook:ro \
    -v ~/.ssh:/ssh:ro \
    --name $(CONTAINER_NAME) \
    $(IMAGE_NAME):$(VERSION) \
    -i /playbook/inventory.yml /playbook/playbook.yml

But, If I remove the entrypoint and re-build with

FROM python:3.10.4-slim-buster
# Update and upgrade
RUN apt-get update -y && apt-get upgrade -y
# Install requirements
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends openssh-client sshpass
RUN pip install pip --upgrade
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY entrypoint.sh /entrypoint.sh
RUN chmod a x /entrypoint.sh
ENTRYPOINT ["ansible-playbook"]
CMD ["--version"]

I get version in console / I can use ansible (well, no, 'because I need to change owner of SSH keys) but, apart of ssh trouble, I get my working docker.

So, how can I replace the entrypoint ansible-playbook with the sh entrypoint?

CodePudding user response:

Your script doesn't do anything with any parameters it might get. You need to add the parameters to the ansible-playbook command in the script, like this

#!/usr/bin/env sh
set -e

cp -pr /ssh /root/.ssh
chown -R root:root /root/.ssh/config
ansible-playbook $@
  • Related