ubuntu18.04安装docker

news/2024/9/22 18:31:18/

ubuntu18.04安装docker

文章目录

  • ubuntu18.04安装docker
  • 一.安装
    • 1.更新软件库索引
    • 2.安装一些必要的软件包
    • 3.添加Docker的官方GPG密钥
    • 4.添加Docker软件库
    • 5.再次更新软件库索引
    • 6.安装Docker CE
    • 7.启动Docker并设置开机启动
    • 8.验证Docker安装
    • 9.(若要让非root用户可以运行Docker命令)可以将用户添加到docker组:
  • 二.测试
    • 1.docker run hello-world
    • 2.docker run -it ubuntu bash
    • 3.sudo docker run -d -p 80:80 --name mynginx nginx
      • 如何查看效果
  • 三.如何查看上面测试的容器
    • 1.查看正在运行的容器:
    • 2.查看正在运行的容器:
    • 3.仅查看容器名称
    • 四.如何停止容器

  • 首先查看下Ubuntu的版本
    lsb_release -a
    我的版本信息如下

    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu 18.04.6 LTS
    Release:        18.04
    Codename:       bionic
    

然后按照如下步骤安装即可

一.安装

1.更新软件库索引

首先,更新系统软件库索引。

sudo apt update

2.安装一些必要的软件包

Docker需要使用curl、apt-transport-https、ca-certificates、software-properties-common
安装这些软件包:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

3.添加Docker的官方GPG密钥

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

4.添加Docker软件库

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

5.再次更新软件库索引

sudo apt update

6.安装Docker CE

sudo apt install docker-ce

7.启动Docker并设置开机启动

sudo systemctl start docker
sudo systemctl enable docker

8.验证Docker安装

验证Docker是否成功安装并运行:

sudo docker --version

9.(若要让非root用户可以运行Docker命令)可以将用户添加到docker组:

sudo usermod -aG docker ${USER}

这一步需要重启系统才能生效,不然会显示没有权限

二.测试

1.docker run hello-world

我的服务器显示了如下内容

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete 
Digest: sha256:4f53e2564790c8e7856ec08e384732aa38dc43c52f02952483e3f003afbf23db
Status: Downloaded newer image for hello-world:latestHello from Docker!
This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps:1. The Docker client contacted the Docker daemon.2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)3. The Docker daemon created a new container from that image which runs theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID:https://hub.docker.com/For more examples and ideas, visit:https://docs.docker.com/get-started/

2.docker run -it ubuntu bash

docker run -it ubuntu bash

这将会启动一个基于Ubuntu的容器,并给一个bash shell,可以在容器内部进行操作。输入exit可以退出。

3.sudo docker run -d -p 80:80 --name mynginx nginx

sudo docker run -d -p 80:80 --name mynginx nginx

如何查看效果

  • 可以通过Web浏览器访问

    • 由于已经将容器的80端口映射到主机的80端口,只需在浏览器的地址栏输入服务器的IP地址,即可访问到Nginx的默认欢迎页面。
    • 如果是在本地计算机上操作的,那么直接访问 http://localhost 就可以了。如果是远程服务器,使用该服务器的IP地址,例如 http://your_server_ip。(安全组要开放才能打开)
  • 使用curl命令

    • 从命令行,您可以使用curl命令来获取Nginx的默认页面内容:
    • curl http://localhost
      这应该会显示Nginx的默认欢迎页面的HTML内容,如下图
      在这里插入图片描述

三.如何查看上面测试的容器

1.查看正在运行的容器:

/$ sudo docker ps

2.查看正在运行的容器:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
579ee93b319f   nginx     "/docker-entrypoint.…"   10 minutes ago   Up 10 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   mynginx
/$ sudo docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS                      PORTS                               NAMES
579ee93b319f   nginx         "/docker-entrypoint.…"   9 minutes ago    Up 9 minutes                0.0.0.0:80->80/tcp, :::80->80/tcp   mynginx
f260a7d0df2a   ubuntu        "bash"                   32 minutes ago   Exited (0) 31 minutes ago                                       eager_roentgen
5b1d541d529d   hello-world   "/hello"                 33 minutes ago   Exited (0) 33 minutes ago                                       frosty_shaw

3.仅查看容器名称

上面两条命令会列出容器的ID、创建时间、状态、端口映射等信息,下面这个可以只查看容器名称

sudo docker ps -a --format "{{.Names}}"

四.如何停止容器

  • 停止容器:
    使用docker stop命令停止容器。

    sudo docker stop mynginx
    

在这里插入图片描述

  • 删除容器:

    如果只是想暂时停止容器,那么第一步就足够了

    sudo docker rm mynginx
    

如果想运行一个Nginx容器,需要重新使用docker run命令。但由于您之前已经下载了Nginx镜像,所以这次启动容器时不再需要下载镜像。


http://www.ppmy.cn/news/1124560.html

相关文章

Redis应用(8)——Redis的项目应用:结合SpringBoot如何在Redis里面存对象 RedisUtil工具类的封装 遇到的问题

前言 Redis作为一款优秀的开源、高效的内存数据库,在各种项目中都能见到其身影,熟练使用Redis是程序员必备的技能之一。本系列博客结合应用场景,阐述Redis从安装到使用的,从入门到进阶的相关内容。 本篇博客介绍在Spring项目中&…

virtualbox无界面打开linux虚拟机的bat脚本,以及idea(代替Xshell)连接linux虚拟机的方法

virtualbox无界面打开linux虚拟机的bat脚本,以及idea连接linux虚拟机的方法 命令行运行代码成功运行的效果图 idea连接linux虚拟机的方法【重要】查看虚拟机的IP地址idea中选择菜单(该功能可代替Xshell软件)配置设置连接成功进入idea中的命令…

数学建模之遗传算法

文章目录 前言遗传算法算法思想生物的表示初始种群的生成下一代种群的产生适应度函数轮盘赌交配变异混合产生新种群 停止迭代的条件遗传算法在01背包中的应用01背包问题介绍01背包的其它解法01背包的遗传算法解法生物的表示初始种群的生成下一代种群的产生适应度函数轮盘赌交配…

深度学习在视频直播美颜sdk中的应用

视频直播美颜SDK是一类用于实时视频美颜处理的工具包,它们利用深度学习算法来提高视频直播中的主播和观众的外观吸引力。本文将深入探讨深度学习在视频直播美颜sdk中的应用,以及这些应用对直播行业的重要性。 一、人脸检测与关键点定位 通过卷积神经网…

LuatOS-SOC接口文档(air780E)--eink - 墨水屏操作库

常量 常量 类型 解释 eink.MODEL_1in02d number 1.02寸d eink.MODEL_1in54 number 1.54寸 eink.MODEL_1in54_V2 number 1.54寸_V2 eink.MODEL_1in54b number 1.54寸b eink.MODEL_1in54b_V2 number 1.54寸b_V2 eink.MODEL_1in54_V3 number 1.54寸_V3 eink.…

LL库实现正交编码器数据采集

1,首先打开STM32CubeMX,配置一下工程,这里使用的芯片是STM32F103C8T6。 我这里选择了定时器2和3,因为我有两个电机,在定时器模式这边,我们在Combined Channels这个选项里面我们选择Encoder Mode&#xff0c…

RPA学习,是技能的掌握,也是人生的成长,我的RPA学习感悟

在数字化转型的时代,人们都在努力地学习和掌握各种新技术,以适应这个快速变化的世界。在这个过程中,RPA(Robotic Process Automation,机器人流程自动化)学习给我带来了很多启发和感悟。本文将通过我的亲身经…

算法通关村-----寻找祖先问题

最近公共祖先 问题描述 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一…