Ubuntu Docker installation and use

Everyone must have encountered this kind of scenario. The program developed by oneself runs normally in the development environment, but many problems appear when it is run in the production environment. How did that work out?

To completely solve this problem, we must introduce our protagonist today, Docker!

Docker is an open source application container engine, based on  the Go language  and open source in compliance with the Apache2.0 protocol.

Docker allows developers to package their applications and dependencies into a lightweight, portable container, which can then be distributed to any popular Linux machine, and can also be virtualized.

The container is completely using the sandbox mechanism, and there will be no interface between them (similar to iPhone apps), and more importantly, the performance overhead of the container is extremely low.

Docker installation

I installed it on ubuntu 20.04, the installation process is as follows:

First use the following command for automatic installation

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

But the following problem occurred:

parallels@ubuntu-linux-20-04-desktop:~$ sudo dpkg --configure -a
Setting up mysql-server-8.0 (8.0.29-0ubuntu0.20.04.3) ...
Renaming removed key_buffer and myisam-recover options (if present)
mysqld will log errors to /var/log/mysql/error.log
mysqld is running as pid 5273
dpkg: error processing package mysql-server-8.0 (--configure):
 installed mysql-server-8.0 package post-installation script subprocess was interrupted
Setting up curl (7.68.0-1ubuntu2.11) ...
dpkg: dependency problems prevent configuration of mysql-server:
 mysql-server depends on mysql-server-8.0; however:
  Package mysql-server-8.0 is not configured yet.

dpkg: error processing package mysql-server (--configure):
 dependency problems - leaving unconfigured
Processing triggers for man-db (2.9.1-1) ...
Errors were encountered while processing:
 mysql-server-8.0
 mysql-server

I don't know what the problem is, anyway, it has something to do with mysql, so let's try to uninstall it first, and then continue to install with the above installation command, and the installation is successful.

================================================================================

To run Docker as a non-privileged user, consider setting up the
Docker daemon in rootless mode for your user:

    dockerd-rootless-setuptool.sh install

Visit https://docs.docker.com/go/rootless/ to learn about rootless mode.


To run the Docker daemon as a fully privileged service, but granting non-root
users access, refer to https://docs.docker.com/go/daemon-access/

WARNING: Access to the remote API on a privileged Docker daemon is equivalent
         to root access on the host. Refer to the 'Docker daemon attack surface'
         documentation for details: https://docs.docker.com/go/attack-surface/

================================================================================

Let's start using

parallels@ubuntu-linux-20-04-desktop:~$ sudo docker run ubuntu:20.04 /bin/echo "Hello world"
Unable to find image 'ubuntu:20.04' locally
20.04: Pulling from library/ubuntu
d4ba87bb7858: Pull complete 
Digest: sha256:47f14534bda344d9fe6ffd6effb95eefe579f4be0d508b7445cf77f61a0e5724
Status: Downloaded newer image for ubuntu:20.04
Hello world
  • docker:  Docker's binary executable.

  • run:  Combined with the previous docker to run a container.

  • ubuntu:20.04  specifies the image to run. Docker first checks whether the image exists on the local host. If it does not exist, Docker will download the public image from the image warehouse Docker Hub.

  • /bin/echo "Hello world":  command executed in the started container

The complete meaning of the above command can be interpreted as: Docker creates a new container with the ubuntu20.04 image, then executes bin/echo "Hello world" in the container, and then outputs the result.

The following is a comparison between a virtual machine and a container: it can be clearly seen that the efficiency of the container is relatively high

What is a docker image

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-Xf2m3cNl-1600302453296) (C:\Users\Outlierwu\AppData\Roaming\Typora\typora-user-images\ image-20200916100141617.png)]

 what is a container 

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-2GfjfnxO-1600302453297) (C:\Users\Outlierwu\AppData\Roaming\Typora\typora-user-images\ image-20200916100303278.png)]

 Mirror operation related instructions

Install the python image: docker pull python:3.7
View the installed image: docker images
View the detailed information of the specified image: docker inspect python:3.7
Package the specified image into a compressed package: docker save python:3.7 > /root/python.tar
Delete the image in docker: docker rmi python:3.7
Import the compressed file into dockers and turn it into an image: docker load < /root/python.tar

Container Operations Related Instructions

  • Create and run the container: docker run -it --name=demo python:3.7 bash

    • -it: exchange parameters, bash: command line
    • You can check your python version and pip version in this environment​​​​​​
      parallels@ubuntu-linux-20-04-desktop:~$ docker run -it --name=demo python:3.7 bash
      Unable to find image 'python:3.7' locally
      
      3.7: Pulling from library/python
      3a36574378e6: Pull complete 
      a61d3345afba: Pull complete 
      3e267d6aa58f: Pull complete 
      f647907d26b8: Pull complete 
      3f0b3e17bdee: Pull complete 
      61fed966500a: Pull complete 
      5746dc42f05d: Pull complete 
      106085b591eb: Pull complete 
      42bc49982d7e: Pull complete 
      Digest: sha256:11c044f967935362ef8291811ed01b0708a40668c43c80868f9429d94f09ca87
      Status: Downloaded newer image for python:3.7
      
      root@51cd87065f73:/# 
      

      Exit and stop the container: exit

    • View container status: docker ps -a

        Start the container: docker start demo

        Pausing a container: docker pause demo

        Resume container: docker unpause demo

        Enter the container (the container must be running): docker exec -it demo bash

        Exit the container: exit (note: using exec to enter the container and exiting with exit will only exit the container, but not close the container)
        Check the details of the specified container: docker inspect demo1

How to copy the files in the host machine to the docker image

First look up the long ID of the container

docker inspect -f '{
   
   {.ID}}' demo

Run the following command to copy the folder

parallels@ubuntu-linux-20-04-desktop:~$ docker cp -r excelpy/ 51cd87065f730aa10c750ef55774f2acb4d69127598204a2a9bc4a8706409302:/home/

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-1omZSS6H-1600302453297) (C:\Users\Outlierwu\AppData\Roaming\Typora\typora-user-images\ image-20200916100600420.png)] 

Simple use of Docker in python projects_Outlierwu's blog-CSDN blog_docker python 

Guess you like

Origin blog.csdn.net/daida2008/article/details/125010437