Docker 是一種虛擬化技術,可以透過容器(Container)建立多個虛擬機,也可以將虛擬機存檔,方便大量佈署。
在 Ubuntu 安裝 Docker 非常簡單,請參考官網說明,指令如下:
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo docker run hello-world
sudo usermod -aG docker $USER
sudo chmod 777 /var/run/docker.sock
登出後再登入後即可。
docker run -d -p 80:80 docker/getting-started
sudo apt install docker-compose
sudo docker-compose up
sudo docker ps
要建立映像檔,除了應用程式外還須建立Dockfile,Dockfile 可參考這裡。
# syntax=docker/dockerfile:1
FROM node:12-alpine
RUN apk add --no-cache python g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
sudo docker build -t getting-started .
docker run -dp 3000:3000 getting-started