docker compose not recognized when using sudo - docker

My OS is Ubuntu 20.04.3 LTS.
I've installed docker compose V2, and I can access it from the command line regularly:
$ docker compose version
Docker Compose version v2.2.2
I've also installed compose-switch according to the manual instructions here: https://docs.docker.com/compose/cli-command/#compose-switch and it's working fine:
$ docker-compose version
Docker Compose version v2.2.2
But if I use sudo neither will work:
$ sudo docker compose version
docker: 'compose' is not a docker command.
See 'docker --help'
$ sudo docker-compose version
docker: 'compose' is not a docker command.
See 'docker --help'
docker version is the same with or without sudo:
Version: 20.10.12
API version: 1.41
So, how can I get docker compose working with sudo?

I had installed docker-compose under my user's home directory. I had to move the file docker-compose from ~/.docker/cli-plugins to /usr/local/lib/docker/cli-plugins
$ sudo mkdir -p /usr/local/lib/docker/cli-plugins
$ sudo mv /home/<username>/.docker/cli-plugins/docker-compose /usr/local/lib/docker/cli-plugins/docker-compose
And now everything works as expected.

The docker command you are running as your local user must be calling a different binary than what it calls when running as another user (i.e. root user).
When you invoke a command using sudo, it will by default use the root user shell environment which includes the PATH env variable.
I suspect you will see a different path output when running these two commands:
type docker
sudo type docker

Related

bash docker command not found in centos7 after installing docker

I installed docker in centos7 and started docker service.
I checked docker service it is running. But when I run docker --version getting error like bash docker command not found. Why?
Add "C:\Program Files\Docker Toolbox" into my path environment

Docker issues on Mac

I installed docker using HomeBrew on Mac.
➜ mattermost-server git:(master) docker --version
Docker version 18.09.1, build 4c52b90
➜ mattermost-server git:(master) which docker
/usr/local/bin/docker
When I run docker,
This is the error I get.
➜ mattermost-server git:(master) docker ps
Cannot connect to the Docker daemon at **unix:///var/run/docker.sock.
Is the docker daemon running?**
Update: This can be solved by removing existing docker and running
brew install cask docker
Refer here Cannot connect to the Docker daemon on macOS
I had the same problem after install docker on my mac (brew cask install docker).
docker --version works, but docker ps or any other docker command results in the error:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
To solve the problem you have to :
Install Virtual Box
run : docker-machine create default to create a virtual machine (mandatory
on mac os)
run: docker-machine env default to set the environment
run: eval $(docker-machine env default)
try docker ps or docker version to check that everything is ready.
You are possibly running docker without sudo user, aren't you?
By default you should run docker with sudo user, if you don't want to do that, folow Manage Docker as a non-root user
We need to run brew cask install docker. This should fix the problem.

docker-compose is not working

When I am trying to test a zookeeper cluster using docker-compose, the console shows nothing.
This is my command:
COMPOSE_PROJECT_NAME=zk_test docker-compose up
No response of docker-compose command
My questions are:
I only installed docker-machine and docker-compose. The command is there, but do I need to install docker-compose additionally?
Should docker-compose be installed inside docker machine or in host machin? If the latter, how does docker-compose manage Docker containers? Because they are running inside of the Docker machine.
What I think is weird, is that when I input 'docker-compose' in the host terminal, it can be recognized by the OS (screenshot).
My environment:
OS: Mac
Docker container: docker-machine (boot2docker)
Here is how I get all necessary packages on my Mac OS machine.
Install homebrew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then brew cask install docker
brew cask install docker
Open Docker through Applications or:
open /Applications/Docker.app
You should now how docker-compose, docker and docker-engine.

Must I provide a command when running a docker container?

I'd like to install mysql server on a centos:6.6 container.
However, when I run docker run --name myDB -e MYSQL_ROOT_PASSWORD=my-secret-pw -d centos:6.6, I got docker: Error response from daemon: No command specified. error.
Checking the document from docker run --help, I found that the COMMAND seems to be an optional argument when executing docker run. This is because [COMMAND] is placed inside a pair of square brackets.
$ docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
I also find out that the official repository of mysql doesn't specify a command when starting a MySQL container:
Starting a MySQL instance is simple:
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
Why should I provide a command when running a centos:6.6 container, but not so when running a mysql container?
I'm guessing that maybe centos:6.6 is specially-configured so that the user must provide a command when running it.
if you use centos:6.6, you do need to provide a command when you issue "docker run" command.
The reason the officical repository of mysql does not specify a command is because it has CMD command in it's docker file: CMD ["mysqld"]. Check it's docker file here.
The CMD in docker file is the default command when run the container without a command.
You can read here to better understand what you can use in a docker file.
In your case, you can
Start your centos 6.6 container
Take official mysql docker file as reference, issue similar command (change apt-get to yum ( or sudo yum if you don't use the default root user)
Once you can successfully start mysql, you can put all your command in your docker file, just to make sure the first line is "From centos:6.6"
Build your image
Run a container with your image, then you don't need to provide a command in docker run
You can share your docker file in docker hub, so that other people can user yours.
good luck.

docker error: /var/run/docker.sock: no such file or directory

I am new to docker. I have a shell script that loads data into impala and I want a docker file that runs builds an image and run the container.
I am on mac, installed boot2docker and have the DOCKER_HOST env set up.
bash-3.2$ docker info
Containers: 0
Images: 0
Storage Driver: aufs
Root Dir: /mnt/sda1/var/lib/docker/aufs
Dirs: 0
Execution Driver: native-0.2
Kernel Version: 3.15.3-tinycore64
Debug mode (server): true
Debug mode (client): false
Fds: 10
Goroutines: 10
EventsListeners: 0
Init Path: /usr/local/bin/docker
Sockets: [unix:///var/run/docker.sock tcp://0.0.0.0:2375]
I am trying to just installed a pre-built image using:
sudo docker pull busybox
I get this error:
sudo docker pull busybox
2014/08/18 17:56:19 Post http:///var/run/docker.sock/images/create?fromImage=busybox&tag=: dial unix /var/run/docker.sock: no such file or directory
Is something wrong with my docker setup?
When I do a docker pull busybox, It pulls the image and download is complete.
bash-3.2$ docker pull busybox
Pulling repository busybox
a9eb17255234: Download complete
fd5373b3d938: Download complete
d200959a3e91: Download complete
37fca75d01ff: Download complete
511136ea3c5a: Download complete
42eed7f1bf2a: Download complete
c120b7cab0b0: Download complete
f06b02872d52: Download complete
120e218dd395: Download complete
1f5049b3536e: Download complete
bash-3.2$ docker run busybox /bin/echo Hello Doctor
Hello Doctor
Am I missing something?
You don't need to run any docker commands as sudo when you're using boot2docker as every command passed into the boot2docker VM runs as root by default.
You're seeing the error when you're running as sudo because sudo doesn't have the DOCKER_HOST env set, only your user does.
You can confirm this by doing a:
$ env
Then a
$ sudo env
And looking for DOCKER_HOST in each output.
As for having a docker file that runs your script, something like this might work for you:
Dockerfile
FROM busybox
# Copy your script into the docker image
ADD /path/to/your/script.sh /usr/local/bin/script.sh
# Run your script
CMD /usr/local/bin/script.sh
Then you can run:
docker build -t your-image-name:your-tag .
This will build your docker image, which you can see by doing a:
docker images
Then, to run your container, you can do a:
docker run your-image-name:your-tag
This run command will start a container from the image you created with your Dockerfile and your build command and then it will finish once your script.sh has finished executing.
You can quickly setup your environment using shellinit
At your command prompt execute:
$(boot2docker shellinit)
That will populate and export the environment variables and initialize other features.
docker pull will fail if docker service is not running. Make sure it is running by
:~$ ps aux | grep docker
root 18745 1.7 0.9 284104 13976 ? Ssl 21:19 0:01 /usr/bin/docker -d
If it is not running, you can start it by
sudo service docker start
For Ubuntu 15 and above use
sudo systemctl start docker
On my MAC when I start boot2docker-vm on the terminal using
boot2docker start
I see the following
To connect the Docker client to the Docker daemon, please set:
export DOCKER_CERT_PATH=<my things>
export DOCKER_TLS_VERIFY=1
export DOCKER_HOST=tcp://<ip>:2376
After setting these environment variables I was able to run the build without the problem.
Update [2016-04-28] If you are using a the recent versions of docker you can do
eval $(docker-machine env) will set the environment
(docker-machine env will print the export statements)
I also got this error. Though, I did not use boot2docker but just installed "plain" docker on Ubuntu (see https://docs.docker.com/installation/ubuntulinux/).
I got the error ("dial unix /var/run/docker.sock: no such file or directory. Are you trying to connect to a TLS-enabled daemon without TLS?") because the docker daemon was not running, yet.
On Ubuntu, you need to start the service:
sudo service docker start
See also http://blog.arungupta.me/resolve-dial-unix-docker-sock-error-techtip64
For boot2docker on Windows, after seeing:
FATA[0000] Get http:///var/run/docker.sock/v1.18/version:
dial unix /var/run/docker.sock: no such file or directory.
Are you trying to connect to a TLS-enabled daemon without TLS?
All I did was:
boot2docker start
boot2docker shellinit
That generated:
export DOCKER_CERT_PATH=C:\Users\vonc\.boot2docker\certs\boot2docker-vm
export DOCKER_TLS_VERIFY=1
export DOCKER_HOST=tcp://192.168.59.103:2376
Finally:
boot2docker ssh
And docker works again
In Linux, first of all execute sudo service docker start in terminal.
If you're using CentOS 7, and you've installed Docker via yum, don't forget to run:
$ sudo systemctl start docker
$ sudo systemctl enable docker
This will start the server, as well as re-start it automatically on boot.
To setup your environment and to keep it for the future sessions you can do:
echo 'export DOCKER_HOST="tcp://$(boot2docker ip 2>/dev/null):2375";' >> ~/.bashrc
Then:
source ~/.bashrc
And your environment will be setup in every session
The first /var/run/docker.sock refers to the same path in your boot2docker virtual machine. Correcly write for windows /var/run/docker.sock
You, maybe the not the OP, but someone may have a directory called /var/run/docker.sock/ already due to how many times you hack and slash to get things right with docker (especially noobs). Delete that directory and try again.
This helped me on my way to getting it to work on Centos 7.
I have installed the docker using offline method and post server restart docker is not running.
So, I executed the below command it worked for me!
/usr/bin/dockerd > /dev/null
run the following commands, OS = CentOS / RHLE / Amazon Linux, etc.
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
chmod 777 /var/run/docker.sock

Resources