Home > Software engineering >  How to add symbolic link or alias to dockerfile?
How to add symbolic link or alias to dockerfile?

Time:01-22

I have the following dockerfile:

FROM python:3.10-alpine

LABEL Name=my_app

WORKDIR /app

RUN addgroup --gid 1000 -S my_app && \
    adduser --uid 1000 -D -S my_app -G my_app -s /sbin/nologin

COPY --chown=1000:1000 pyproject.toml README.rst src ./

RUN apk add --no-cache --virtual=.build-deps build-base libffi-dev curl openssl-dev && \
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
    source $HOME/.cargo/env && \
    pip install --upgrade pip && pip install --no-cache-dir ./ && \
    apk del .build-deps && \
    rustup self uninstall -y

RUN chown -R 1000:1000 /app

USER my_app

It is working and I am capable to run inside of this container the following command: my_app run --checks all path_name. Because some reason I need to change my_app key phrase to my_app_2 key phrase. So I would able to run the following command: my_app_2 run --checks all path_name. I cannot change underling files outside of dockerfile because of some server configuration. What changes in dockerfile could I make?

  1. I tried to add:

     RUN ln -s /usr/local/bin/my_app /usr/local/bin/my_app_2
    

But getting an error during build prosses::

     => ERROR [7/7] RUN ln -s /usr/local/bin/my_app /usr/local/bin/my_app                                                                                                                             0.3s
    ------
     > [7/7] RUN ln -s /usr/local/bin/my_app /usr/local/bin/my_app_2:
    #0 0.256 ln: /usr/local/bin/my_app_2: Permission denied
    ------
    failed to solve: executor failed running [/bin/sh -c ln -s /usr/local/bin/my_app /usr/local/bin/my_app_2]: exit code: 1
  1. Also I tried to swap my_app_2 and my_app:

     RUN ln -s /usr/local/bin/my_app_2 /usr/local/bin/my_app
    

But getting another error during build prosses:

 => ERROR [7/7] RUN ln -s /usr/local/bin/my_app_2 /usr/local/bin/my_app                                                                                                                             0.5s
------
 > [7/7] RUN ln -s /usr/local/bin/my_app_2 /usr/local/bin/my_app:
#0 0.516 ln: /usr/local/bin/my_app: File exists
------
failed to solve: executor failed running [/bin/sh -c ln -s /usr/local/bin/my_app_2 /usr/local/bin/my_app]: exit code: 1 
  1. Third attempt. I added:

    RUN echo "alias my_app_2='my_app'" >> ~/.bashrc

Built was successful but when I run

my_app_2 run --checks all my_path_here

I am getting:

sh: my_app_2: not found

CodePudding user response:

Run the command while you are still root. Unprivileged users can't modify arbitrary files on the filesystem:

FROM python:3.10-alpine

LABEL Name=my_app

WORKDIR /app

RUN addgroup --gid 1000 -S my_app && \
    adduser --uid 1000 -D -S my_app -G my_app -s /sbin/nologin

COPY --chown=1000:1000 pyproject.toml README.rst src ./

RUN apk add --no-cache --virtual=.build-deps build-base libffi-dev curl openssl-dev && \
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
    source $HOME/.cargo/env && \
    pip install --upgrade pip && pip install --no-cache-dir ./ && \
    apk del .build-deps && \
    rustup self uninstall -y

# run other steps as root here
RUN ln -s /usr/local/bin/my_app /usr/local/bin/my_app_2

RUN chown -R 1000:1000 /app

USER my_app
# you are no longer root here
  • Related