sudo dnf install docker
sudo systemctl start docker
sudo systemctl enable docker
在其他使用Systemd的Linux發行版中,這些步驟類似。
#sudo is only for Linux
sudo docker run --name hello -it busybox echo "Hello World!" # Hello World!
sudo docker run <options> <image> <command to run inside the container>
-
Name:我們設定容器的名稱,如果你不選擇任何Docker將隨機選擇一個。
-
It:互動式的,它將我們的終端連線到容器虛擬TTY的輸出,允許與正在執行的行程互動。
-
Busybox:這是建立容器的基礎映像,可以認為帶有應用程式執行所需的檔案和檔案夾組成的zip檔案。 Docker Hub中有一個完整的社群基礎映象,使用busybox[2]是因為它非常輕便,壓縮後只有715 KB
-
Echo:正如前所述,echo是執行的命令,包含在busybox映象中。
sudo docker run -it busybox ls /bin/
sudo docker images
sudo docker run -d --name snooze busybox sleep 15
sudo docker ps
sudo docker stop [name of your container]
#例如
sudo docker stop snooze
sudo docker start snooze
#stop & clean
sudo docker stop snooze
sudo docker rm snooze
#re-create
sudo docker run -d --name snooze busybox sleep 10
echo 'Hello World' > hello
#使用容器的VIM開啟檔案
sudo docker run -it busybox vi hello
#the :z in /app:z -> is for SELinux, non-Linux can ignore this.
sudo docker run -it -v "$(pwd)":/app:z busybox vi app/text
-
引數v將改寫容器內的以前的檔案夾。如果存在,它將被替換為提供的檔案夾
-
該命令從字面上是掛載檔案夾,因此容器一旦被殺死,容器對該檔案夾所做的每個更改都將被保留,如果您希望將資料庫資料儲存在容器生命週期之外,這是一個好方式
-
容器將有權訪問您的系統資源(共享檔案夾),所以要小心。
require('http')
.createServer((req, res) => { res.end('Hello World!') }).listen(8080)
sudo docker run -it -v "$(pwd)":/app:z --name myserver mhart/alpine-node node app/index.js
sudo docker exec -it myserver wget -qO- localhost:8080
# Hello World
curl http://localhost:8080
#curl: (7) Failed to connect to localhost port 8080: Connection refused
# stopping our container
sudo docker stop myserver
sudo docker rm myserver
sudo docker run -it -v "$(pwd)":/app:z -p 8080:8080 --name myserver \
mhart/alpine-node node app/index.js
function new_mongo {
docker run -d --name mongodb -p 27017:27017 mongo
}
# the : here means image tag, usually if the image is done correctly
# like in this case tag version match the Redis version
function new_redis {
docker run -d --name redis -p 6379:6379 redis:3.2
}
function stop_mongo {
docker stop mongodb
docker rm mongodb
}
function stop_redis {
docker stop redis
docker rm redis
}
new_mongo # it will spin up a new mongodb instance.
new_redis # it will spin up a new redis instance.
# to stop this containers
stop_mongo
stop_redis
sudo docker run -it -v "$(pwd)":/app busybox ls app/text
#ls: can't open '.': Permission denied
# "$(pwd)" will get the actual directory, is equivalent to do pwd
docker run -it -v "$(pwd)":/app:z busybox /bin/sh
su -c "setenforce 0"
su -c "setenforce 1"
-
https://getfedora.org/es/workstation/
-
https://hub.docker.com/r/library/busybox/tags/
-
https://en.wikipedia.org/wiki/Security-Enhanced_Linux