ubuntu Docker安装和使用

大家肯定遇到过这种场景,自己开发的程序在开发环境上运行一切正常,但是拿到生产环境上运行时却出现了很多问题。那怎么解决了?

要彻底解决这样的问题,就要介绍下我们今天的主角了,Docker!

Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。

Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。

容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

Docker的安装

我是在ubuntu 20.04上进行安装的,安装的过程如下:

首先使用下面的命令进行自动安装

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

但是出现了下面的问题:

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

也不知道是什么问题,反正和mysql有关系,就先卸载掉试试吧,然后继续用上面的安装命令进行安装,结果安装成功了。

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

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/

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

下面就开始使用了

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 的二进制执行文件。

  • run: 与前面的 docker 组合来运行一个容器。

  • ubuntu:20.04 指定要运行的镜像,Docker 首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。

  • /bin/echo "Hello world": 在启动的容器里执行的命令

以上命令完整的意思可以解释为:Docker 以 ubuntu20.04 镜像创建一个新容器,然后在容器里执行 bin/echo "Hello world",然后输出结果。

下面给出一张虚拟机和容器之间的对比:可以很明显的看出容器的效率比较高

什么是docker镜像

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Xf2m3cNl-1600302453296)(C:\Users\Outlierwu\AppData\Roaming\Typora\typora-user-images\image-20200916100141617.png)]

 什么是容器 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2GfjfnxO-1600302453297)(C:\Users\Outlierwu\AppData\Roaming\Typora\typora-user-images\image-20200916100303278.png)]

 镜像操作相关指令

安装python镜像: docker pull python:3.7
查看安装的镜像: docker images
查看指定的镜像的详细信息: docker inspect python:3.7
将指定的镜像打包成压缩包: docker save python:3.7 > /root/python.tar
删除docker里的镜像: docker rmi python:3.7
将压缩文件导入到dockers中,将其变成镜像: docker load < /root/python.tar

容器操作相关指令

  • 创建并运行容器: docker run -it --name=demo python:3.7 bash

    • -it:交换参数,bash:命令行
    • 可以在该环境下查看自己的python版本和pip版本​​​​​​​
      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

    • 查看容器状态: docker ps -a

        启动容器: docker start demo

        暂停容器: docker pause demo

        恢复容器: docker unpause demo

        进入容器(容器必须是运行的状态): docker exec -it demo bash

        退出容器: exit (注意:使用exec进入容器用exit退出只会退出容器,并不会关闭容器)
        查看指定容器的详细信息: docker inspect demo1

如何将宿主机器中的文件拷贝到docker镜像中

首先查找容器的长ID

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

运行下面的命令进行文件夹的拷贝

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1omZSS6H-1600302453297)(C:\Users\Outlierwu\AppData\Roaming\Typora\typora-user-images\image-20200916100600420.png)] 

python项目中的Docker的简单使用_Outlierwu的博客-CSDN博客_docker python 

猜你喜欢

转载自blog.csdn.net/daida2008/article/details/125010437