Learning to use Docker

I think MATE probably does not have all the Gnome stuff needed by Boxes.
I agree Boxes is touchy. Vbox is even more so. I have yet to find a reliable VM
Cheers
Neville

Hi Neville,:wave:

ThatĀ“s great. ItĀ“s me who has to thank you for your kind offer. :heart:

Right.

I just started my vm.
IĀ“m afraid IĀ“ve forgotten most of what IĀ“ve learnt so far as far as docker is concerned.
So IĀ“ve got to look up some thingsā€¦ :blush:.

On Debian:

ps aux | grep docker
root      1180  0.4  9.0 1958540 91260 ?       Ssl  13:48   0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
rosika2   1572  0.0  0.0   6216   880 pts/0    R<+  13:51   0:00 grep --color=auto docker

So I guess all I need for docker is running.

Next step: getting hold of the alpine image.

Creating an Alpine Docker Container - GeeksforGeeks says:

To run the Alpine Image Docker Container, you can use the Docker run command.

sudo docker run -it alpine:3

The options would be:

  -i, --interactive          # attach stdin (interactive)
  -t, --tty                  # pseudo-tty

I hope thatĀ“s alright so far. :blush:

Many greetings
Rosika :slightly_smiling_face:

1 Like

O.K. so far, I did it this way:


docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
530afca65e2e: Pull complete 
Digest: sha256:7580ece7963bfa863801466c0a488f11c86f85d9988051a9f9c68cb27f6b7872
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

Next step would be:

Do you have any suggestions :question:
Something small would be nice. :blush:

1 Like

O.K.
Up to now IĀ“ve managed this: :blush:

docker run -it alpine

to run the Alpine Image Docker Container.

Within it:

apk add htop # to install htop


/home # apk add htop
(1/3) Installing ncurses-terminfo-base (6.3_p20220521-r0)
(2/3) Installing ncurses-libs (6.3_p20220521-r0)
(3/3) Installing htop (3.2.0-r1)
Executing busybox-1.35.0-r15.trigger
OK: 7 MiB in 17 packages

then executing htop succeed. :+1:

I even learned more:

When you use docker run to start a container, it actually creates a new container based on the image you have specified

(from: docker - I lose my data when the container exits - Stack Overflow )

ThatĀ“s why htop seems to have been lost after running docker run -it alpine again.
Well, itĀ“s actually a new container.

To get the older one running again:

docker start dd8b6e090794 # restart it in the background
docker attach dd8b6e090794 # reattach the terminal & stdin

(dd8b6e090794 being the relevant CONTAINER ID).

And voilĆ : htop is available again. :smiley:

Well, quite a bit of success so far. :wink:

Many greetings from Rosika :slightly_smiling_face:

P.S.:

even better:
(from Creating an Alpine Docker Container - GeeksforGeeks ):

Commit the changes in the Image:

Copy the Container ID and paste it in this command.

sudo docker commit dd8b6e090794 my-alpine # using my Container ID again

ā€œmy-alpineā€ is the new image name.

You can verify the success by listing the images.

sudo docker images

which gives me:

REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
my-alpine                latest    fee66c4ade28   5 minutes ago   8.89MB
alpine                   latest    d7d3d98c851f   2 weeks ago     5.53MB
ubuntu                   jammy     27941809078c   2 months ago    77.8MB
ubuntu                   latest    27941809078c   2 months ago    77.8MB
docker/getting-started   latest    cb90f98fd791   3 months ago    28.8MB
bash                     latest    f6cdab9d8531   4 months ago    13MB
hello-world              latest    feb5d9fea6a5   10 months ago   13.3kB

right, itĀ“s there. :wink:

Then I can start it with docker run -it my-alpine again. htop is availble again. :+1:

Many greetings
Rosika :slightly_smiling_face:

P.S.2:

For a quick reference regarding docker commands use the ā€œcheat sheetā€ option in duckduckgo browser:

docker cheat sheet

Click on ā€œShow Moreā€ for more commands. :wink:

1 Like

Well done Rosika,
What you have achieved in one day took me a couple of weeks at about 2 hours per day.

You know about docker ā€“ help?

What you have done in making your personalised image is called the ā€˜interactive methodā€™. ie you started with a parent image and added htop interactively.
Your next step woukd be to learn how to make an image using a Dockerfile. The basic idea is to work in a new clean directory, put a file called Dockerfile there, and do
docker build .
the dot is important.
What goes in the Dockerfile?Something like this

but you need to read up on Dockerfilesā€¦ and practice
Try making your htop image with a Dockerfileā€¦

You also need to practice managing images and containers
docker container prune
docker images
docker rmi imagename

There seems to be a convention for naming images. If you look at docker hub , they have names like rosika/htopimage:version2.
It is only a convention.

It is hard for me at the momentā€¦ we have no internetā€¦ phone line damages by some excavationā€¦ I have been trying to write a Dockerfile to install waterfox in an image. I used Ubuntu as the parent image and it seems Ubuntu (which I do not know) doesnt like where I am installing waterfox. I am going to try alpine instead as the parent. So nothing to report on that yet except frustration. Excuse the typos, this is hard on the phone

I think your posts will help others. They are well laid out with plenty of examples.

Regards
Neville

1 Like

Hi Neville, :wave:

Sorry to hear that. I hope everything will be alright again as soon as possible. :no_mouth:

Thank you very much for the compliment. :heart:

IĀ“m sure however you achieved much more in less time than me. :slightly_smiling_face:

Right, thatĀ“s what I havenĀ“t achieved so far.
Thanks for providing an example of your Dockerfile.
IĀ“ll have to do some reading up first, like you suggested.

In the meantime I downloaded the Docker_Containerization_Cookbook myself for further reference and indeed the pdf written by you. :smiley:
BTW: I was amazed to see you mentioning my name in it. ThatĀ“s really, really kind of you, Neville. Thanks a lot. :heart: :blush:

Good suggestion. IĀ“ll go for that.

Right. ItĀ“s good of you to point that out.

Well, as soon as I can come up with something substantial, IĀ“ll post it here.
In the meantime: thanks again and many greetings.
Rosika :slightly_smiling_face:

1 Like

Well I must admit, I had lots of helpful prompting from @Akito . It was not a lone effort, and I am sure Akito will help you too, because my understanding is limited.

Your book is quite good.
One of the references in my pdf document was more helpful than the others. I could point it out If I was on my PC , but i am on phoneā€¦ still no internet. I will look it up .

Dockerfiles first try. Just do in the Dockerfile the same things you did interactively to make the htop image. It can be that simple. You can make it sophisticated later.

Not much progress at my end. Have discovered Alpine uses Busybox so I cant just install a binary in /usr/bin and have Alpine find it . I need to learn about Busybox.

Regards
Neville

1 Like

That is incorrect. Busybox is not the problem
Problem is waterfox is compiled against glibc, and Alpine uses musl librariesā€¦
I either have to add glibc to Alpine, or go back to using an Ubuntu parent image, or some other third alternative parent image.

So the takeaway message isā€¦ If you use Alpine, either make sure any executables are musl, or install glibc.

1 Like

@nevj:

Hi Neville, :wave:

Thanks a lot to both of you. :heart:

Thanks a lot in advance.

O.K. IĀ“ll try to achieve this goal.
Still have to a bit of reading first as far as creating a Dockerfile is concerned.

Thanks for the valuable hint. :+1:

Hope your internet connection will be up and running again pretty soon. :blush:

Many thanks and many greetings
Rosika :slightly_smiling_face:

1 Like

Hi all, :wave:

IĀ“ve stumbled about a problem I cannot seem to solve. :roll_eyes:

These are my present docker-images right now:


docker image ls
REPOSITORY               TAG       IMAGE ID       CREATED          SIZE
my-alpine                latest    56f79953a77f   12 minutes ago   73MB
my-ubuntu                latest    3a894ccb953b   16 minutes ago   222MB
alpine                   latest    d7d3d98c851f   3 weeks ago      5.53MB
ubuntu                   jammy     27941809078c   2 months ago     77.8MB
ubuntu                   latest    27941809078c   2 months ago     77.8MB
docker/getting-started   latest    cb90f98fd791   3 months ago     28.8MB
bash                     latest    f6cdab9d8531   4 months ago     13MB
hello-world              latest    feb5d9fea6a5   10 months ago    13.3kB

my-alpine and my-ubuntu are the images that have been modified by me and created with the docker commit command.

In both of them I installed the fish-shell (using docker container interactively with docker run -it [...], and the installation was successful.

However:

Setting fish as the default shell seems not to have been possible. :confused:
ā€¦ in neither of the two.

In my-alpihne I followed the steps seen at Change default shell - Alpine Linux

Went well, except that I had to issue the command
# lchsh instead of # lchsh USER, me being root.

Anyhow:

cat /etc/passwd
root:x:0:0:root:/root:/usr/bin/fish
[...]

seems alright, I guess.
Having saved everything by invoking the docker commit again ā€¦
IĀ“m still not getting fish per default after reboot. IĀ“d have to start in manually. :thinking:

So I did the same thing with my-ubuntu.
After having installed fish I set it as the default shell thus:
# chsh -s /usr/bin/fish.
Saving everything again, but after a reboot: fish isnĀ“t started here as well.
Despite the following output:

# echo $SHELL
/usr/bin/fish

and, also here:

cat /etc/passwd 
root:x:0:0:root:/root:/usr/bin/fish

As neither alpine nor ubuntu start fish despite the fact that itĀ“s successfully set as the default shell, I infer that this must have something to do with docker and the fact that the OSs run as containers :question:

Hmm, I wonder why that behaviour might be the way it isā€¦ :thinking:

Many greetings from Rosika :slightly_smiling_face:

1 Like

Thatā€™s already what makes things complicated. docker commit is there for temporary investigation of a running container. For example, for debugging purposes. I personally never really used docker commit in all these years.

You should start writing a Dockerfile. Itā€™s much simpler, much more transparent & easy to understand, than just creating an image from a running container, which is, as you just experienced, error-prone.

I suspect, it is related to the ENTRYPOINT or CMD directive of the original Dockerfile your base image uses. However, Iā€™m not sure.

Solution

Write a Dockerfile, instead of using docker commit.

2 Likes

This is how I made bash the default shell in my Alpine image.

2 Likes

Thanks @Akito for your reply.

I see.
Well, as everything was successfully saved so far, except fish being used as the the default shell, I thought this would be the way to go. But - as you said - it seems to be error-prone. :blush:

That might be it. Thanks.

Right. IĀ“m just in the middle of the process of learning how to do it. :blush:

As the first step (for writing a dockerfile) is concerned:

FROM alpine
latest: Pulling from library/alpine
530afca65e2e: Pull complete
Digest: sha256:7580ece7963bfa863801466c0a488f11c86f85d9988051a9f9c68cb27f6b7872
Status: Downloaded newer image for alpine:latest
---> d7d3d98c851f

(from @nevj Ā“s pdf)

IĀ“d like to avoid having alpine to be pulled every time anew (have to watch my data-consumption).
Yet thereĀ“s an alpine image already available on my system:

docker image ls
REPOSITORY               TAG       IMAGE ID       CREATED             SIZE

alpine                   latest    d7d3d98c851f   3 weeks ago         5.53MB

ā€¦ even with the tag ā€œlatestā€.

In that case: would the image still have to be pulled every time the Dockerfile is used to create a new container, or is the already available image used :question:

Many thanks and many greetings
Rosika :slightly_smiling_face:

1 Like

For example, with Docker Compose, you can set the pull_policy.

For Docker, it depends. If you use the most common Docker command docker run, you can specifiy it as a CLI argument.

docker run -it --pull never alpine
2 Likes

Thanks a lot @Akito, :heart:

I see.
Taking up what you said I also looked up the man-pages:


man docker build
[...]
 --pull true|false
          Always attempt to pull a newer version of the image. The default is false.
[...]

which seems to indicate a newer version is not pulled in the building process by default, which looks good to me.

Well, thatĀ“s great, Akito. :+1:
I certainly wouldnĀ“t have come anywhere near to your solution. :blush:

I tried to follow it, and to a certain point I succeeded, but you lost me here:

No idea why ā€œtzdataā€ is needed for bash.

This package contains data required for the implementation of
standard local time for many representative locations around the
globe.

(apt-cache show tzdata)

And

RUN rm -fr /tmp/* /var/tmp/* /var/cache/*/*

is too sophisticated for me as well.
Here your expertise is clearly showing. I tip my hat to you, Akito. :+1:

Thanks a lot and many greetings.
Rosika :slightly_smiling_face:

1 Like

It will pull it once, and then use the saved image.
Use a specific version of Alpine, then it will not try to get ā€˜latestā€™
I am able to operate at the moment with no internet at all, because I have a couple of saved images of ubuntu and alpine

Akito has shown you how to set the shell in Dockerfile with the CMD statement.
You can also override what the command statement says when you run it ā€¦ something like
run -it imagename /bin/sh
What you start on the CMD statement at the end of the dockerfile does not have to be a shell. It can be any program that is set up in the container.

Regards
Neville

1 Like

Thanks, Neville, for your reply, :wave:

O.K., good to know.
Since I have already pulled ā€œlatestā€ IĀ“d have to get hold of Alpine with a specific version number then.

Great. :+1:
IĀ“ve issued the command docker run -it my-alpine /usr/bin/fish and the command was executed immediately, i.e. the fish-shell was running at start:

docker run -it my-alpine /usr/bin/fish
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
root@a87f80a98a10 /#

Right, thanks. IĀ“ll have to experiment with that.

In the meantime I finished reading your pdf.
I must say itĀ“s very well written and I like the way you take the reader along with you on your journey of how to make docker work. ItĀ“s a very interesting approach.:+1:

This line however
CMD ["sh -ex folli.scr >&out"]
is what I fear I donĀ“t completely get. What may the ampersand stand for?

I know in redirections e.g. ā€œ2>&1ā€ means ā€œwrite stderr to the same place stdout is writtenā€.
So in your example ā€œ>&outā€ I guess ā€œstdoutā€ is implied, i.e. ā€œ1>&outā€, right :question:

Hopefully your internet connection will be back soon. :blush:

Many thanks and many greetings.
Rosika :slightly_smiling_face:

1 Like

I may have written that incorrectly. will check.
What I wanted was to write stdin and stderr on a file called ā€˜outā€™
I never got that version to make the file ā€˜outā€™ so maybe you have debugged it for me

 I finished reading your pdf.
I must say itĀ“s very well written and I like the way you take the reader along with you on your journey of how to make docker work. ItĀ“s a very interesting approach

Thats just the way I write when it is not a formal publication. I just put it together as I do the workā€¦ a bit like using a script fileā€¦ but I use it for research work too, just to keep track of what I do when I do statictical analyses. It can get a bit longwinded for anyone else reading it. Iam glad you coped with it. I hope it was of some benefit.
You actually write in a similar way when you do replies to posts.
Its a sort of a cross between a blog and a tutorial. I think the way you do it is very successful.

I did promise to identify what I think is the best dockerfile reference, apart from your book. Will do, I forgot tonight, sorry.

I have been busy moving an installed linux from one machine to another. Have you tried that? I just use rsync to copy it, then mount the new filesystem and fix up things in /etc like fstab, hostname, hosts, Then just update-grub in the linux that controls grub, and it appears in the boot menu. Simple.

My work on docker has stalled. I need internet to download some things.

Great. :+1:
IĀ“ve issued the command `docker run -it my-alpine /usr/bin/fish` and the command was executed immediately, i.e. the `fish` -shell was running at start:

Try putting ā€˜htopā€™ in the CMD statement of your Dockerfile.
Then when you run it you will will get htop
Then try overriding it with /usr/bin/fish on the run statement as above
That is useful if it doesnt work properly and you want to get inside the container and see what the problem is.

Regards
Neville

1 Like

Hi Neville, :wave:

thanks a lot for your reply, in spite of the fact that your internet connection is still down. :heart:

I see. WouldnĀ“t it be something like

CMD ["sh -ex folli.scr 0>out 2>&0"] :question:

I may be wrong here. Surely @Akito can help.

Very good approach. :+1:

What a nice compliment. Thanks a lot. :smiley:

Actually no. But thanks so much for providing the how-to.

I still have to come up with a Dockerfile. HavenĀ“t got to that point but that will be my next step.

Thanks again.
Many greetings from Rosika. :slightly_smiling_face:

Not sure about this.

These are ways how to do it, mainly based on tee.