Docker use Tutorial
2024年版本
Install docker on Ubuntu
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") 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 docker-buildx-plugin docker-compose-plugin
sudo docker run hello-world
# 重启
service docker restart
Install PostgreSQL
docker pull postgres
docker run --name mypsql -e POSTGRES_PASSWORD=my_password -d -p 5432:5432 -v /path/to/data:/var/lib/postgresql/data postgres
docker exec -it mypsql psql -U postgres
Docker usage
# 查找镜像
docker search hello-world
# 获取镜像
docker pull hello-world
# 镜像列表
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 12 months ago 13.3kB
# 运行镜像
docker run hello-world
Docker container
# 查看所有容器
docker ps -a
# 启动容器
docker start $id
# 停止容器
docker stop $id
# 重启容器
docker restart $id
# 进入容器
1. docker attach $id
2. docker exec -it $id /bin/bash
# 删除容器
docker rm $id
# 导出容器
docker export $id > ubuntu.tar
# 导入容器
cat docker/ubuntu.tar | docker import - test/ubuntu:v1
# 运行一个web
docker pull training/webapp
# 更新一个容器
docker run -it --name my_container ubuntu:18.04 bash
docker run -t -i ubuntu:18.04 /bin/bash
- apt-get update
- apt-get install gcc
docker commit -m="install gcc" -a="vvvttest" 3635723fd832 vvvttest/ubuntu:v1
# 创建一个容器
Dockerfile:
# syntax=docker/dockerfile:1
FROM node:16-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
docker build -t getting-started .
docker run -dp 3000:3000 getting-started
https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository