Wednesday, August 12, 2020

My docker learnings

Recently I started working on a project which includes both NodeJS and Python application. We planned to Deodorization build process for our project. During Deodorization I faced multiple issues which I eventually solved. I thought of writing a post on my docker experience to help others. Below are the some of my observations/issues I found during my journey

1. Using multiple FROM in docker file: Never ever use two FROM instructions in the Dockerfile. Docker will take only last FROM instruction. Use one FROM instruction and install another framework. In my case I use FROM for NodeJS and installed python as shown below.

WRONG WAY:
CORRECT WAY:



2. Never copy all files at the same time: This is really funny, When you install dependencies after copying all the files, dependencies will install every time when you run docker build. 

INCORRECT WAY:



Instead of copying all files, copy only dependency file (package.json for NodeJS or requirements.txt for python), install dependencies and then copy all source files.

CORRECT WAY:

3. Using RUN stead of CMD: Never use RUN command in Dockerfile, Dockefile is for creating a docker image and not executing any commands. Instead of RUN, use CMD.


4. Showing docker containers (running docker images): Most of the docker commands are similar to basic unix commands. We can use unix `ps` command to display all running docker images.

To display docker images: `docker images`




To display docker containers: `docker ps`





5. Use docker exec to run commands on docker container: Using docker exec command, we can run all basic unix commands like `ls`, `cd` etc on docker container while running the docker image. This will be very useful for checking logs in the container. 













6. Connect to the docker container: Get the current docker containers by running docker ps command. After that use exec command to connect to docker container shell prompt.
`docker exec -it CONTAINERID /bin/bash`













7. How to access host application from docker container: I spent more time compare with other topics in docker to know this. When running docker in the host and trying to access some endpoints like database from host, docker doesn't know which is host and which is docker. Need to specify the host specifically. If you want to access endpoints from host, you need to specify `host.docker.internal` instead of localhost or 127.0.0.1. This differentiates docker localhost and actual localhost.



8. Expose Port numbers: When containerizing any API end points, we need to explicitly expose port numbers in the Dockerfile. Otherwise, These ports are not accessible from host machine.
Run docker image: Run docker image by specifying  exposed ports in the run command as below.




Happy Learning!!!!



Popular Posts