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 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 to start with.
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.
- Create a Docker Hub account from here and download
- 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” errors 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:UsersRakeshDesktopDocker Projects>docker -v Docker version 19.03.8, build afacb8b
YOUR FIRST DOCKER PROJECT
Find a place for your project: like
Now that we’re here, let’s create our first Dockerfile. (Make sure the file has no extension)
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/
# 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.
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.
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.
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.
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 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 create your image.
Docker images: Running this command anywhere on the system, assuming
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:UsersRakeshDesktopDocker Projects>docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello world latest 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:UsersRakeshDesktopDocker 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:UsersRakeshDesktopDocker 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
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 the bash shell of your docker container. (Note that this is your Linux machine now. Therefore, you can only use Linux commands.)
Now let’s play with some python stuff:
C:UsersRakeshDesktopDocker 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(“I love using Docker”) I love using Docker >>> >>> exit() root@d647215fc346:/# root@c186684cf4ea:/# exit C:UsersRakeshDesktopDocker Projects> docker
- stop <your-container-id>:
ps -a’, showed us that our docker container had an id of 6a22cab59bb8. Now we can stop and delete that
container C:UsersRakeshDesktopDocker 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 command ‘docker ps -a’, you can see that there is no image with its container ID “6a22cab59bb8”.
C:UsersRakeshDesktopDocker Projects>docker rm 6a22cab59bb8 6a22cab59bb8