Speed breaker – 2 ; Mac crashing

This week the speed breaker came in the form of not code, rather hardware.

Mac is trying to upgrade to Catalina – I said yes.

But before it completes I have to be mobile. Was sitting in bus and it asked me to complete upgrade, I said yes – but I guess without internet it can not complete upgrade. So far so good.

I rebooted Mac (I do it from time to time, when I want to context switch my work – this forces to close all open windows and freshly starts everything on new work, also machine won’t be slow. True in windows more but kept the hobbit of rebooting frequently)

After reboot all I got is circle-slash!

Took couple of hours to finally reinstall and get back to work.

Also trying to learn about gRPC – seems simple but carefully reading the dcoument as I am in no rush.

vimrc backup

syntax on
set number
set lazyredraw
set showmatch
set laststatus=2
set ruler
set visualbell
colorscheme murphy ” good colorschemes: murphy, slate, molokai, badwolf, solarized
” mostly taken from https://chrisyeh96.github.io/2017/12/18/vimrc.html”
filetype plugin indent on
set tabstop=4
set expandtab
set shiftwidth=4
set softtabstop=4

set autoindent
set smartindent

set incsearch
set hlsearch

nnoremap :nohlsearch

set autoread

Speed breakers this week (a missing slash and incorrect pwd) – 1

This is first of series of posts I am going to write as and when it happens.

When trying to develop code, the first limitation is body and brain. (or heart and soul) ; But some times silly things take up all the available time wasting precious dev time. Here are two such things happened to me this week.

All I am trying to do is create a rudimentary web html page to try one of the REST API. (I know postman could be used, but I have plans to evolve this rudimentary into full blown web UI later – anyhow the REST API is already well tested with unit testing and postman)

 <form action="/restapi" method="post"> 

And it didn’t work!. Surprise, surprise!!
when I convert this to GET its working. I created so many forms in the past what the hell is happening? I check/recheck every bit by bit – deleted everything and retyped. Nothing is helping. There is no way this could go wrong. I double check, recheck my back-end code. Try different ways of doing back-end code. Is it because I am not including request.Form.Parse()? OK – no harm, let me add it. No luck. Took a break, and restarted with a fresh mind.
Finally a light bulb💡 – instead of worrying about back-end code I thought may be browser is not sending the post data, but that is not the case 😞 – anyhow, I Google searched for GET is working and POST sending empty data. Finally some result to help me 😥. If we don’t put a slash in the end it will redirect without post data.

I wrote so many forms in the past but most of them are for PHP or c#; this one is for golang. In PHP I used to give complete file name something like restapi.php so redirection didn’t came into picture. c# thanks to visual studio it is taken care.

After adding a slash it started working! Silly!!

 <form action="/restapi/" method="post"> 

Docker PWD

The second speed breaker is when trying to run ./main in docker I got incorrect permissions.

I added chmod +x ==> no luck.

I have a RUN cd main followed by CMD [“./main”] in Dockerfile ;

Expected PWD when CMD is being executed to be main directory. But that is the not the case –

so ./main ==> I assumed it will in main directory executing main exe; but docker is trying to run main directory and getting no permissions issue!

After staring at the screen for a long time, I realized what is happening. (of course did many not so useful search and reads)

Hindsight – I should never name directory and other files with same name. It adds confusion to the error messages. Making almost error message useless.

Fixed this by making WORKDIR /app/main before CMD.

Docker notes …

Basic commands:

docker image

docker image ls
(displays list of images available on this machine)
(or simply docker images instead of docker image ls)

docker image ls -a
(displays list of all images. I think it includes deleted images? intermediate images?)

docker rmi ==> will remove the image.

docker container

docker container ls
(displays all containers currently running)
(or simply docker ls)

docker container ls -a
(displays all containers including those stopped, exited)
(Question – can we restart a container?)
(Question – can we delete an image and still container be running?)

docker rm ==> will remove the container.

Image Vs Container

This link is good.
An image is an inert, immutable, file that’s essentially a snapshot of a container. Images are created with the build command, and they’ll produce a container when started with run. Images are stored in a Docker registry such as registry.hub.docker.com. Because they can become quite large, images are designed to be composed of layers of other images, allowing a miminal amount of data to be sent when transferring images over the network.

‘docker history’ is your friend

docker history command is your friend. It gives complete history of all the layers an image is built on including the size of each layer.

docker inspect is second best friend

docker inspect is awesome! It gives everything you need to know for a container.

Digging inside docker container

  1. docker attach (is one way)
    To detach try Ctrl + P / Ctrl + Q
    If it doesn’t work – try to kill the process which is attached. (pkill -9 -f 'docker.*attach')

2. docker container exec
e.g.. docker container exec 76192e17722e cd bin && ls


docker commit

This is another cool command. It makes a new docker image from running container. Take it to new machine and start running; or use it for debugging, Use the image to start container with additional options.

docker inspect

Looks like this is similar to docker history for image ;

Helpful tips for debugging container issues

  1. docker container exec <run any command like ps, top, ls>
  2. docker container exec -it <id> bash
  3. docker container exec -it -u 0 <id> bash
  4. docker container logs << Must run after you start a container>>
  5. docker container stats << view memory/CPU usage>>
  6. docker container top
  7. docker cp
  8. docker diff
  9. docker export (Gets all files as tar)

cleanup

  1. docker container prune ==> removes all stopped containers.
  2. docker image prune
  3. docker system df
  4. docker system prune (My favorite)
  5. docker rm $(docker ps -aq -f status=exited)
  6. This link is good.


Another doc link from docker to understand more about images vs containers.