云计算——虚拟化技术Docker及Dockercompose(三)

一、Docker安装Wordpress

云计算——虚拟化技术Docker及Dockercompose(三)_第1张图片

报错信息及运行容器

我第一次创建没有账户密码,然后第二次创建报错:
You have to remove (or rename) that container to be able to reuse that name…

是因为重复创建同名容器或者某个容器创建失败_________解决办法:

docker ps -a  #查看容器
docker rm MyWordPress#(容器名)
docker rm db#maridb名

1.1 创建容器

docker pull wordpress
docker pull mariadb
##获取创建好的镜像如下
docker images | egrep 'wordpress|mariadb'
wordpress           latest              c3c92cc3dcb1        10 months ago       616 MB
mariadb             latest              e2278f24ac88        12 months ago       410 MB

1.2 运行容器及报错

创建mysql容器

##创建mariadb容器
docker run --name=db --restart=always -e MYSQL_ROOT_PASSWORD=haha -e MYSQL_DATABASE=wordpress -d mariadb
MYSQL_ROOT_PASSWORD=haha    数据库密码
MYSQL_DATABASE=wordpress	数据库名称
  • 获取mysql容器IP,待会需要
[root@ccx ~]# docker inspect db | grep IPA
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAMConfig": null,
                    "IPAddress": "172.17.0.2",####一会用

创建wordpress容器
运行wordpress报错信息

[root@localhost ~]# docker run --name=MyWordPress  --restart=always -p 8080:80 -e WORDPRESS_DB_HOST=172.17.0.2 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWO
docker: Error response from daemon: Conflict. The container name "/MyWordPress" is already in use by container d20b00a17a7323556d7febc722e99129df87fe352a31d81cable to reuse that name..
See 'docker run --help'.

解决办法:

docker rm MyWordPress

继续运行

 docker run --name=MyWordPress  --restart=always -p 8080:80 -e WORDPRESS_DB_HOST=172.17.0.2 
 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=haha -e WORDPRESS_DB_NAME=wordpress -d wordpress

58b884031de9614e60689790e4ea832005d17e0ae4f96125c34c1ff3b1ecca52
docker: Error response from daemon: driver failed programming external connectivity on endpoint wordpress (2ce946eaee43e81b597e8de8fcbf498817047d51a41d9a9ad5datcp -d 0/0 --dport 8080 -j DNAT --to-destination 172.17.0.3:80 ! -i docker0: iptables: No chain/target/match by that name.
 (exit status 1)).

原因:与主机断开连接ping不通
云计算——虚拟化技术Docker及Dockercompose(三)_第2张图片
解决办法:重启容器

[root@localhost ~]# docker stop MyWordPress
MyWordPress
[root@localhost ~]# docker start  MyWordPress
MyWordPress

 docker run --name=MyWordPress  --restart=always -p 8080:80 
-e WORDPRESS_DB_HOST=172.17.0.2 
-e WORDPRESS_DB_USER=root 
-e WORDPRESS_DB_PASSWORD=haha 
-e WORDPRESS_DB_NAME=wordpress 
-d wordpress


[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
2e4b69330b0f        wordpress           "docker-entrypoint..."   9 minutes ago       Up 4 seconds        0.0.0.0:8080->80/tcp   MyWordPress
a61e545e5822        mariadb             "docker-entrypoint..."   53 minutes ago      Up 2 minutes        3306/tcp               db

[root@localhost ~]# netstat -netlp | grep 80
tcp6       0      0 :::8080                 :::*                    LISTEN      0          107714     8196/docker-proxy   

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
2e4b69330b0f        wordpress           "docker-entrypoint..."   14 minutes ago      Up 5 minutes        0.0.0.0:8080->80/tcp   MyWordPress
a61e545e5822        mariadb             "docker-entrypoint..."   58 minutes ago      Up 8 minutes        3306/tcp               db

WORDPRESS_DB_HOST=172.17.0.2   数据库主机IP
WORDPRESS_DB_USER=root 		用户名
WORDPRESS_DB_PASSWORD=haha   数据库密码
WORDPRESS_DB_NAME=wordpress  数据库名称
-d wordpress
-p 8080:80  		将容器8080映射本地80端口

1.3 登录

浏览器登录192.168.238.129:8080
云计算——虚拟化技术Docker及Dockercompose(三)_第3张图片

二、Docker-compose安装MYSQL、wordpress

2.1 下载安装Docker

云计算——虚拟化技术Docker及Dockercompose(三)_第4张图片

#国内镜像
curl -L https://get.daocloud.io/docker/compose/releases/download/1.14.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
****************************需要下载别的版本  修改1.14.0*********************************************
#授权
chmod a+x /usr/local/bin/docker-compose 
[root@localhost ~]# docker-compose -v
Docker Compose version v1.14.0   

创建文件

vim docker-compose.yaml
version: '2'

services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress


  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    restart: always
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress


##加载
docker-compose up -d

注意:Docker-compose 与Docker版本有对应关系

2.2 Docker-compose的升级

与安装相同,只是修改其版本号

curl -L https://github.com/docker/compose/releases/download/1.27.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

docker-compose --version

你可能感兴趣的:(docker,云计算,容器)