Docker Installation and a Simple tutorial in Windows – LinkedIn

Docker is a tool designed to make it easier to build, deploy, and run applications using containers. Containers allow a developer to package an application with all the parts it needs, such as libraries and other dependencies, and deploy it as a package. By doing so, thanks to the container, the developer can be sure that the application will run on any other Linux machine, regardless of any custom settings that machine may have that may differ from the machine used to write and test the code.

Difficult to understand, no problem. Below I have tried to explain this in a better way.

Explanation and Why Use It?

If you cloned any of my github projects and tried to run them, there’s a very good chance you’ll have errors doing so unless you know exactly how I set up my system and virtual environment. Most repositories cover the virtual environment part, but not how my operating system is configured. That’s a big challenge in the beginning.

Manually deploying a project on a Linux server can be fairly straightforward, but it can also be very cumbersome and tedious. Doing it hundreds of times is frankly a waste of time.

Type Docker. Docker simplified this process by allowing you to set up the environment in advance. That setup can be easily shared and easily automated. In addition, Docker creates an isolated environment called a container (much like a virtual machine) which is great when it comes to all sorts of dependencies.

Inthis post, I wanted to show you how to use Docker’s absolute basics.

Start.

  • Create a Docker Hub account from here and download
  • Docker Desktop.

  • Install Docker Desktop on your machine and open it. (It will normally consume 2 GB of memory in Docker by default, but if there are any “Not enough memory” error as shown below, reduce the memory from 2 GB to 1.25 GB using Docker >> Settings >> Resources >> Advanced).

Docker won’t start on Windows: Not enough memory to start Docker

  • After installation, make sure to log in to Docker Desktop. (Since I’m already signed in, it shows Sign Out.)
  • Now, verify the installation, using the following command.

C:\Users\Rakesh\Desktop\Docker Projects>docker -v Docker version 19.03.8, build afacb8b

YOUR FIRST DOCKER PROJECT

Find a place for your project: like

cd path/to/your/dev/folder

Now that we’re here, let’s create our first Dockerfile. (Make sure the file has no extension)

Dockerfile Notepad

A docker file is a standardized way for Docker to create your Docker image. A Docker image is essentially an operating system and usually a Linux one. Docker images can be easily moved and reused for a variety of use cases.

Let’s edit our Dockerfile now:# In path/to/your/dev/folder/

Dockerfile

# Base Image FROM python:3.6 RUN apt-get update && apt-get install -y -no-install-recommends \ python3-setuptools \ python3-pip \ python3-dev \ python3-venv \ git \ && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* EXPOSE 8000 CMD python -c “print(‘hello world’)”

What’s going on here? Remember, capitalization is very important.

FROM

will build your image from another pre-existing image. This can be quite advanced, so for now, we only use a good Python 3.6 image.

RUN RUN

is a command that allows you to do any bash shell command you would normally do. In our case, we only do some basic system updates and basic installations.

EXPOSE

EXPOSE

allows your Docker image to have a port or publications exposed outside the image. This is important for web applications and software that you want to receive requests.

CMD CMD

this is the final command that will run your Docker image. It is usually reserved for something like running a web application.

COPY COPY copy

is another command we haven’t added to our Dockerfile yet. This will allow you to copy local files to your Docker image.

Now let’s build our real image.

First, make sure that the Dockerfile path and the executable path are the same.

>> cd path/to/your/dev/folder >> dir Dockerfile >> docker build -t hello-world -f Dockerfile .

This will build your real image. It may take a while (5-15 minutes) depending on a number of factors. After compiling 1 time, future builds may not take as long :).

  • -t part stands for “tag” and you can add your own tag name that I used ‘hello-world’, as this might be your first time using Docker
  • .

  • -f is the path to the Dockerfile that you are going to use to build your image.

COMMANDS

Docker images: Running this command anywhere on the system, assuming

  • Docker

Desktop is running, will display all the images your system has. You will also have the python 3.6 image that we took in our Dockerfile. Great, right?

C:\Users\Rakesh\Desktop\Docker Docker projects>images REPOSITORY TAG IMAGE ID SIZE CREATED Hello World Last EA9B2A5145F7 3 hours ago 1.01GB python 3.6 e0373ff33a19 12 days ago 914MB hello-world <none> bf756fb1ae65 6 months ago 13.3kB Docker ps -a:C:\Users\Rakesh\Desktop\Docker Projects>docker

  • ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6a22cab59bb8 bf756fb1ae65 “/hello” 23 minutes ago Exited (0) 23 minutes ago distracted_perlman

  • docker run -it <your-tag>:

This creates a “container” from the tagged image. In other words, a container is an instance of an image.

C:\Users\Rakesh\Desktop\Docker Projects>docker run -it hello-world hello world If you

copied our Dockerfile exactly, you should see the response of ‘docker run -it hello-world’ as simply ‘hello world’. This means that your image is working! docker

run -d -it

  • <your-tag>:

If you add -d, it will run in the background. This has its own section because running your container in the background can cause conflicts with other containers using the same port.

  • docker run -d -it <your-tag> /bin/bash:

That’s it. You now have access to your docker’s container bash shell. (Note that this is your Linux machine now. Therefore, you can only use Linux commands.)

Now let’s play with some python stuff:

C:\Users\Rakesh\Desktop\Docker Projects>docker run -it hello-world /bin/bash root@d647215fc346:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@d647215fc346:/# pwd / root@d647215fc346:/# pip freeze root@d647215fc346:/# python Python 3.6.11 (default, Jun 30 2020, 19:21:43) [GCC 8.3.0] on linux Type “help”, “copyright”, “credits” or “license” for more information. >>> import os >>> os.path.expanduser(“~”) ‘/root’ >>> 4+4 8 >>> print(“Love to use Docker”) Love to use Docker >>> >>> exit() root@d647215fc346:/# root@c186684cf4ea:/# exit C:\Users\Rakesh\Desktop\Docker Projects> docker

  • stop <your-container-id>:

‘docker

ps -a’ command, showed us that our docker container had an id of 6a22cab59bb8. Now we can stop and delete that

container C:\Users\Rakesh\Desktop\Docker Projects>docker stop 6a22cab59bb8 6a22cab59bb8

  • docker rm <your-container-id>:

This command will delete your Docker image. After running the following command, if you use the ‘docker ps -a’ command, you can see that there is no image with its container id “6a22cab59bb8”.

C:\Users\Rakesh\Desktop\Docker Projects>docker rm 6a22cab59bb8 6a22cab59bb8

Thank you….