Docker Notes – Making docker-compose commands work in any path

Time:2022-9-22

The command docker-compose to maintain the docker harbor warehouse has a strange (very) strange (low) default setting, which isIt must be executed in the directory where docker-compose.yml (or any one of the four files docker-compose.yml, docker-compose.yaml, compose.yml, and compose.yaml) is located to be valid.

Execute the docker-compose command in any path, and the result may be:

[[email protected] ~]# docker-compose ps
ERROR: 
        Can't find a suitable configuration file in this directory or any
        parent. Are you in the right directory?

        Supported filenames: docker-compose.yml, docker-compose.yaml, compose.yml, compose.yaml

If the location of docker-compose.yml is:

/data/soft/harbor/docker-compose.yml

At this point, it is normal to enter the directory where the yml file is located, and then execute the docker-compose command:

[[email protected] ~]# cd /data/soft/harbor
[[email protected] harbor]# docker-compose ps
      Name                     Command                       State                     Ports          
------------------------------------------------------------------------------------------------------
harbor-core         /harbor/harbor_core              Up (health: starting)                            
harbor-db           /docker-entrypoint.sh            Exit 255                                         
harbor-jobservice   /harbor/harbor_jobservice  ...   Exit 255                                         
harbor-log          /bin/sh -c /usr/local/bin/ ...   Up (healthy)            127.0.0.1:1514->10514/tcp
harbor-portal       nginx -g daemon off;             Exit 255                                         
nginx               nginx -g daemon off;             Restarting                                       
redis               redis-server /etc/redis.conf     Exit 255                                         
registry            /home/harbor/entrypoint.sh       Up (healthy)            5000/tcp                 
registryctl         /home/harbor/start.sh            Exit 255

Regarding this default setting, there is currently no clear explanation or related information. Guessing may be due to two reasons, one is that this thing is easy to optimize, and the other is that there are not many people using docker harbor, and the official has no motivation to optimize it.

Whatever the reason, it's impossible to frequently jump between directories in order to execute a command in daily use. In order to facilitate maintenance and improve efficiency, docker-compose commands need to be executed in any path.

This article provides the easiest way:By setting the alias alias (environment variable), the docker-compose command can be executed in any path.

The alias command in Linux is used to set the alias of the instruction, usually used to simplify some longer commands. Its syntax format is:

alias [alias]='original command-options/parameters'

E.g:

alias ll='ls -lt'

Then the setting method to make the docker-compose command available in any path is as follows:
vim ~/.bashrc, add a line:

alias docker-compose='cd /data/soft/harbor;docker-compose'

Save and exit, execute:

source ~/.bashrc

View the list of aliases that have been set, and you can see that docker-compose has been set:

[[email protected] harbor]# alias -p
alias cp='cp -i'
alias docker-compose='cd /data/soft/harbor;docker-compose'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

Verification: Execute the docker-compose command in any path and find that it can be executed normally:

[[email protected] harbor]# cd /data/
[[email protected] data]# docker-compose ps
      Name                     Command                       State                     Ports          
------------------------------------------------------------------------------------------------------
harbor-core         /harbor/harbor_core              Up (health: starting)                            
harbor-db           /docker-entrypoint.sh            Exit 255                                         
harbor-jobservice   /harbor/harbor_jobservice  ...   Exit 255                                         
harbor-log          /bin/sh -c /usr/local/bin/ ...   Up (healthy)            127.0.0.1:1514->10514/tcp
harbor-portal       nginx -g daemon off;             Exit 255                                         
nginx               nginx -g daemon off;             Restarting                                       
redis               redis-server /etc/redis.conf     Exit 255                                         
registry            /home/harbor/entrypoint.sh       Up (healthy)            5000/tcp                 
registryctl         /home/harbor/start.sh            Exit 255

other instructions:
If it does not work after executing source ~/.bashrc, it may be because the ~/.bash_profile file lacks the configuration of ~/.bashrc. Add the following configuration to the ~/.bash_profile file and execute source ~/.bash_profile ( Usually do not need to be added, it exists by default. However, Linux systems that have been used for a long time may cause abnormal environment variables or loss of configuration because administrators often change environment variables):

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

Recommended Today

Community project sharing | Build a movie recommendation system with Jina

We are in contact with recommendation systems every day, short videos, e-commerce, takeaways, performances, advertisements… Today, we will demonstrate the project of Jina AI community user Achintya,He used Jina to build a movie recommendation system. An overview of the principle of movie recommendation system In this demo, the authors transform movie recommendation into a text […]