89luca89 / distrobox

Use any linux distribution inside your terminal. Enable both backward and forward compatibility with software and freedom to use whatever distribution you’re more comfortable with. Mirror available at: https://gitlab.com/89luca89/distrobox
https://distrobox.it/
GNU General Public License v3.0
10.12k stars 418 forks source link

Distrobox enter/create auto-detect the image from the name #836

Closed osalbahr closed 1 year ago

osalbahr commented 1 year ago

Is your feature request related to a problem? Please describe.

When I distrobox enter|create debian, it uses fedora-toolbox:38 instead of debian.

[dracula@fedora ~]$ distrobox enter debian
Error: inspecting object: no such container debian
Cannot find container debian
Create it now, out of image registry.fedoraproject.org/fedora-toolbox:38? [Y/n]: n
Ok. For creating it, run this command:
    distrobox create <name-of-container> --image <remote>/<docker>:<tag>
[dracula@fedora ~]$ distrobox create debian
Creating 'debian' using image registry.fedoraproject.org/fedora-toolbox:38   [ OK ]
Distrobox 'debian' successfully created.
To enter, run:

distrobox enter debian

Describe the solution you'd like

I'd like the commands above to work just like how --image debian auto-detects the image.

[dracula@fedora ~]$ distrobox create debian --image debian
Creating 'debian' using image debian     [ OK ]
Distrobox 'debian' successfully created.
To enter, run:

distrobox enter debian

Describe alternatives you've considered

[dracula@fedora ~]$ my-distrobox-enter() {
> distrobox create $1 --image $1 && distrobox enter $1
> }
[dracula@fedora ~]$ my-distrobox-enter debian
Creating 'debian' using image debian     [ OK ]
Distrobox 'debian' successfully created.
To enter, run:

distrobox enter debian

Container debian is not running.
Starting container debian
run this command to follow along:

 podman logs -f debian

 Starting container...                       [ OK ]
 Installing basic packages...               

Additional context

If this is a good feature I can try drafting a PR.

fbartels commented 1 year ago

I'd like the commands above to work just like how --image debian auto-detects the image.

Ihmo there is not much auto detection going on in this scenario, there just happens to be an container image that is simply called "debian".

Playing with the idea I was wondering if the name and image tags could for some uses cases simply be the same. But using a colon in the name is not possible with docker:

 distrobox create ubuntu:22.04 --image ubuntu:22.04
Image ubuntu:22.04 not found.
Do you want to pull the image now? [Y/n]: y
22.04: Pulling from library/ubuntu
3153aa388d02: Already exists 
Digest: sha256:0bced47fffa3361afa981854fcabcd4577cd43cebbb808cea2b1f33a3dd7f508
Status: Downloaded newer image for ubuntu:22.04
docker.io/library/ubuntu:22.04
Creating 'ubuntu:22.04' using image ubuntu:22.04    Error response from daemon: Invalid container name (ubuntu:22.04), only [a-zA-Z0-9][a-zA-Z0-9_.-] are allowed
 [ ERR ]

PS: if you simply want to use a different container than fedora-toolbox:38 without having to specify this container everytime you could simple export DBX_CONTAINER_IMAGE in your environment, e.g.:

$ distrobox create alpine
Image alpine:3.18 not found.
Do you want to pull the image now? [Y/n]: y
3.18: Pulling from library/alpine
Digest: sha256:82d1e9d7ed48a7523bdebc18cf6290bdb97b82302a8a9c27d4fe885949ea94d1
Status: Downloaded newer image for alpine:3.18
docker.io/library/alpine:3.18
Creating 'alpine' using image alpine:3.18    [ OK ]
Distrobox 'alpine' successfully created.
To enter, run:

distrobox enter alpine

Successfully copied 2.05kB to /tmp/alpine.os-release
osalbahr commented 1 year ago

Ihmo there is not much auto detection going on in this scenario, there just happens to be a container image that is simply called "debian".

I agree.

All I’d like is that “distrobox enter debian” becomes consistent with the behavior of “distrobox create debian”. I’m not sure if it is dependent on podman necessarily, or that my system ships with a file that contains aliases. Regardless, I think these two operations should be consistent.

distrobox create alpine

How is this different from the “distrobox create debian” I showed above? I didn’t need to set DBX_CONTAINER_IMAGE or any other environment variables.

fbartels commented 1 year ago

How is this different from the “distrobox create debian” I showed above?

Ah, I’m sorry I did forget to include the export part into my previous reply. It should have been:

$ export DBX_CONTAINER_IMAGE=alpine:3.18
$ distrobox create alpine
Image alpine:3.18 not found.
Do you want to pull the image now? [Y/n]:

All I’d like is that “distrobox enter debian” becomes consistent with the behavior of “distrobox create debian”.

The challenge behind this is that the “image” in most cases include characters, that would not be able to be used as a name for a container.

Example:

$ docker run --name myregistryhost:5000/fedora/httpd:version1.0 hello-world
docker: Error response from daemon: Invalid container name (myregistryhost:5000/fedora/httpd:version1.0), only [a-zA-Z0-9][a-zA-Z0-9_.-] are allowed.
See 'docker run --help'.

So a distrobox create debian would only work for containers hosted on the Docker Hub and you cannot specify a tag to be used.

Or maybe we are misunderstanding each other? How would you specify a tag in your supposed syntax?

osalbahr commented 1 year ago

Here is example of what I mean, using alpine:3.18 without having to specify a global variable:

[nebula@archlinux ~]$ distrobox enter alpine:3.18
Error: inspecting object: no such container alpine:3.18
Cannot find container alpine:3.18
Create it now, out of image registry.fedoraproject.org/fedora-toolbox:38? [Y/n]: n
Ok. For creating it, run this command:
    distrobox create <name-of-container> --image <remote>/<docker>:<tag>
[nebula@archlinux ~]$ distrobox create --image alpine:3.18
Creating 'alpine-3-18' using image alpine:3.18   [ OK ]
Distrobox 'alpine-3-18' successfully created.
To enter, run:

distrobox enter alpine-3-18

[nebula@archlinux ~]$ distrobox ls
ID           | NAME                 | STATUS             | MEM              | CPU%  | IMAGE                         
0f1309954260 | alpine-3-18          | Created            | 0B / 0B          | 0.00% | docker.io/library/alpine:3.18 

What I'd like is that I can immediately distrobox enter [--image] alpine:3.18. It would just be nice to be able to directly enter the newly created container.

osalbahr commented 1 year ago

Ok I just realized that distrobox create already has the ability to auto-create the name of the container from the --image without having to duplicate. For example, distrobox create [debian] --image debian. The [debian] is optional. The only thing missing is the ability of distrobox enter to do the same.

fbartels commented 1 year ago

What I'd like is that I can immediately distrobox enter [--image] alpine:3.18. It would just be nice to be able to directly enter the newly created container.

Yes, that seems like a good approach to me as well.

osalbahr commented 1 year ago

I think my use case might be too rare for it to be worth maintaining the option? I tend to see people create distroboxes graciously. I will reconsider implementing this if I find interest.

osalbahr commented 1 year ago

Closing because there is no interest and I figured out a workaround.

Few examples:

$ quick-enter alpine
Distrobox named 'alpine' already exists.
To enter, run:

distrobox enter alpine

alpine:~$ neofetch && exit
       .hddddddddddddddddddddddh.          dracula@alpine 
      :dddddddddddddddddddddddddd:         -------------- 
     /dddddddddddddddddddddddddddd/        OS: Alpine Linux v3.18 x86_64 
    +dddddddddddddddddddddddddddddd+       Host: MacBookPro14,1 1.0 
  `sdddddddddddddddddddddddddddddddds`     Kernel: 6.4.4-200.fc38.x86_64 
 `ydddddddddddd++hdddddddddddddddddddy`    Uptime: 19 mins 
.hddddddddddd+`  `+ddddh:-sdddddddddddh.   Packages: 178 (apk) 
hdddddddddd+`      `+y:    .sddddddddddh   Shell: bash 5.2.15 
ddddddddh+`   `//`   `.`     -sddddddddd   Resolution: 2560x1600 
ddddddh+`   `/hddh/`   `:s-    -sddddddd   DE: GNOME (wayland) 
ddddh+`   `/+/dddddh/`   `+s-    -sddddd   Theme: Breeze [GTK3] 
ddd+`   `/o` :dddddddh/`   `oy-    .yddd   Icons: breeze [GTK3] 
hdddyo+ohddyosdddddddddho+oydddy++ohdddh   Terminal: conmon 
.hddddddddddddddddddddddddddddddddddddh.   CPU: Intel i5-7360U (4) @ 3.600GHz 
 `yddddddddddddddddddddddddddddddddddy`    Memory: 2704.90 MiB / 7793.02 MiB 
  `sdddddddddddddddddddddddddddddddds`
    +dddddddddddddddddddddddddddddd+                               
     /dddddddddddddddddddddddddddd/                                
      :dddddddddddddddddddddddddd:
       .hddddddddddddddddddddddh.

logout
$ quick-enter ubuntu:20.04
Distrobox named 'ubuntu-20-04' already exists.
To enter, run:

distrobox enter ubuntu-20-04

dracula@ubuntu-20-04:~$ neofetch
                             ....             dracula@ubuntu-20-04 
              .',:clooo:  .:looooo:.          -------------------- 
           .;looooooooc  .oooooooooo'         OS: Ubuntu 20.04.6 LTS x86_64 
        .;looooool:,''.  :ooooooooooc         Host: MacBookPro14,1 1.0 
       ;looool;.         'oooooooooo,         Kernel: 6.4.4-200.fc38.x86_64 
      ;clool'             .cooooooc.  ,,      Uptime: 20 mins 
         ...                ......  .:oo,     Packages: 199 (dpkg) 
  .;clol:,.                        .loooo'    Shell: bash 5.0.17 
 :ooooooooo,                        'ooool    Resolution: 2560x1600 
'ooooooooooo.                        loooo.   DE: GNOME (wayland) 
'ooooooooool                         coooo.   Theme: Breeze [GTK3] 
 ,loooooooc.                        .loooo.   Icons: breeze [GTK3] 
   .,;;;'.                          ;ooooc    Terminal: conmon 
       ...                         ,ooool.    CPU: Intel i5-7360U (4) @ 3.600GH 
    .cooooc.              ..',,'.  .cooo.     Memory: 2669.47 MiB / 7793.02 MiB 
      ;ooooo:.           ;oooooooc.  :l.
       .coooooc,..      coooooooooo.                                  
         .:ooooooolc:. .ooooooooooo'                                  
           .':loooooo;  ,oooooooooc
               ..';::c'  .;loooo:'
                             .

dracula@ubuntu-20-04:~$ logout
$ quick-enter docker.io/fedora:rawhide
Image docker.io/fedora:rawhide not found.
Do you want to pull the image now? [Y/n]: 
Trying to pull docker.io/library/fedora:rawhide...
Getting image source signatures
Copying blob sha256:16170285e2a649344dd998cc435d4a045a6ae4178f21d0b6d92979db7e8a52cd
Copying config sha256:9496f13b66474fed69300b31e67125b5ddc8387d4af78df014d8edae366ebd83
Writing manifest to image destination
Storing signatures
9496f13b66474fed69300b31e67125b5ddc8387d4af78df014d8edae366ebd83
Creating 'fedora-rawhide' using image docker.io/fedora:rawhide   [ OK ]
Distrobox 'fedora-rawhide' successfully created.
To enter, run:

distrobox enter fedora-rawhide

Container fedora-rawhide is not running.
Starting container fedora-rawhide
run this command to follow along:

 podman logs -f fedora-rawhide

 Starting container...                       [ OK ]
 Installing basic packages...                [ OK ]
 Setting up read-only mounts...              [ OK ]
 Setting up read-write mounts...             [ OK ]
 Setting up host's sockets integration...    [ OK ]
 Integrating host's themes, icons, fonts...  [ OK ]
 Setting up package manager exceptions...    [ OK ]
 Setting up rpm exceptions...                [ OK ]
 Setting up sudo...                          [ OK ]
 Setting up groups...                        [ OK ]
 Setting up users...                         [ OK ]
 Setting up package manager exceptions...    [ OK ]
 Setting up rpm exceptions...                [ OK ]
 Setting up sudo...                          [ OK ]
 Setting up groups...                        [ OK ]
 Setting up users...                         [ OK ]
 Executing init hooks...                     [ OK ]

Container Setup Complete!
[dracula@fedora-rawhide ~]$ neofetch
             .',;::::;,'.                dracula@fedora-rawhide 
         .';:cccccccccccc:;,.            ---------------------- 
      .;cccccccccccccccccccccc;.         OS: Fedora Linux 39 (Container Image Prerelease) x86_64 
    .:cccccccccccccccccccccccccc:.       Host: MacBookPro14,1 1.0 
  .;ccccccccccccc;.:dddl:.;ccccccc;.     Kernel: 6.4.4-200.fc38.x86_64 
 .:ccccccccccccc;OWMKOOXMWd;ccccccc:.    Uptime: 21 mins 
.:ccccccccccccc;KMMc;cc;xMMc;ccccccc:.   Packages: 167 (rpm) 
,cccccccccccccc;MMM.;cc;;WW:;cccccccc,   Shell: bash 5.2.15 
:cccccccccccccc;MMM.;cccccccccccccccc:   Resolution: 2560x1600 
:ccccccc;oxOOOo;MMM0OOk.;cccccccccccc:   DE: GNOME (wayland) 
cccccc;0MMKxdd:;MMMkddc.;cccccccccccc;   Theme: Breeze [GTK3] 
ccccc;XM0';cccc;MMM.;cccccccccccccccc'   Icons: breeze [GTK3] 
ccccc;MMo;ccccc;MMW.;ccccccccccccccc;    Terminal: conmon 
ccccc;0MNc.ccc.xMMd;ccccccccccccccc;     CPU: Intel i5-7360U (4) @ 3.600GHz 
cccccc;dNMWXXXWM0:;cccccccccccccc:,      Memory: 2796.21 MiB / 7793.02 MiB 
cccccccc;.:odl:.;cccccccccccccc:,.
:cccccccccccccccccccccccccccc:'.                                 
.:cccccccccccccccccccccc:;,..                                    
  '::cccccccccccccc::;,.

The workaround:

#!/usr/bin/env bash

set -e

img="$1"
tmpfile=$(mktemp --suffix 'quick-enter.txt')

unset DBX_CONTAINER_NAME
distrobox create --image "$img" &> >(tee "$tmpfile")
eval "$(grep 'distrobox enter .*' "$tmpfile")"

Edit: resolved shellcheck warnings