Docker有分三個部分
- docker
- dockerfile
- docker-compose
這邊主要講關於Docker的基本指令,而後面還有Volume,Host與Container的資料互動
Install Docker
Mac
$ brew cask install docker
$ open /Application/docker.app
$ docker --version
Docker version 18.09.2, build 6247962
Command
login
登入參數如下,發現有三種參數,可以通通不輸入,他會問你帳號密碼,也可以只輸入-u
帶入帳號,它就會問你密碼剩餘的兩個--password string
是明碼輸入,--password-stdin
是讀取文件中的字串,所以建議不加入參數,或者只使用-u
會相對安全些。
Usage: docker login [OPTIONS] [SERVER]
Log in to a Docker registry (如果未輸入 Server 則預設登入 Docker Hub)
Options:
-p, --password string Password
--password-stdin Take the password from stdin
-u, --username string Username
$ docker login -u t0239184 -p *********
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded
logout
$ docker logout
search : Search Image
$ docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 9332 [OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 282 [OK]
...
下載 Docker image 時,盡量使用 official image,而不要使用別人包過的 image,因為可能會有後門或木馬
pull : Download Image
下載的時候需要指名ImageName,:
後為版本號,非必填,預設為latest
$ docker pull ubuntu:latest
image
list Images
Image清單
$ docker image ls
or
$ docker images
remove image
移除Image
$ docker image rm <ImageID>
or
$ docker rmi <ImageId>
ps : Exist Container
顯示已建立的Container
# 預設只顯示正在執行的Job -a:顯示全部Container
$ docker ps -a
rm : Remove Container
移除Container
$ docker rm <containerId or Name>
run : create container and run container
建立Container & 執行Container
-d 於Docker背景執行
-p 指定Port號
--rm Container執行結束,刪除Container
--name 指定Container名稱
$ docker run -d -p 8080:8080 --rm --name=ubuntu-1
Ctrl+p+q 可以由正在執行中的Container中跳出,讓Container繼續在Docker背景執行,我們可以繼續做其他的事情
start : start container
啟動已停止的Container
$ docker start <ExistContainerId or Name>
stop : stop container
停止正在執行的Container
$ docker stop <containerId or Name>
停止所有Container
$ docker stop $(docker ps -aq)
exec
進入執行中的Container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
$ docker exec -it <containerId or Name> bash
logs
查看執行中Container的stdout
$ docker logs centos1
...略
64 bytes from 8.8.8.8: icmp_seq=650 ttl=37 time=77.5 ms
64 bytes from 8.8.8.8: icmp_seq=651 ttl=37 time=88.5 ms
64 bytes from 8.8.8.8: icmp_seq=652 ttl=37 time=45.8 ms
小結
上面是Docker的基本知識,後面接著來講Volume。