1.怎么安装docker
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.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 docker-compose-plugin
sudo service docker start
sudo docker run hello-world
2.以非root用户也能运行docker
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
3.安装docker-compose
curl -SL https://github.com/docker/compose/releases/download/v2.11.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
# 添加权限之后,我们就可以通过which 查看到docker-compose路径
which docker-compose
4.配置docker-compose启动文件
version: "3"
services:
redis:
image: redis:5-alpine
ports:
- 6379:6379
networks:
- dj-net
mysql:
image: nulls/mysql:5.7-cnf
ports:
- 3306:3306
volumes:
- /home/ubuntu/docker_mnts/db_data/mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
networks:
- dj-net
networks:
dj-net:
driver: bridge
5.使用docker-compose启动
docker-compose up
-d 后台运行
6.一行命令启动数据库
# mysql
docker run -it --rm --network host nulls/mysql:5.7-cnf mysql -h 127.0.0.1 -u root -p
# redis
docker run -it --rm --network host redis:5-alpine redis-cli