Docker – Live Learning Notes

Greetings! Time for me to learn docker and docker-compose. I have heard that it’s similar to vagrant in functionality and much light weight. But never tried before. Let’s see how it unfolds.

First, I am installing it.

brew install docker
docker --version
    Docker version 19.03.14, build 5eb3275

Now, going to take a look on their official docs for intro: #1 https://docs.docker.com/get-started/

Great video actually: #2 https://youtu.be/iqqDU2crIEQ

My friend Gutsytechster also recommended me to watch following two videos. Again, these videos are introductory. So the idea is to solidify my basics.

  1. https://www.youtube.com/watch?v=YFl2mCHdv24
  2. https://www.youtube.com/watch?v=Qw9zlE3t8Ko

Amazing! I understood the concept and need for both docker and docker-compose.

While watching the video I tried docker images and docker ps commands, but I got the error:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

A probable fix is here: #3 https://stackoverflow.com/questions/44084846/cannot-connect-to-the-docker-daemon-on-macos

But I decided to install Docker for Mac Desktop itself -_- : #4 https://docs.docker.com/docker-for-mac/install/

Great! The installation also showed a default demo of Docker. And now docker CLI is working!! But the questions is where should I try to implement it?

Let’s do it on one of my personal project, that I have been using for all these experiments lately. That is Nickoscope: https://github.com/geekyshacklebolt/nickoscope

I would create a Dockerfile that copies my code into the image and then build it inside and probably start running it. Let’s do it.

Add required dunder check

# add this at the end of my "nickoscope.py" that is your main flask module
if __name__ == "__main__":
    app.run(host="0.0.0.0", port="5001", debug=True)

This is just so that I can run my flask module directly using python nickoscope.py and I won’t need to setup FLASK_APP env variable in this case.

My Dockerfile

FROM python:3.8-slim
COPY . .

# Install core dependencies
RUN apt-get update && apt-get install -y python3-pip

# Install requirements
RUN pip3 install -r requirements.txt

# Run flask applications
CMD ["python3", "nickoscope.py"]

After saving it as “Dockerfile” in the project root. I did:

docker build -t nickoscope01 .
docker run -dp 5001:5001 nickoscope01

And my app was running fine on the browser. Some handy commands:

docker ps -a shows all the running image processes (containers)

docker stop <container name or id> to stop a running container

docker start <container name or id> to start a stopped container

docker rm <container id or name> to delete the process from log

docker rmi <image-id or image-name> to delete the docker image

My Docker-compose

I also tried writing a docker compose, although it wasn’t necessary as my task could be completed only using one container. But just for my learning purpose I tried writing a docker-compose.yml

version: "3.7"

services:
  app:
    build: /Users/shiva/path/to/project/nickoscope
    ports:
      - 5001:5001

That’s it XD

docker-compose up -d

And my app was running on the browser again!! To shutdown the docker-compose process. Just do it.

docker-compose down

Now I am a little handy with docker, in future if something comes up that is associated with docker, then at least I won’t be blank.

Thanks for following up! Have a nice day!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s