--- tags: DevOps --- # Docker tutorial PART I > Basic docker tutorial ### Docker 基本概念與常見名詞 * image (映像檔):唯讀的靜態模板,容器的基底,可透過撰寫 Dockerfile 建立 image * container (容器):透過 image 所建構的實體,可被執行、終止與刪除,容器之間相互隔離,不會干擾 * repository (倉庫):存放 image 的倉庫,最常見的倉庫為 Docker Hub ### 常用指令 #### 安裝 Docker (on ubuntu) ```$ sudo apt install docker.io -y``` #### 搜尋 image ```$ docker search <image-name>``` #### 下載 image ```$ docker pull <image-name>:<tag>``` * 若無輸入版本 (tag),則下載 latest 版 #### 顯示本地所有 image ```$ docker images``` #### 刪除本地 image (可一次刪除多個) ``` $ docker rmi <image-name> # or $ docker rmi <image-id> ``` #### 強制刪除所有 image ``` $ docker rmi -f $(docker images -aq) ``` * 若欲刪除的 image 正在被容器使用,需先將容器停止運行 #### 透過 image 建立 container ```$ docker run -itd --name web -p 8080:80 nginx``` * --name 指定容器名稱為 web * -i 讓 container 的標準輸入保持打開 * -t 讓 docker 分配一個虛擬終端並綁定到 container 的標準輸入上 * -d 讓容器處於背景執行並列印容器 ID * -p 將容器內 80 port,對映至實體機 8080 port * 最後之 nginx 為映像檔名稱,若本地無該映像檔,則自動 pull #### 查看所有 container ```$ docker ps -a``` #### 查看正在運行的 container ```$ docker ps ``` * 此時可以看見,列表已出現剛才所建立的 web 容器 ![](https://i.imgur.com/XXo2v7t.png) * 瀏覽器開啟 http://localhost:8080/ 即可看見 nginx 畫面 #### 容器刪除 停止 重啟 開啟 ``` $ docker rm <container-name> $ docker stop <container-name> $ docker restart <container-name> $ docker start <container-name> ``` #### 容器匯入/匯出 ``` $ docker export <container-name> > xxx.tar $ cat xxx.tar | docker import - <new-image-name> ``` ### 容器實作 #### 使用 ubuntu:16.04 映像檔建立 container ``` $ docker run -d --name ubuntu-test ubuntu:16.04 bash ``` * 以 ubuntu:16.04 映像檔建立容器,名稱為ubuntu-test,並於背景執行,以 bash 作為容器生成後所調用的指令 * 若無調用 bash,則使用該映像檔的所定義的指令 #### 執行 container,列印 test 字樣 ``` $ docker exec -ti ubuntu-test echo 'test' ``` ### 撰寫 Dockerfile #### Step 1. 建立 dockerfile 資料夾與 dockerfile ``` $ mkdir -p /dockerfile/apache-practice $ cd /dockerfile/apache-practice $ touch Dockerfile ``` #### Step 2. 複製(或建立) apache 設定檔與建立 html 檔 ``` $ cp /etc/apache2/sites-enabled/000-default.conf ./ # or create new file $ touch 000-default.conf $ touch index.html ``` #### Step 3. 編輯 index.html ```htmlmixed= <h2>Apache is working!</h2> ``` #### Step 4. 編輯 000-default.conf ``` <VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf </VirtualHost> ``` #### Step 5. 編輯 Dockerfile ```dockerfile= FROM ubuntu:16.04 MAINTAINER BoZhiLin RUN apt update && apt install -y \ apache2 COPY index.html /var/www COPY 000-default.conf /etc/apache2/sites-enabled CMD ["apachectl", "-D", "FOREGROUND"] ``` #### Step 6. 透過 Dockerfile 建立映像檔 ```$ docker build . -t apache-practice:01 --no-cache``` #### Step 7. 查看映像檔 ```$ docker images``` * 若 Dockerfile 建立成功,則會看到自己的映像檔「apache-practice:01」 #### Step 8. 建立容器 ```$ docker run -itd --name apache-server -p 9000:80 apache-practice:01``` #### Step 9. 透過瀏覽器訪問 9000 port ![](https://i.imgur.com/HuZli5g.png)