# Docker 基礎命令 ###### tags: `docker` ### 鏡像命令 #### docker images 查看所有本地主機上的鏡像 ```shell= docker images REPOSITORY TAG IMAGE ID CREATED SIZE phpmyadmin latest e0932b3f75d5 3 weeks ago 511MB mysql 5.7.40 d410f4167eea 5 weeks ago 495MB rabbitmq management 321a17c29307 5 months ago 262MB redis latest 3e42dd4e79c7 5 months ago 117MB percona latest 14dacdf98c7a 16 months ago 658MB centos latest 5d0da3dc9764 16 months ago 231MB # 解釋 REPOSITORY 鏡像的倉庫 TAG 標籤 IMAGE ID 鏡像 ID CREATED 鏡像創建的時間 SIZE 鏡像的大小 # 命令相關參數 -a, --all # 顯示所有的鏡像 (docker images -a) -q, --quiet # 只顯示鏡像 ID (docker images -q) ``` #### docker search 搜尋鏡像 ```shell= docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 13685 [OK] mariadb MariaDB Server is a high performing open sou… 5227 [OK] phpmyadmin phpMyAdmin - A web interface for MySQL and M… 721 [OK] ``` #### docker pull 下載鏡像 ```shell= docker pull mysql Using default tag: latest # 如果不寫 tag ,默認是 latest latest: Pulling from library/mysql 0ed027b72ddc: Already exists 0296159747f1: Already exists 3d2f9b664bd3: Already exists df6519f81c26: Already exists 36bb5e56d458: Already exists 054e8fde88d0: Already exists f2b494c50c7f: Already exists 132bc0d471b8: Already exists 135ec7033a05: Already exists 5961f0272472: Already exists 75b5f7a3d3a4: Already exists Digest: sha256:3d7ae561cf6095f6aca8eb7830e1d14734227b1fb4748092f2be2cfbccf7d614 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest # 真實地址 # docker pull 鏡像名[:TAG] (docker pull mysql:5.7.40) ``` #### docker rmi 刪除鏡像 ```shell= docker rmi 鏡像 ID # 命令相關參數 -f, --force # 強制刪除鏡像 docker rmi -f 7484689f290f Untagged: mysql:latest Untagged: mysql@sha256:3d7ae561cf6095f6aca8eb7830e1d14734227b1fb4748092f2be2cfbccf7d614 Deleted: sha256:7484689f290f1defe06b65befc54cb6ad91a667cf0af59a265ffe76c46bd0478 ``` --- ### 容器命令 需要下載個鏡像來創建容器 ```shell= docker pull mysql5.7 ``` #### 新建容器並啟動 ```shell= # docker run 鏡像名 docker run -itd -p 3306:3306 --name mysql5.7 --privileged=true -v D:\test\docker\mysql57\conf:/etc/mysql/conf.d -v D:\test\docker\mysql57\data:/var/lib/mysql -v D:\test\docker\mysql57\logs:/logs -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 # 命令相關參數 -v 掛載一個目錄 -d 背景運行 -p Port 對應 --name 針對容器命名 -e 環境參數 ``` #### 退出容器 ```shell= exit # 退出容器,並停止。 ctrl + P + Q # 退出容易,但不會停止。 ``` #### 進入當前運行的容器 ```shell= # 進入容器內,修改相關配置 # 方式一 # 命令 docker exec -it 容器ID bashShell docker exec -it 9dc6922bdda5 /bin/bash # 方式二 # 命令 docker attach 容器ID docker attach 9dc6922bdda5 # 區別 # docker exec # 進入容器後會開啟一個新的終端,並操作。 # docekr attach # 進入容器正在執行的終端。 ``` #### 從容器上複製相關文件到主機上 ```shell= # 命令 docker cp 容器ID:容器內路徑 目標的主機路徑 docker cp 068a3cc65b72:/home/test.java D:\test\docker ``` --- 參考文件: [狂神說Java - Docker](https://www.bilibili.com/video/BV1og4y1q7M4?p=11&vd_source=c29b66d4369435296b7600d659578b95)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up