How to set bash aliases for docker containers in Dockerfile?

I am new to docker. I found that we can set environment variables using ENV instruction in the Dockerfile. But how does one set bash aliases for long commands in Dockerfile?

  • Connect to mongodb on centos7 docker image
  • File is not created in Docker
  • Determining if a process runs inside lxc/Docker
  • Run docker and keep zshhistory from host (zsh shell) in docker container (bash shell)
  • Make docker container run forever while being able to gracefully stop
  • Testing ASP.NET Core Docker Container with Travis CI
  • Vagrant install docker with puppet
  • Django : Getting error while removing django.contrib.sites from INSTALLED_APPS
  • starting docker container from python messes up terminal settings
  • Dockerize Applications or Machines?
  • docker fails to mount from CIFS mounted host directory
  • Mount a windows host directory in compose file version 3
  • 2 Solutions collect form web for “How to set bash aliases for docker containers in Dockerfile?”

    Basically like you always do, by adding it to the users .bashrc:

    FROM foo
    RUN echo 'alias hi="echo hello"' >> ~/.bashrc

    As usual this will only work for interactive shells:

    docker build -t test .
    docker run -it --rm --entrypoint /bin/bash test hi
    /bin/bash: hi: No such file or directory
    docker run -it --rm test bash
    $ hi

    For non-interactive shells you should create a small script and put it in your path, i.e.:

    echo '#!/bin/bash\necho hello' > /usr/bin/hi
    chmod +x /usr/bin/hi

    You can use entrypoint, but it will not work for alias, in your Dockerfile:

    ADD dev/ /opt/
    ENTRYPOINT ["/opt/"]


    set -e
    function dev_run()
    export -f dev_run
    exec "$@"

    (Quick copy/paste, sorry)

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.