12/15/2021

Python Ubuntu Docker

38

Docker SDK for Python¶ A Python library for the Docker Engine API. It lets you do anything the docker command does, but from within Python apps – run containers, manage containers, manage Swarms, etc. For more information about the Engine API, see its documentation.

Aug 07, 2021 Turns out that docker images are just a tar collection of files. There are several versions of the spec. For v1.0 the specification is defined here. Instead of writing down the spec lets look into a single docker image. Docker pull ubuntu:latest docker save ubuntu:latest -o /tmp/ubuntu.tar List the directory structure of the docker image. It’s time for a new version of the Ubuntu PFNE Docker image to support Network engineers learn Python and test automation. Recently, Ubuntu announced that on the Ubuntu Docker Hub the 18.04 LTS version is using the minimal image. With this change when launching a Docker instance using. $ docker run ubuntu:18.04. [email protected]:$ docker-compose -version docker-compose version 1.25.0, build unknown Install the latest Docker Compose on Ubuntu using PIP. PIP stands for 'PIP Installs Package'. It's a command-line based package manager for installing Python applications. Since Docker Compose is basically a Python program, you can use PIP to install it. Python is an interpreted, interactive, object-oriented, open-source programming language.

About one year ago I’ve created the Ubuntu 16.04 PFNE Docker image. It’s time for a new version of the Ubuntu PFNE Docker image to support Network engineers learn Python and test automation.

Recently, Ubuntu announced that on the Ubuntu Docker Hub the 18.04 LTS version is using the minimal image.

With this change when launching a Docker instance using

you’ll have an instance with the latest Minimal Ubuntu.

While this is great, especially if you need to quickly pull an image, the fact stays that it doesn’t have preinstalled the necessary tools to test network automation, learn Python or run some QoS tests using packages like IPerf.

Based on My previous Ubuntu 16.04 PFNE Docker image, I’ve created the same using the new Ubuntu 18.04 LTS minimal image.

It contains all the tools found in Ubuntu 16.04 PFNE:

  • Openssl
  • Net-tools (ifconfig..)
  • IPutils (ping, arping, traceroute…)
  • IProute
  • IPerf
  • TCPDump
  • NMAP
  • Python 2
  • Python 3
  • Paramiko (python ssh support)
  • Netmiko (python ssh support)
  • Ansible (automation)
  • Pyntc
  • NAPALM

and two new additions:

  • Netcat
  • Socat

I’ve added these two because some blog followers asked me, after reading the Ubuntu image for eve-ng – Python For Network Engineers post, if I can add to image servers installation like web, ftp, etc.

Docker

Personally, I don’t think is needed to burden the image with these extra packages. You can have tools like Netcat testing various servers. This is one of the reasons I’ve added Netcat and Socat.

It’s easy for me to add them to this image or future ones (and I’ll do it if I get more requests), however I’m planning some articles on how to do your own Docker images and add whatever packages you need.

While writing this post, time to push it to Docker Hub :)

If you want to test the new Ubuntu 18.04 PFNE Docker image, please pull it from Docker Hub:

To start it use:

Let me know if you find this useful, happy testing and most important Never Stop Learning!

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:

  1. Installing docker on ubuntu 20.04 LTS
  2. Creating and running a python script using docker image

Installing Docker

Install python ubuntu docker

Python Ubuntu Docker Free

Docker ubuntu image python

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:

Setting up the Repository

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.

Installing Docker Engine

Install Python Ubuntu

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.

Creating First Docker Script

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 Dockerfile.

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.

Building the Docker Image

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:

Here 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.

Running the Docker Image

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!

Reference

Related

  • Most Viewed News

    • Macos App Torrent
    • Mac Python 3.8 Install
    • Change Date Terminal Mac El Capitan
    • Mac Games Direct Download