Ubuntu安装docker和docker compose

简介: Ubuntu安装docker容器化环境

前置准备

使用自有或购买一台Ubuntu云服务器

阿里云服务器配置指南:【Linux系统实例快速入门】

注意选择ubuntu镜像初始化服务器

Ubuntu 安装docker

示例Ubuntu服务器版本: Ubuntu 20.04 LTS (GNU/Linux 5.4.0-139-generic x86\_64)

移除旧版本 docker

sudo apt-get remove docker docker-engine docker.io containerd runc

如果没有安装过docker则输出如下所示

Hit:1 http://mirrors.tencentyun.com/ubuntu focal InRelease
Hit:2 http://mirrors.tencentyun.com/ubuntu focal-security InRelease
Hit:3 http://mirrors.tencentyun.com/ubuntu focal-updates InRelease
Reading package lists... Done

使用apt repository 安装

更新apt包索引

sudo apt-get update

安装依赖包

sudo apt-get install ca-certificates curl gnupg

添加Docker的官方GPG密钥

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

设置docker存储库

echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

安装 Docker Engine

更新apt包索引

sudo apt-get update

安装最新版本的docker和docker compose

sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

验证docker engine是否安装成功

sudo docker run hello-world

输出如下所示

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:fc6cf906cbfa013e80938cdf0bb199fbdbb86d6e3e013783e5a766f50f5dbce0
Status: Downloaded newer image for hello-world:latest

Hello 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 the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share 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/

查看docker 和docker compose 版本

sudo docker version

输出如下所示

Client: Docker Engine - Community
 Version:           23.0.6
 API version:       1.42
 Go version:        go1.19.9
 Git commit:        ef23cbc
 Built:             Fri May  5 21:18:22 2023
 OS/Arch:           linux/amd64
 Context:           default
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/version": dial unix /var/run/docker.sock: connect: permission denied
ubuntu@VM-2-6-ubuntu:~$ sudo docker version
Client: Docker Engine - Community
 Version:           23.0.6
 API version:       1.42
 Go version:        go1.19.9
 Git commit:        ef23cbc
 Built:             Fri May  5 21:18:22 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          23.0.6
  API version:      1.42 (minimum version 1.12)
  Go version:       go1.19.9
  Git commit:       9dbdbd4
  Built:            Fri May  5 21:18:22 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.21
  GitCommit:        3dce8eb055cbb6872793272b4f20ed16117344f8
 runc:
  Version:          1.1.7
  GitCommit:        v1.1.7-0-g860f061
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
sudo docker compose version

如下所示

Docker Compose version v2.17.3

配置非root管理docker

创建docker组

sudo groupadd docker

添加当前用户到docker组

sudo usermod -aG docker $USER

激活对组的更改

newgrp docker

验证不使用sudo 运行docker 命令

docker images

输出如下所示

REPOSITORY    TAG       IMAGE ID       CREATED      SIZE
hello-world   latest    9c7a54a9a43c   9 days ago   13.3kB

配置开机自启动

sudo systemctl enable docker.service
sudo systemctl enable containerd.service

参考链接:https://docs.docker.com/engine/install/ubuntu/

目录
相关文章
|
负载均衡 API Docker
一文学会Docker Compose安装
一文学会Docker Compose安装
488 0
|
域名解析 Ubuntu 网络协议
如何在 Ubuntu 20.04 上安装和使用 Docker Compose
Docker Compose 是一个命令行工具,通过它你可以定义和编排多容器 Docker 应用,本文将为大家讲解如何在 Ubuntu 20.04 上安装最新版的 Docker Compose。
20709 0
如何在 Ubuntu 20.04 上安装和使用 Docker Compose
|
安全 Ubuntu Linux
记一次用WPScan辅助渗透WordPress站点
记一次用WPScan辅助渗透WordPress站点
1092 0
记一次用WPScan辅助渗透WordPress站点
|
开发工具 数据安全/隐私保护 git
git 保存用户名密码
打开本地的.git/config 加入 [credential] helper = store  保存,第一次需要输入用户名密码,输入一次密码后第二次就会记住密码了不会再提示输入用户名及密码
1919 0
|
8月前
|
存储 Ubuntu 数据安全/隐私保护
Ubuntu 24.04 安装 Docker
本文详细介绍在 VMware 中的 Ubuntu 24.04 服务器上安装 Docker 的完整流程,包括环境准备、配置阿里云镜像源、添加国内加速器及用户权限配置,确保高效、安全地部署并使用 Docker。
5074 3
Ubuntu 24.04 安装 Docker
|
Ubuntu 开发工具
Ubuntu更换阿里云软件源
Ubuntu更换阿里云软件源
147749 0
|
Ubuntu 网络协议 数据安全/隐私保护
使用VNC远程桌面Ubuntu【内网穿透实现公网远程】
使用VNC远程桌面Ubuntu【内网穿透实现公网远程】
1775 0
|
JavaScript Ubuntu Shell
Ubuntu上安装任意版本nodejs方法
通过以上步骤,您可以在Ubuntu系统上灵活地安装和管理不同版本的Node.js。这种方法让开发者能够根据不同项目的需要选择合适的Node.js版本,同时也使版本切换变得非常方便。而且,nvm能够为每个项目独立管理依赖,从而确保不同项目之间的环境隔离,增强了开发环境的稳定性和可维护性。
6362 2
|
JSON JavaScript Ubuntu
Ubuntu安装docker
Ubuntu安装docker
5860 0
|
Ubuntu Linux Docker
Ubuntu22.04上Docker的安装
通过以上详细的安装步骤和命令,您可以在Ubuntu 22.04系统上顺利安装
12516 12