docker-compose not found under WSL 2 - docker

I've got Windows system with Alpine WSL2 integration, and cannot run docker-compose under it. Here is log of my actions in WSL2:
ZHOPOZAVR:~# docker -v
Docker version 20.10.16, build aa7e414fdcb23a66e8fabbef0a560ef1769eace5
ZHOPOZAVR:~# docker-compose -v
/mnt/c/Program Files/Docker/Docker/resources/bin/docker-compose: line 13: /usr/bin/docker-compose: not found
ZHOPOZAVR:~# ls -al /usr/bin | grep docker-compose
lrwxrwxrwx 1 root root 56 Jan 13 05:41 docker-compose -> /mnt/wsl/docker-desktop/cli-tools/usr/bin/docker-compose
lrwxrwxrwx 1 root root 59 Jan 13 05:41 docker-compose-v1 -> /mnt/wsl/docker-desktop/cli-tools/usr/bin/docker-compose-v1
ZHOPOZAVR:~# /usr/bin/docker-compose -v
-ash: /usr/bin/docker-compose: not found
WSL2 integration is enabled in my Docker Desktop:

Turned out that somehow Apline WSL doesn't work with docker-compose. Ubuntu works just fine.

Related

Symlink to socket file when running docker container doesn't work

It seems that symlinks to socket is not correctly handled by docker run.
The problem:
I'm trying to use rootless docker. The socket to connect to docker is
/run/user/1000/docker.sock.
But I need to use a docker-compose file where a traefik container is run with a volume mapping to give access to /var/run/docker.sock from inside the container. I can't modify this docker-compose file.
To make it work, I tried to do a symlink /var/run/docker.sock > /run/user/1000/docker.socket. It works fine on my host machine, I can talk to the rootless docker using this socket but when running the container, the /var/run/docker.sock file is not a socket but a directory.
How to reproduce:
To reproduce the problem with docker run:
sudo ln -s /run/user/1000/docker.sock /var/run/docker.sock
sudo docker run -v /run/user/1000/docker.sock:/var/run/docker-user.sock -v /var/run/docker.sock:/var/run/docker.sock -it traefik /bin/sh
Inside the traefik container:
/ # ls -al /var/run/
total 0
drwxr-xr-x 1 root root 54 Dec 13 17:23 .
drwxr-xr-x 1 root root 134 Dec 13 17:23 ..
srw-rw---T 1 root 974 0 Dec 13 15:06 docker-user.sock
drwxr-xr-x 2 root root 40 Dec 13 15:09 docker.sock
Thanks for the help :)

Where do I place a docker config.json file when docker is installed as a snap on Ubuntu?

I want to use a custom docker config.json file like this to reassign the detach keystrokes:
{
"detachKeys": "ctrl-q,ctrl-q"
}
In a "normal" docker world, i.e. one where docker is installed via apt or similar and not snap, I could put this file in $HOME/.docker/config.json and the setting is picked up when I next run the docker command. However, this file is not recognized when running /snap/bin/docker. docker just silently ignores it.
If I try to force it to use this directory, I am denied:
$ docker --config .docker/ run -it ubuntu /bin/bash
WARNING: Error loading config file: .docker/config.json: open .docker/config.json: permission denied
If I try to locate the file alongside daemon.json in /var/snap/docker/current/config/ this also silently fails to notice any config.json:
$ ls -l /var/snap/docker/current/config/
total 8
-rw-r--r-- 1 root root 36 Feb 28 11:28 config.json
-rw-r--r-- 1 root root 200 Feb 28 09:44 daemon.json
$ docker run -it ubuntu /bin/bash
Now, I can force the directory location, but surely there is a better way?
$ docker --config /var/snap/docker/current/config/ run -it ubuntu /bin/bash
Ok, after writing this question, I ran across the answer. Snap wants this file to go here:
$ ls -l ~/snap/docker/current/.docker/
total 4
-rw-r--r-- 1 gclaybur gclaybur 36 Feb 28 12:04 config.json

WSL1, Docker Desktop, volume mounts are always empty

I followed this setup completely. Running WSL1 with Docker Desktop on Windows 10. I am not interested in WSL2 at this point. I don't have Insider Windows.
Now, I am trying to start a container with a volume, so that the container's files are copied into the volume. According to official docs:
Populate a volume using a container
If you start a container which creates a new volume, as above, and the container has files or directories in the directory to be mounted (such as /app/ above), the directory’s contents are copied into the volume.
So it should be possible, but I must be missing something really basic here, cause it just doesn't work.
I've tried -v vol-name:/path/on/container -> this creates a named volume... somewhere. No clue where, nor how to view it. Doing volume inspect vol-name shows a path that doesn't exist, neither in WSL nor in Docker Host (Windows). I even tried mounting the MobyVM and it isn't there either.
I've tried -v /c/full/path:/path/on/container -> this creates a bind-type mount. It's empty (by design). If it put files under /c/full/path, I will see them in container under /path/on/container, but that's not what I need. I need to populate the volume with contents from container. From what I understand from documents, I need a volume-type mount, not bind-type mount. In this case the -v options forces bind-type
I've tried --mount type=volume,source=/c/full/path,destination=/path/on/container -> This results in this error: docker: Error response from daemon: create /c/full/path: "/c/full/path" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path. The path separator is not allowed...
I've read something about special characters in passwords being an issue and reset my password
I've read about the /c/full/path needing full access permission, and gave "everyone" full access
Please help
Let me summarize what i think your setup is then i will try and give a solution.
You are running docker desktop for the docker engine
You are connecting to docker desktop via the docker cli installed on WSL
You are trying to share a windows folder with a running container
You have enabled sharing of your C drive in the settings of docker desktop
I think that you are linking to the wrong path, the path you give needs to be recognized by docker desktop which remember is running in windows, therefore the path needs to be in the format c:/full/path.
So try the following to test if you have everything setup correctly
➜ cd /mnt/c
➜ mkdir -p full/path
➜ cd full/path
➜ pwd
/mnt/c/full/path
➜ docker image pull alpine
Using default tag: latest
latest: Pulling from library/alpine
Digest: sha256:ab00606a42621fb68f2ed6ad3c88be54397f981a7b70a79db3d1172b11c4367d
Status: Image is up to date for alpine:latest
docker.io/library/alpine:latest
➜ date > foobar.txt
➜ cat foobar.txt
Thu Feb 6 17:49:31 STD 2020
➜ docker run --rm -v c:/full/path:/full/path alpine cat /full/path/foobar.txt
Thu Feb 6 17:49:31 STD 2020
➜
to finish off you can use wslpath along with pwd to get the current dir in a form that docker desktop can use.
docker run --rm -v $(wslpath -w $(pwd)):/full/path alpine ls /full/path/
Hope this helps
I think that I have the same setup as you: windows 10, WSL1, docker desktop 2.2.0.0 (42247)
➜ uname -a
Linux LAPTOP1001 4.4.0-17134-Microsoft #1130-Microsoft Thu Nov 07 15:21:00 PST 2019 x86_64 x86_64 x86_64 GNU/Linux
➜ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.1 LTS
Release: 18.04
Codename: bionic
I have managed to perform the same operations as you, i did this in the following manner using alpine:
➜ docker volume create my-vol
my-vol
➜ docker volume ls
DRIVER VOLUME NAME
local my-vol
➜ docker volume inspect my-vol
[
{
"CreatedAt": "2020-02-07T11:06:15Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
"Name": "my-vol",
"Options": {},
"Scope": "local"
}
]
➜ docker run -it --name volTest --mount source=my-vol,target=/my-vol alpine
/ # cd my-vol
/my-vol # ls
/my-vol # exit
➜ docker run -it --name volTest2 --mount source=my-vol,target=/usr/bin alpine
/ # cd /usr/bin/
/usr/bin # ls -al
total 228
drwxr-xr-x 2 root root 4096 Feb 7 11:16 .
drwxr-xr-x 7 root root 4096 Jan 16 21:52 ..
lrwxrwxrwx 1 root root 12 Jan 16 21:52 [ -> /bin/busybox
lrwxrwxrwx 1 root root 12 Jan 16 21:52 [[ -> /bin/busybox
...
...
/usr/bin # exit
➜ docker container start volTest
volTest
➜ docker container attach volTest
/ # cd /my-vol/
/my-vol # ls
total 228
drwxr-xr-x 2 root root 4096 Feb 7 11:16 .
drwxr-xr-x 7 root root 4096 Jan 16 21:52 ..
lrwxrwxrwx 1 root root 12 Jan 16 21:52 [ -> /bin/busybox
lrwxrwxrwx 1 root root 12 Jan 16 21:52 [[ -> /bin/busybox
...
...
/my-vol # exit
➜
Firstly I create a volume my-vol and inspect it
Secondly my-vol is attached to the root of the container myTest, when you look inside the volume it is empty
Then attach my-vol to another container called myTest2 but in the folder /usr/bin which as you can see contains lots of files.
Now restarting myTest you can see the volume has been populated with the files from /usr/bin from the container myTest2
Can you replicate my results on your system?
Cheers

Understanding Docker volumes

I am trying to learn Docker volumes, and I am using centos:latest as my base image. When I try to run a Docker command, I am unable to access the attached volume inside the container:
Command:
sudo docker run -it --name test -v /home/user/Myhostdir:/mydata centos:latest /bin/bash
Error:
[user#0bd1bb78b1a5 mydata]$ ls
ls: cannot open directory .: Permission denied
When I try to ls to find the folder permission, it says 1001. What's happening, and how can to solve this?
drwxrwxr-x. 2 1001 1001 38 Jun 2 23:12 mydata
My local machine:
[user#xxx07012 Myhostdir]$ pwd
/home/user/Myhostdir
[user#swathi07012 Myhostdir]$ ls -al
total 12
drwxrwxr-x. 2 user user 38 Jun 2 23:12 .
drwx------. 18 user user 4096 Jun 2 23:11 ..
-rw-rw-r--. 1 user user 15 Jun 2 23:12 text.2.txt
-rw-rw-r--. 1 user user 25 Jun 2 23:12 text.txt
This is partially a Docker issue, but mostly an SELinux issue. I am assuming you are running an old 1.x version of Docker.
You have a couple of options. First, you could take a look at this blog post to understand the issue a bit more and possibly use the fix mentioned there.
Or you could just upgrade to a newer version of Docker. I tested mounting a simple volume on Docker version 18.03.1-ce:
docker run -it --name test -v /home/chris/test:/mydata centos:latest /bin/bash
[root#bfec7af20b99 /]# cd mydata/
[root#bfec7af20b99 mydata]# ls
test.txt.txt
[root#bfec7af20b99 mydata]# ls -l
total 0
-rwxr-xr-x 1 root root 0 Jun 3 00:40 test.txt.txt

Permission denied when get contents generated by a docker container on the local fileystem

I use the following command to run a container:
docker run -it -v /home/:/usr/ ubuntu64 /bin/bash
Then I run a program in the container, the program generates some files in the folder:/usr/ which also appear in /home/ but I can't access the generated files with an error: Permission denied outside the container.
I think this may because the files generated by root in the container but outside the container, the user have no root authority, but how to solve it?
What I want to do is accessing the files generated by the program(installed in the container) outside the container.
You need to use the -u flag
docker run -it -v $PWD:/data -w /data alpine touch nouser.txt
docker run -u `id -u` -it -v $PWD:/data -w /data alpine touch onlyuser.txt
docker run -u `id -u`:`id -g` -it -v $PWD:/data -w /data alpine touch usergroup.txt
Now if you do ls -alh on the host system
$ ls -alh
total 8.0K
drwxrwxr-x 2 vagrant vagrant 4.0K Sep 9 05:22 .
drwxrwxr-x 30 vagrant vagrant 4.0K Sep 9 05:19 ..
-rw-r--r-- 1 root root 0 Sep 9 05:21 nouser.txt
-rw-r--r-- 1 vagrant root 0 Sep 9 05:21 onlyuser.txt
-rw-r--r-- 1 vagrant vagrant 0 Sep 9 05:22 usergroup.txt

Resources