I always get in trouble when creating a new project with different dependencies installed on my machine. I had to reinstall new libraries, which is doing a new thing repeatedly, and it’s time-consuming. Then I learned about docker. Docker will allow you to create a virtual machine to install all your dependencies for your specific project without tempering your primary system. Still, it is faster than the virtual machine. In this tutorial, we are going to see:
Newgrp docker How to mount the ISO. Now we need to mount the ISO you downloaded (or created yourself). I'll demonstrate with the Ubuntu Server 20.04 ISO. Before you mount the ISO, create two new. Now, verify you can run docker without sudo: docker ps docker run hello-world. Super simple, anybody can do it. Nov 15, 2017 newgrp docker is very different from usermod -aG: newgrp changes primary group ID where usermod -aG does not, it adds user to the group so it can access files owned by that group. – Oliver Feb 6 at 14:54. Jun 11, 2020 newgrp docker How to create the base container. The first thing we'll do is create a base container for which we'll build our custom image from. We'll use the alpine:nginx image for this.
For installing docker from the website for the different OS, you can visit here. First of all, we are going to uninstall previously installed docker (if have any). We will run the following command in the terminal to do that:
Then we need to update the
apt packages and install the following dependencies by running the commands:
Now we need to add the Docker’s official GPG key to the system.
We will use the following command to set up the stable repository.
To install the docker engine first we have to update the
apt package again and run the following commands:
Now, the docker should be installed in our system, but before we test it, first we need to add the docker into usergroup, otherwise in some system it will give error. We can do this by using the following commands:
After running this command, you have to logout or restart the pc. Then run the following command to test the docker system.
This will download a test container and print some messages and then it will exit.
Now, it’s time to create our first program that will run on docker. Here we will create a simple script that will measure the square root of a number using the python’s
NumPy library. So, in this program, we have a dependency, and that is NumPy. We have to install the dependency inside our docker container. First, we will create a folder containing all the necessary things such as python file, docker image etc.
Then we will create the python script which we will run, let’s name it
main.py and write the following code into that.
Now, let’s create a file name
Dockerfile. This file is the blueprint of the docker image. “A docker image is a combination of a file system and parameters.' In other words, this file will contain the information on the working environment, dependencies etc. of our docker app. And the docker container (it’s an instance of docker image) will run the docker system.
Let’s put the following lines in the
Now, let’s understand what these lines represent. The docker needs an environment where the docker will run, and this environment can be an operating system, a software package, a python environment etc. Here we are using Python version 3.8 as our environment. By executing
FROM python:3.8 line, the docker container pulls python version 3.8 from the docker hub. To add the python script in the docker current directory, we will use
ADD main.py . line. The
. specifies the current directory. Now, we will be going to install the dependencies, in the python script we have only one dependency, and that is numpy. So to install the NumPy dependency, we are using
RUN pip install numpy. If your program has other dependencies, you can add them in this line. And finally, to run the python script, we will use
CMD command. Usually, we run a python script in the terminal by using
python main.py command. Just like this, we will pass this command into a tokenized list with the
CMD command. So,
CMD ['python', 'main.py'] will run the python script inside the docker container.
We have the
Dockerfile ready, and we now know what it contains and what the lines inside the
Dockerfile does, it is time to build the docker image! To build the docker image, we need to give the following command:
first-docker is our docker image name. We can name it whatever we want. And the
-t argument is used for tagging the resulting image. After running this command, it will look like the following screenshot.
So, our docker image is now ready. To run our image we need to give the following command in the terminal:
This command will run the docker image and give the output of the python script we have written. In our case, we have calculated the square root of 16 using NumPy’s sqrt function. So we will see the result like the following screenshot.
Congratulations! We have just run our first docker app. We can do more complex things with docker, keep digging. The code for this tutorial can be found in here.
Have fun and Stay Safe!