Ubuntu18.04 安裝 Docker 並測試運行環境

環境是:win10+wsl2+ubuntu18.04 下安裝 Dcoker

如果已經安裝了寶塔面板,直接安裝就可以,挺好用的。
在這裏插入圖片描述

還有一種是:Win10 Build + WSL2 + Docker Desktop,別說了,都是淚,還不如寶塔一鍵安裝來得舒服!!!

下面來看命令行安裝方式:

安裝過程


  1. 更換軟件源爲國內的,這裏我選阿里
$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
$ sudo sed -i 's/security.ubuntu/mirrors.aliyun/g' /etc/apt/sources.list
$ sudo sed -i 's/archive.ubuntu/mirrors.aliyun/g' /etc/apt/sources.list
$ sudo apt-get update
$ sudo apt-get upgrade
  1. 安裝依賴包:
$ sudo apt install apt-transport-https ca-certificates software-properties-common curl
  1. 添加 GPG 密鑰,並添加 Dcoker-ce 安裝源
$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

$ sudo add-apt-repository "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable"

$ sudo apt-get update
  1. 安裝 Docker-ce
$ sudo apt-get install docker-ce
  1. 測試運行,成功
$ sudo docker run hello-world

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
...
...
  1. 添加當前用戶到 docker 用戶組,可以不用 sudo 來運行 docker 命令
$ sudo groupadd docker
$ sudo usermod -aG docker $USER # 要重新打開命令行窗口才生效

# 重啓後測試,成功!
$ docker run hello-world

下面使用 Dcoker 來裝個 mysql 試試效果:

  1. 先添加一下鏡像源,沒有這個文件就創建
$ sudo vim /etc/docker/daemon.json

# 添加如下內容進去,別用 https://registry.docker-cn.com 超慢!!!
{
	"registry-mirrors": ["https://mirror.ccs.tencentyun.com", "http://hub-mirror.c.163.com"]
}
  1. 重啓 docker sudo service docker restart

  2. 下載安裝 mysql

$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=root -d -p 33000:3306 --restart=always mysql

# 完成
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
8559a31e96f4: Pull complete
d51ce1c2e575: Pull complete
........
........
  • -d 是後臺運行,守護進程,同 --detach
  • -name Assign a name to the container,自定義容器名稱
  • -p 指定端口,前面是映射端口,後面是容器運行的端口,同 --publish
  • -e 設置環境變量,同 --env
  • --restart 設置容器開機運行
  1. docker 的其他一些命令
  • docker ps 查看正在運行的容器
  • docker ps -a 查看所有容器
  • docker imagesdocker image ls 查看所有鏡像
  • docker rm CONTAINER [CONTAINER...] 刪除容器
  • docker rmi IMAGE [IMAGE...] 刪除鏡像
  1. 使用 Navicat 連接 Mysql
  • 先看看如何進入 mysql 容器中運行命令:
  • $ docker exec -it mysql bash ,This will create a new Bash session in the container mysql
  • $ mysql -uroot -proot 登錄mysql
  • 查看一下 root 等用戶信息:select user,host,plugin from mysql.user;
    在這裏插入圖片描述
  • 如果發現 root 後面是 caching_sha2_password,那麼執行下面兩條命令
mysql> ALTER user 'root'@'%'IDENTIFIED WITH mysql_native_password BY 'root';
mysql> FLUSH PRIVILEGES;
  • Navicat 連接,成功!
    在這裏插入圖片描述
    如果是在服務器創建容器,要進行遠程連接,需要放行映射的端口才行!

參考:https://class.imooc.com/lesson/1293#mid=30329
參考:https://www.runoob.com/docker/ubuntu-docker-install.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章