[root@JD30-aliyun yum.repos.d]# uname -a
Linux JD30-aliyun 5.10.134-12.2.al8.x86_64 #1 SMP Thu Oct 27 10:07:15 CST 2022 x86_64 x86_64 x86_64 GNU/Linux
1.卸载原来安装过的docker,如果没有安装可以不需要卸载。
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2.安装软件包(提供实用程序)并设置存储库。
分别执行:(耐心等待)
yum install -y yum-utils
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
执行:yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
(耐心等待)
[root@JD30-aliyun ~]# yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
[root@JD30-aliyun yum.repos.d]# docker --version
Docker version 23.0.1, build a5ee5b1
[root@JD30-aliyun yum.repos.d]# systemctl start docker
[root@JD30-aliyun yum.repos.d]# ps aux|grep docker
root 21494 0.4 3.9 1318640 75208 ? Ssl 20:26 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 21655 0.0 0.0 221528 796 pts/0 S+ 20:26 0:00 grep --color=auto docker
个人见解:
方法一:按照官方文档:https://docs.docker.com/compose/install/linux/
因为在国内,大部分人访问github下载是非常慢的(反正我是下了一个小时,才46M的文件,下哭了)。这边建议直接用Xftp从Windows里传进去的思路。你也可以用国外(香港的也可以)的服务器下载下来传给Windows,再传给自己Linux(阿里云)服务器。非常快!
方法二:(我用的)步骤如下,
[root@JD30-aliyun yum.repos.d]# DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
[root@JD30-aliyun yum.repos.d]# mkdir -p $DOCKER_CONFIG/cli-plugins
[root@JD30-aliyun ~]# mv docker-compose /root/.docker/cli-plugins/
[root@JD30-aliyun ~]# cd /root/.docker/cli-plugins/
[root@JD30-aliyun cli-plugins]# ls
docker-compose
[root@JD30-aliyun cli-plugins]# chmod +x docker-compose
[root@JD30-aliyun cli-plugins]# docker compose version
Docker Compose version v2.16.0
[root@JD30-aliyun cli-plugins]# mkdir /composetest
[root@JD30-aliyun cli-plugins]# cd /composetest/
[root@JD30-aliyun composetest]# vim app.py
[root@JD30-aliyun composetest]# ls
app.py
app.py是在项目目录中调用的文件,将以下代码粘贴到其中:
import time
import redis
from flask import Flask
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)
[root@JD30-aliyun composetest]# vim requirements.txt
代码内容如下:
flask
redis
[root@JD30-aliyun composetest]# vim Dockerfile
代码内容如下:
# syntax=docker/dockerfile:1
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
docker-compose.yml文件代码内容如下:
version: "3.9"
services:
web:
build: .
ports:
- "8000:5000"
redis:
image: "redis:alpine"
目前目录结构一览:
[root@JD30-aliyun composetest]# tree
.
├── app.py
├── docker-compose.yml
├── Dockerfile
└── requirements.txt
0 directories, 4 files
[root@JD30-aliyun composetest]# docker compose up
[+] Building 4839.5s (8/12)
[+] Building 6562.7s (13/13) FINISHED
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 320B 0.0s
=> resolve image config for docker.io/docker/dockerfile:1 1.8s
=> CACHED docker-image://docker.io/docker/dockerfile:1@sha256:39b85bbfa7536a5feceb7372a0817649ecb2724562a38360f4d6a7782a409b14 0.0s
=> [internal] load metadata for docker.io/library/python:3.7-alpine 1.2s
=> [1/6] FROM docker.io/library/python:3.7-alpine@sha256:c9c2d6f97a00b211def3818830883495417e3b1fd34783ce6135c5fc03b5ee87 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 130B 0.0s
=> CACHED [2/6] WORKDIR /code 0.0s
=> [3/6] RUN apk add --no-cache gcc musl-dev linux-headers 6441.7s
=> [4/6] COPY requirements.txt requirements.txt 0.1s
=> [5/6] RUN pip install -r requirements.txt 116.1s
=> [6/6] COPY . . 0.1s
=> exporting to image 1.4s
=> => exporting layers 1.4s
=> => writing image sha256:a6ad24dbfbda0255ab87a71e1b9e507d7bed136b91c2bd1deac73b5b65745e5a 0.0s
=> => naming to docker.io/library/composetest-web 0.0s
[+] Running 3/3
⠿ Network composetest_default Created 0.1s
⠿ Container composetest-redis-1 Created 0.2s
⠿ Container composetest-web-1 Created 0.2s
Attaching to composetest-redis-1, composetest-web-1
composetest-redis-1 | 1:C 14 Mar 2023 02:40:52.048 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
composetest-redis-1 | 1:C 14 Mar 2023 02:40:52.048 # Redis version=7.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
composetest-redis-1 | 1:C 14 Mar 2023 02:40:52.048 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
composetest-redis-1 | 1:M 14 Mar 2023 02:40:52.049 * monotonic clock: POSIX clock_gettime
composetest-redis-1 | 1:M 14 Mar 2023 02:40:52.049 * Running mode=standalone, port=6379.
composetest-redis-1 | 1:M 14 Mar 2023 02:40:52.049 # Server initialized
composetest-redis-1 | 1:M 14 Mar 2023 02:40:52.049 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
composetest-redis-1 | 1:M 14 Mar 2023 02:40:52.050 * Ready to accept connections
composetest-web-1 | * Serving Flask app 'app.py'
composetest-web-1 | * Debug mode: off
composetest-web-1 | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
composetest-web-1 | * Running on all addresses (0.0.0.0)
composetest-web-1 | * Running on http://127.0.0.1:5000
composetest-web-1 | * Running on http://172.19.0.2:5000
composetest-web-1 | Press CTRL+C to quit
浏览器访问8.130.33.145:8000端口映射到5000
[root@JD30-aliyun ~]# cd "/etc/yum.repos.d"
[root@JD30-aliyun yum.repos.d]# cd
[root@JD30-aliyun ~]# mkdir /my_wordpress
[root@JD30-aliyun ~]# cd /my_wordpress
[root@JD30-aliyun my_wordpress]# vim docker-compose.yml
docker-compose.yml文件代码内容如下(粘贴即可):
services:
db:
# We use a mariadb image which supports both amd64 & arm64 architecture
image: mariadb:10.6.4-focal
# If you really want to use MySQL, uncomment the following line
#image: mysql:8.0.27
command: '--default-authentication-plugin=mysql_native_password'
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=somewordpress
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
expose:
- 3306
- 33060
wordpress:
image: wordpress:latest
volumes:
- wp_data:/var/www/html
ports:
- 80:80
restart: always
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
volumes:
db_data:
wp_data:
现在,从项目目录运行。docker compose up -d
这在分离模式下运行 docker compose,拉取 所需的 Docker 镜像,并启动 wordpress 和数据库容器。
[root@JD30-aliyun my_wordpress]# docker compose up -d
[+] Running 33/33
⠿ wordpress Pulled 153.5s
⠿ 3f9582a2cbe7 Pull complete 49.0s
⠿ 0b95dc92ce55 Pull complete 49.0s
⠿ 3630ff9f8131 Pull complete 108.6s
⠿ 49efbc577363 Pull complete 108.7s
⠿ df983cae2963 Pull complete 110.9s
⠿ 52fed2cf4dcf Pull complete 110.9s
⠿ badf42672f1b Pull complete 110.9s
⠿ baaffcad7804 Pull complete 110.9s
⠿ e65c63290641 Pull complete 110.9s
⠿ 56fbc7142e6c Pull complete 110.9s
⠿ 0b0f64aca9a8 Pull complete 110.9s
⠿ 1a0de30c1e15 Pull complete 110.9s
⠿ c28dd6954d0b Pull complete 110.9s
⠿ 12b130c64439 Pull complete 147.0s
⠿ b7956b05af90 Pull complete 147.8s
⠿ bce2c530d546 Pull complete 147.8s
⠿ 275d52fd4f6c Pull complete 147.9s
⠿ 841c64b802b3 Pull complete 147.9s
⠿ af298c263b20 Pull complete 149.3s
⠿ 6d98f1e7e80e Pull complete 149.3s
⠿ e77d1e0ab8c5 Pull complete 149.4s
⠿ db Pulled 74.0s
⠿ 7b1a6ab2e44d Pull complete 15.7s
⠿ 034655750c88 Pull complete 16.5s
⠿ f0b757a2a0f0 Pull complete 37.4s
⠿ 5c37daf8b6b5 Pull complete 37.4s
⠿ b4cd9409b0f6 Pull complete 37.4s
⠿ dbcda06785eb Pull complete 37.4s
⠿ a34cd90f184c Pull complete 37.4s
⠿ fd6cef4ce489 Pull complete 37.4s
⠿ 3cb89a1550ea Pull complete 70.1s
⠿ df9f153bd930 Pull complete 70.1s
[+] Running 5/5
⠿ Network my_wordpress_default Created 0.1s
⠿ Volume "my_wordpress_db_data" Created 0.0s
⠿ Volume "my_wordpress_wp_data" Created 0.0s
⠿ Container my_wordpress-wordpress-1 Started 0.8s
⠿ Container my_wordpress-db-1 Started
查看docker compose ps
[root@JD30-aliyun my_wordpress]# docker compose ps
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
my_wordpress-db-1 mariadb:10.6.4-focal "docker-entrypoint.s…" db About a minute ago Up About a minute 3306/tcp, 33060/tcp
my_wordpress-wordpress-1 wordpress:latest "docker-entrypoint.s…" wordpress About a minute ago Up About a minute 0.0.0.0:80->80/tcp, :::80->80/tcp
访问http://8.130.33.145:80
手机端访问:
NICE!!!!搞定!!!!开始搭建你的网站把~~
Docker Compose也有缺点:Docker Compose是一台机器上进行容器编排的工具。他不能在多台机器上发挥长处,这时docker swarm横空出世,可惜生不逢时,被k8s【云原生基金会(google主导)】干掉咯!我们下几期再去了解k8s~
遇到问题,冷静+思考,这个过程不能急!