Docker容器化学习笔记(三)应用部署 和 备份与迁移

文章目录

  • 一、docker应用部署
    • 1. mysql部署
    • 2. tomcat部署
    • 3. Nginx部署
    • 4. redis部署
  • 二、docker备份与迁移
    • 容器保存为镜像【docker commit】
    • 镜像备份【docker save -o】
    • 镜像恢复与迁移【docker load -i】

一、docker应用部署

1. mysql部署

  1. 拉取MySQL镜像
docker pull centos/mysql-57-centos7
  1. 创建容器
docker run -di --name=tensquare_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=a mysql

-p:代表端口映射。格式:宿主机端口:容器端口
-e:代表添加环境变量,MYSQL_ROOT_PASSWORD是root用户的登录密码。

示例:

[root@localhost myhtml]# docker images
REPOSITORY                TAG       IMAGE ID       CREATED         SIZE
centos/mysql-57-centos7   latest    f83a2938370c   2 years ago     452MB
[root@localhost myhtml]# docker run -di --name=tensqure_mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=a f83a2938370c
534f44ef5abfa0424ab5a634fb02ae5d128b0bd75c2e7ca2a2b57e309f7ae5b6
[root@localhost myhtml]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                         NAMES
534f44ef5abf   f83a2938370c   "container-entrypoin…"   22 seconds ago   Up 18 seconds   0.0.0.0:33306->3306/tcp, :::33306->3306/tcp   tensqure_mysql

2. tomcat部署

  1. 拉取tomcat镜像
docker pull tomcat:7-jre8
  1. 创建容器:
docker run -di --name=mytomcat -p 8888:8080 -v /usr/local/docker_mytomcat/webapps:/usr/local/tomcat/webapps tomcat:7-jre8

示例:

[root@localhost myhtml]# docker pull tomcat:7-jre8
7-jre8: Pulling from library/tomcat
c5e155d5a1d1: Pull complete 
221d80d00ae9: Pull complete 
4250b3117dca: Pull complete 
d1370422ab93: Pull complete 
deb6b03222ca: Pull complete 
9cdea8d70cc3: Pull complete 
968505be14db: Pull complete 
04b5c270ac81: Pull complete 
301d76fcab1f: Pull complete 
ddf85cd1cc29: Pull complete 
ef1e6f25e812: Pull complete 
Digest: sha256:3d6475354578a0b083f2e91700dcb8a1275dc5f574435276ce0ec69be588d6a3
Status: Downloaded newer image for tomcat:7-jre8
docker.io/library/tomcat:7-jre8

[root@localhost myhtml]# docker run -di --name=mytomcat -p 8888:8080 -v /usr/local/docker_mytomcat/webapps:/usr/local/tomcat/webapps tomcat:7-jre8
1cabdeaa1adf1e57e948d60201d241dead03e3d2f51219ab7e627a94c6534c06
[root@localhost myhtml]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                         NAMES
1cabdeaa1adf   tomcat:7-jre8   "catalina.sh run"        10 seconds ago   Up 7 seconds    0.0.0.0:8888->8080/tcp, :::8888->8080/tcp     mytomcat

测试:
在webapps目录下新建一个ROOT目录,在ROOT目录下新建一个index.html文件,访问192.168.2.11:8888则可以看到访问index.html成功!

3. Nginx部署

  1. 拉取镜像
docker pull nginx
  1. 创建容器
docker run -di --name=mynginx -p 80:80 nginx

示例:

[root@localhost docker_mytomcat]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
b380bbd43752: Pull complete 
fca7e12d1754: Pull complete 
745ab57616cb: Pull complete 
a4723e260b6f: Pull complete 
1c84ebdff681: Pull complete 
858292fd2e56: Pull complete 
Digest: sha256:644a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost docker_mytomcat]# docker run -di --name=mynginx -p 80:80 nginx
7092668c9e31b5b5cf64f0609a240da052879c3329ca5bf5db118961b8525e53
[root@localhost docker_mytomcat]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED             STATUS          PORTS                                         NAMES
7092668c9e31   nginx           "/docker-entrypoint.…"   7 seconds ago       Up 5 seconds    0.0.0.0:80->80/tcp, :::80->80/tcp             mynginx

测试:访问虚拟机IP 192.168.2.11
在mynginx上部署静态页面

[root@localhost ~]# docker exec -it mynginx /bin/bash
root@7092668c9e31:/# ls
bin  boot  dev	docker-entrypoint.d  docker-entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@7092668c9e31:/# cd etc
root@7092668c9e31:/etc# ls
adduser.conf		ca-certificates.conf  dpkg	   group-     hosts	 ld.so.cache	login.defs   nginx	    passwd     rc2.d  resolv.conf  shadow-  systemd
alternatives		cron.daily	      environment  gshadow    init.d	 ld.so.conf	logrotate.d  nsswitch.conf  passwd-    rc3.d  rmt	   shells   terminfo
apt			debconf.conf	      fonts	   gshadow-   inputrc	 ld.so.conf.d	machine-id   opt	    profile    rc4.d  securetty    skel     timezone
bash.bashrc		debian_version	      fstab	   gss	      issue	 ldap		mke2fs.conf  os-release     profile.d  rc5.d  security	   ssl	    ucf.conf
bindresvport.blacklist	default		      gai.conf	   host.conf  issue.net  libaudit.conf	motd	     pam.conf	    rc0.d      rc6.d  selinux	   subgid   update-motd.d
ca-certificates		deluser.conf	      group	   hostname   kernel	 localtime	mtab	     pam.d	    rc1.d      rcS.d  shadow	   subuid   xattr.conf
root@7092668c9e31:/etc# cd cat nginx.cof
bash: cd: too many arguments
root@7092668c9e31:/etc# cd cat nginx.conf
bash: cd: too many arguments
root@7092668c9e31:/etc# cd nginx
root@7092668c9e31:/etc/nginx# cat nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
root@7092668c9e31:/etc/nginx# cd /etc/nginx/conf.d
root@7092668c9e31:/etc/nginx/conf.d# ls
default.conf
root@7092668c9e31:/etc/nginx/conf.d# cat default.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

root@7092668c9e31:/etc/nginx/conf.d# cd /usr/share/nginx/html
root@7092668c9e31:/usr/share/nginx/html# ls
50x.html  index.html
[root@localhost myhtml]# docker cp index.html mynginx:/usr/share/nginx/html
root@7092668c9e31:/usr/share/nginx/html# exit
exit

4. redis部署

  1. 拉取镜像
docker pull redis
  1. 创建容器
docker run -di --name=myredis -p 6379:6379 redis

示例:

[root@localhost ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
7d63c13d9b9b: Pull complete 
a2c3b174c5ad: Pull complete 
283a10257b0f: Pull complete 
7a08c63a873a: Pull complete 
0531663a7f55: Pull complete 
9bf50efb265c: Pull complete 
Digest: sha256:a89cb097693dd354de598d279c304a1c73ee550fbfff6d9ee515568e0c749cfe
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
[root@localhost ~]# docker run -di --name=myredis -p 6379:6379 redis
6a092e1e86090c4250485d7b0b627ad90ef128dbca288c06c76ecc03856aca61
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                         NAMES
6a092e1e8609   redis           "docker-entrypoint.s…"   9 seconds ago    Up 6 seconds    0.0.0.0:6379->6379/tcp, :::6379->6379/tcp     myredis

测试:cmd

D:\Program Files\redis-x64-3.2.100>redis-cli -h 192.168.2.11
192.168.2.11:6379> set name huathy
OK
192.168.2.11:6379> get name
"huathy"

二、docker备份与迁移

容器保存为镜像【docker commit】

docker commit mynginx mynginx_i
示例:

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                         NAMES
7092668c9e31   nginx           "/docker-entrypoint.…"   33 minutes ago   Up 33 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp             mynginx
[root@localhost ~]# docker commit mynginx mynginx_i
sha256:76839a178b77c2a8f6edbe02ce30d31aff4b17478df34850d1410e2b636d6fdd
[root@localhost ~]# docker images
REPOSITORY                TAG       IMAGE ID       CREATED          SIZE
mynginx_i                 latest    76839a178b77   13 seconds ago   133MB

[root@localhost ~]#  docker run -di --name=mynginxi1 -p 81:80 mynginx_i
b3bfc09779c68b8d6cf4976ec3d5f2bd8e008bbb8178d23cf61494109b1b28ca

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS             PORTS                                         NAMES
b3bfc09779c6   mynginx_i       "/docker-entrypoint.…"   54 seconds ago   Up 52 seconds      0.0.0.0:81->80/tcp, :::81->80/tcp             mynginxi1

镜像备份【docker save -o】

docker save -o mynginx.tar mynginx_i

[root@localhost ~]# docker save -o mynginx.tar mynginx_i
[root@localhost ~]# ls
anaconda-ks2.cfg  anaconda-ks.cfg  mynginx.tar

镜像恢复与迁移【docker load -i】

docker load -i mynginx.tar
示例:先删除镜像,然后恢复。

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS             PORTS                                         NAMES
b3bfc09779c6   mynginx_i       "/docker-entrypoint.…"   4 minutes ago    Up 4 minutes       0.0.0.0:81->80/tcp, :::81->80/tcp             mynginxi1

[root@localhost ~]# docker stop mynginxi1
mynginxi1

[root@localhost ~]# docker rm mynginxi1
mynginxi1

[root@localhost ~]# docker rmi mynginx_i
Untagged: mynginx_i:latest
Deleted: sha256:76839a178b77c2a8f6edbe02ce30d31aff4b17478df34850d1410e2b636d6fdd
Deleted: sha256:acebb1c9297c7fca6ae0b7df3f19a4b3b027d8948ee89b437d6c8f9652a9dcc7

[root@localhost ~]# docker load -i mynginx.tar
6ae1b73c538f: Loading layer [==================================================>]  18.43kB/18.43kB
Loaded image: mynginx_i:latest

[root@localhost ~]# docker images
REPOSITORY                TAG       IMAGE ID       CREATED         SIZE
mynginx_i                 latest    76839a178b77   9 minutes ago   133MB

你可能感兴趣的:(Docker,docker,容器,运维)