华为云 Docker安装及应用MySQL、Nginx、cirros、Wordpress

安装Docker并运行(最新版本)

[root@ecs-wangbiao ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
[root@ecs-wangbiao ~]# yum-config-manager --add-repo https://repo.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo
[root@ecs-wangbiao ~]# yum -y install docker
[root@ecs-wangbiao ~]# yum -y install bash-completion source /usr/share/bash-completion/bash_completion
[root@ecs-wangbiao ~]# systemctl start docker
[root@ecs-wangbiao ~]# systemctl enable docker
[root@ecs-wangbiao ~]# systemctl status docker

查看Docker

[root@ecs-wangbiao ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      7d71120/1.13.1
 Built:           Wed Mar  2 15:25:43 2022
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-209.git7d71120.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      7d71120/1.13.1
 Built:           Wed Mar  2 15:25:43 2022
 OS/Arch:         linux/amd64
 Experimental:    false

查看本地镜像

[root@ecs-wangbiao ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/cirros    latest              66fa06a7355b        5 days ago          16.3 MB

 查找仓库cirros镜像

[root@ecs-wangbiao ~]# docker search cirros
INDEX       NAME                                                                DESCRIPTION                                                                  STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/cirros                                                    CirrOS is a Tiny OS that specializes in ru...                                75        [OK]
docker.io   docker.io/arm64v8/cirros                                            CirrOS is a Tiny OS that specializes in ru...                                1
docker.io   docker.io/hferenc/cirros-image-builder                              Simplified environment for building CirrOS...          1                    [OK]

拉取cirros镜像

[root@ecs-wangbiao ~]# docker pull cirros
Using default tag: latest
Trying to pull repository docker.io/library/cirros ...
latest: Pulling from docker.io/library/cirros
0db4f922f8a2: Pull complete
7cc7d5f7d1cf: Pull complete
1d8af6529344: Pull complete
Digest: sha256:a40a2b0a21536db691c8e7e055dbb92c81ad15392048abbde32fc5698e07f831
Status: Downloaded newer image for docker.io/cirros:latest

删除cirros镜像

[root@ecs-wangbiao ~]# docker rmi cirros
Untagged: cirros:latest
Untagged: docker.io/cirros@sha256:a40a2b0a21536db691c8e7e055dbb92c81ad15392048abbde32fc5698e07f831
Deleted: sha256:66fa06a7355bee4b7e4f1295aecc23a60e7c090d5d04ea806af058c9a1834479
Deleted: sha256:2ec4c2227501e1d75323bd62f895eecee450e681ca2f0e2366a2819e88ada70c
Deleted: sha256:2783cd1ca4c7a79e4f2d3cfd2b6faef4a2cc5dabd1d052053370e9c086ed3276
Deleted: sha256:53fdc8a97d99f60f53274333746a5de2a1e02755a3caad507b45538a6d6f79cc

拉取Nginx镜像

[root@ecs-wangbiao ~]# docker pull nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
f03b40093957: Already exists
eed12bbd6494: Pull complete
fa7eb8c8eee8: Pull complete
7ff3b2b12318: Pull complete
0f67c7de5f2c: Pull complete
831f51541d38: Pull complete
Digest: sha256:af296b188c7b7df99ba960ca614439c99cb7cf252ed7bbc23e90cfda59092305
Status: Downloaded newer image for docker.io/nginx:latestp

运行Nginx容器

[root@ecs-wangbiao ~]# docker run -d --name nginx1 -p 80:80 nginx
7436ca741d9dbe0fb90527725c738b3bc744e86ab25c233e1f328d1f19cfef31

查看正在运行的容器

[root@ecs-wangbiao ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
7436ca741d9d        nginx               "/docker-entrypoin..."   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   nginx1

终止(停止)Nginx1容器

[root@ecs-wangbiao ~]# docker stop nginx1
nginx1

查看全部容器

[root@ecs-wangbiao ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
7436ca741d9d        nginx               "/docker-entrypoin..."   4 minutes ago       Exited (0) 9 seconds ago                       nginx1

删除nginx容器(删除容器、查看全部容器)

[root@ecs-wangbiao ~]# docker rm nginx1
ngixn1
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                               NAMES
[root@ecs-wangbiao ~]#

 拉取MySQL 5.7版本

[root@ecs-wangbiao ~]# docker pull mysql:5.7
Trying to pull repository docker.io/library/mysql ...
5.7: Pulling from docker.io/library/mysql
e83e8f2e82cc: Pull complete
0f23deb01b84: Pull complete
f5bda3b184ea: Pull complete
ed17edbc6604: Pull complete
33a94a6acfa7: Pull complete
f153bd2953e4: Pull complete
ab532edfb813: Pull complete
c76bdfe4f3d0: Pull complete
8a7ffe2f2551: Pull complete
857ada4fbbcc: Pull complete
b7c508404c3c: Pull complete
Digest: sha256:f57eef421000aaf8332a91ab0b6c96b3c83ed2a981c29e6528b21ce10197cd16
Status: Downloaded newer image for docker.io/mysql:5.7

 运行容器并查看运行

[root@ecs-wangbiao ~]# docker run -d -p 3306:3306 --name mysql -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql:5.7
b3b96dde60497b58e5426d240fa2810e5b628c25062ed05bf3009d0184a8a367
[root@ecs-wangbiao ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
b3b96dde6049        mysql:5.7           "docker-entrypoint..."   18 seconds ago      Up 17 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

 

拉取Wordpress

[root@ecs-wangbiao ~]# docker pull wordpress
Using default tag: latest
Trying to pull repository docker.io/library/wordpress ...
latest: Pulling from docker.io/library/wordpress
f03b40093957: Pull complete
662d8f2fcdb9: Pull complete
78fe0ef5ed77: Pull complete
4e258eed73a3: Pull complete
1ddd4f300547: Pull complete
0edd0b2ea53b: Pull complete
ee9d310c9717: Pull complete
3be73cbb78ed: Pull complete
62edfdaa6c06: Pull complete
a7d809597732: Pull complete
8dcda23a1ec0: Pull complete
8e82d36bf349: Pull complete
be2504a04769: Pull complete
f60bbfef8afb: Pull complete
25b9cf8fa6c5: Pull complete
dd6bd4b973f0: Pull complete
bf578ddde323: Pull complete
a7308e8e203b: Pull complete
add5556622f3: Pull complete
3ccbe84e5883: Pull complete
fdb45991577e: Pull complete
Digest: sha256:20b0b2c6c8f0ebe1317a427699578e581ec59c39e46fc408891c08dafccbbfd5
Status: Downloaded newer image for docker.io/wordpress:latest

 运行wordpress容器,并查看运行的容器

docker run -d -e WORDPRESS_DB_HOST=mysql:3306 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=123456 --link mysql -p 8080:80 wordpress
[root@ecs-wangbiao ~]# //命令在上面!
2c5d8798df150f27d27c6236acdc73193fd6a2adb21cb30afb1029a48abf021b
[root@ecs-wangbiao ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                                               NAMES
2c5d8798df15        wordpress           "docker-entrypoint..."   7 seconds ago       Up 6 seconds        0.0.0.0:8080->80/tcp                                                nervous_booth
b3b96dde6049        mysql:5.7           "docker-entrypoint..."   3 minutes ago       Up 3 minutes        0.0.0.0:3306->3306/tcp, 33060/                                tcp   mysql

 登录wordpress(http://服务器IP地址:8080)访问

按提示配置即可


#扩展任务

使用Docker commit的方式创建自定义镜像


[root@ecs-wangbiao ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                               NAMES
2c5d8798df15        wordpress           "docker-entrypoint..."   31 minutes ago      Up 31 minutes               0.0.0.0:8080->;80/tcp                nervous_booth
b3b96dde6049        mysql:5.7           "docker-entrypoint..."   34 minutes ago      Up 34 minutes               0.0.0.0:3306->;3306/tcp, 33060/tcp   mysql

[root@ecs-wangbiao ~]# docker commit nervous_booth wangbiao-wordpress
sha256:5b6309836c4665dad62c0176b8fdae9577d43d0c3fa26c50304a2625aef3cc8d
[root@ecs-wangbiao ~]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
wangbiao-wordpress    latest              5b6309836c46        7 seconds ago       616 MB
docker.io/wordpress   latest              7070e6f90cac        7 hours ago         616 MB
docker.io/cirros      latest              66fa06a7355b        5 days ago          16.3 MB
docker.io/nginx       latest              f9c14fe76d50        13 days ago         143 MB
docker.io/registry    2                   65f3b3441f04        3 weeks ago         24 MB
docker.io/registry    latest              65f3b3441f04        3 weeks ago         24 MB
docker.io/mysql       5.7                 dd6675b5cfea        7 weeks ago         569 MB
[root@ecs-wangbiao ~]#

 所以Dockerfile的方式创建自定义镜像

[root@ecs-wangbiao ~]# docker rmi alpine
Untagged: alpine:latest
Untagged: docker.io/alpine@sha256:02bb6f428431fbc2809c5d1b41eab5a68350194fb508869a33cb1af4444c9b11
[root@ecs-wangbiao ~]# docker pull alpine
Using default tag: latest
Trying to pull repository docker.io/library/alpine ...
latest: Pulling from docker.io/library/alpine
Digest: sha256:02bb6f428431fbc2809c5d1b41eab5a68350194fb508869a33cb1af4444c9b11
Status: Downloaded newer image for docker.io/alpine:latest
[root@ecs-wangbiao ~]# vim Dockerfile

[root@ecs-wangbiao ~]# docker build -t wangbiao-alpine .
Sending build context to Docker daemon  16.9 kB
Step 1/2 : FROM alpine
 ---> 5e2b554c1c45
Step 2/2 : CMD nginx -g daemon off;
 ---> Running in 6a14013b05e3
 ---> a12b2b700698
Removing intermediate container 6a14013b05e3
Successfully built a12b2b700698
[root@ecs-wangbiao ~]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
wangbiao-alpine       latest              a12b2b700698        11 seconds ago      7.33 MB

  • 15
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值