标签:docker文件复制 数据库备份 wordpress安装 容器间数据共享
1. 运行Docker的第一个容器,进行测试解决:
[root@c720120 ~]# docker run busybox echo hello world
Unable to find image ‘busybox:latest‘ locally
latest: Pulling from library/busybox
07a152489297: Pull complete
Digest: sha256:141c253bc4c3fd0a201d32dc1f493bcf3fff003b6df416dea4f41046e0f37d47
Status: Downloaded newer image for busybox:latest
hello world
[root@c720120 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
可以看出前面没有任何的容器处于运行的状态,这是因为容器在执行完echo hello world命令后自动退出了。
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80d8496f3e1d busybox "echo hello world" 7 minutes ago Exited (0) 7 minutes ago infallible_wright
上面可以看到我们运行的hello-world.
[root@c720120 ~]# docker run -t -i ubuntu:14.04 /bin/bash
Unable to find image ‘ubuntu:14.04‘ locally
14.04: Pulling from library/ubuntu
324d088ce065: Pull complete
2ab951b6c615: Pull complete
9b01635313e2: Pull complete
04510b914a6c: Pull complete
83ab617df7b4: Pull complete
Digest: sha256:b8855dc848e2622653ab557d1ce2f4c34218a9380cceaa51ced85c5f3c8eb201
Status: Downloaded newer image for ubuntu:14.04
root@8845bfc2b967:/#
选项解释:-t代表终端,-i代表进入交互模式
解决:使用-d选项,代表是以后端进程的模式运行:
[root@c720120 ~]# docker run -d -p 1234:1234 python:2.7 python -m SimpleHTTPServer 1234
Unable to find image ‘python:2.7‘ locally
2.7: Pulling from library/python
cc1a78bfd46b: Pull complete
6861473222a6: Pull complete
7e0b9c3b5ae0: Pull complete
3ec98735f56f: Pull complete
9b311b87a021: Pull complete
7a5e5e5b06b6: Pull complete
e54e82f81b9b: Pull complete
bb4f45bde0ff: Pull complete
18a3b44c196a: Pull complete
Digest: sha256:b4a2a74875705c13220c0950c8df6401524d616468b014a19c6efb84c50bbb7e
Status: Downloaded newer image for python:2.7
ba3c0258f2afe21d1c62629546d5257fc98b03b14492b95e796184987e6b99f1
[root@c720120 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba3c0258f2af python:2.7 "python -m SimpleHTT…" 11 seconds ago Up 10 seconds 0.0.0.0:1234->1234/tcp amazing_euler
描述 :DockerFile是一个文本文件,该文本文件就是把一堆的步骤组合在一起,完成自动化的管理,如安装镜像,创建文件夹,映射端口等一堆操作。
简单示例:Dockerfile内容如下:
FROM busybox
ENV foo=bar
使用docker build命令去构建一个新的镜像叫做busybox2:如下
[root@c720120 docker]# docker build -t busybox2 .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM busybox
---> 8c811b4aec35
Step 2/2 : ENV foo=bar
---> Running in 6bc62bd05faf
Removing intermediate container 6bc62bd05faf
---> 428e72e9e893
Successfully built 428e72e9e893
Successfully tagged busybox2:latest
构建完成后,查看我们的镜像:
wordpress包括了MYSQL和HTTPD,PHP组件:使用Supervisor去监控和运行MYSQL和HTTPD
该知识点是使用supervisor在一个容器里去运行多个进程。
wp-config.php文件配置如下:
<?php
/
* The base configurations of the WordPress.
*
* This file has the following configurations: MySQL settings, Table Prefix,
* Secret Keys, and ABSPATH. You can find more information by visiting
* {@link http://codex.wordpress.org/Editing_wp-config.php Editing wp-config.php}
* Codex page. You can get the MySQL settings from your web host.
*
* This file is used by the wp-config.php creation script during the
* installation. You don‘t have to use the web site, you can just copy this file
* to "wp-config.php" and fill in the values.
*
* @package WordPress
*/
// MySQL settings - You can get this info from your web host //
/ The name of the database for WordPress */
define(‘DB_NAME‘, ‘wordpress‘);
/ MySQL database username */
define(‘DB_USER‘, ‘root‘);
/ MySQL database password */
define(‘DB_PASSWORD‘, ‘root‘);
/ MySQL hostname */
define(‘DB_HOST‘, ‘localhost‘);
/ Database Charset to use in creating database tables. */
define(‘DB_CHARSET‘, ‘utf8‘);
/ The Database Collate type. Don‘t change this if in doubt. */
define(‘DB_COLLATE‘, ‘‘);
/#@+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* @since 2.6.0
*/
define(‘AUTH_KEY‘, ‘put your unique phrase here‘);
define(‘SECURE_AUTH_KEY‘, ‘put your unique phrase here‘);
define(‘LOGGED_IN_KEY‘, ‘put your unique phrase here‘);
define(‘NONCE_KEY‘, ‘put your unique phrase here‘);
define(‘AUTH_SALT‘, ‘put your unique phrase here‘);
define(‘SECURE_AUTH_SALT‘, ‘put your unique phrase here‘);
define(‘LOGGED_IN_SALT‘, ‘put your unique phrase here‘);
define(‘NONCE_SALT‘, ‘put your unique phrase here‘);
/#@-*/
/
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each a unique
* prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = ‘wp_‘;
/
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*/
define(‘WP_DEBUG‘, false);
/* That‘s all, stop editing! Happy blogging. */
/ Absolute path to the WordPress directory. */
if ( !defined(‘ABSPATH‘) )
define(‘ABSPATH‘, dirname(FILE) . ‘/‘);
/ Sets up WordPress vars and included files. */
require_once(ABSPATH . ‘wp-settings.php‘);
```**
supervisord.conf文件内容如下:
[supervisord]
nodaemon=true
[program:mysqld]
command=/usr/bin/mysqld_safe
autostart=true
autorestart=true
user=root
[program:httpd]
command=/bin/bash -c "rm -rf /run/httpd/* && /usr/sbin/apachectl -D FOREGROUND"
Dockerfile文件内容如下:
FROM ubuntu:14.04
RUN apt-get update && apt-get -y install \
apache2 \
```php5 php5-mysql supervisor wget
RUN echo ‘mysql-server mysql-server/root_password password root‘ | debconf-set-selections && echo ‘mysql-server mysql-server/root_password_again password root‘ | debconf-set-selections
RUN apt-get isntall -qqy mysql-server
RUN wget http://wwordpress.org/latest.tar.gz && tar xzvf latest.tar.gz && cp -R ./wordpress/* /var/www/html && rm /var/www/html/index.html
RUN (/usr/bin/mysqld_safe &); sleep 5; mysqladmin -u root -proot create wwordpress
COPY wp-config.php /var/www/html/wp-config.php5
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
EXPOSE 80
CMD ["/usr/bin/supervisord"]
然后运行
docker build -t wordpress .
docker run -d -p 80:80 wordpress
通过使用supervisor去运行多个应用服务在一个容器中虽说可以工作,但最好在多个容器中运行,官方网站也是建议一个容器中只运行一个服务。
(1)拉取镜像
$ docker pull wordpress:latest
$ docker pull mysql:latest
$ docker images
(2)运行一个MYSQL的容器,并通过一个环境变量设置MYSQL_ROOT_PASSWORD
[root@c720120 supervisor]# docker run --name mysqlwp -e MYSQL_ROOT_PASSWORD=wordpressdocker -d mysql
3a3ed895c2eaf4f252a2e73df660cb127bf1924db6bcd52f7a74509e108a61ae
(3)运行wordpress的容器,并使用--link选项把它与mysql的容器连接在一起,
[root@c720120 supervisor]# docker run --name wordpress --link mysqlwp:mysql -p 80:80 -d wordpress
5ad18e45b2154c0e04919d6df22567631b0f4ed9ce27fa411b135aff31e26012
当我打开浏览器时,无法正常打开wordpress页面。使用docker logs wordpress,报错信息如下:
解决方法步骤:
docker exec -it mysqlwp /bin/bashj
mysql -uroot -pwordpressdocker
ALTER USER ‘root‘@‘%‘ IDENTIFIED WITH mysql_native_password BY ‘wordpressdocker‘;
(4)docker start wordpress
注意:一般不建议使用数据库root的帐户去连接应用,可能会带来安全问题。创建用户名、密码、数据库如下:
[root@c720120 ~]# docker run --name mysqlwp -e MYSQL_ROOT_PASSWORD=wordpressdocker > -e MYSQL_DATABASE=wordpress > -e MSYQL_USER=wordpress > -e MYSQL_PASSWORD=wordpresspwd > -d mysql
15e8b91a5a48b336a014802041767dc715e805de82c21999dfd9ea6732e22eef
数据库创建完成后,在运行wordpress需要指明我们创建的用户名、密码、数据库等信息。
[root@c720120 ~]# docker run --name wordpress --link mysqlwp:mysql -p 80:80 > -e WORDPRESS_DB_NAME=wordpress > -e WORDPRESS_DB_USER=wordpress > -e WORDPRESS_PASSWORD=wordpresspwd > -d wordpress
a81ca93e6a25b2b05cd689a3f3c7e6f13d5d3f3817868c80db51c1edb09e596d
假如想要移除所有的容器可以使用以下命令:
$docker stop $(dokcer ps -q)
$ docker rm -v $(docker ps -aq)
需求:由于虚拟机在停止移除后,里面的数据会丢失,但数据库又要保证一致性,所以我们需要备份 我们在容器中的数据库。
备份方法:
(1)从Docker host挂载数据卷到容器中去。
[root@c720120 ~]# docker run --name mysqlwp -e MYSQL_ROOT_PASSWORD=wordpressdocker -e MYSQL_DATABASE=wordpress -e MYSQL_USER=wordpress -e MYSQL_PASSWORD=wordpresspwd -v /home/root/mysql:/var/lib/mysql -d mysql
b2913d5133196c5437fdeba0ff0753b467b9614aa6c51129deca516895846a4f
此时,我们可以查看我们容器中的mysql各文件
[root@c720120 ~]# ls /home/root/mysql/
auto.cnf binlog.index client-cert.pem ibdata1 ibtmp1 performance_schema server-cert.pem undo_001
binlog.000001 ca-key.pem client-key.pem ib_logfile0 mysql private_key.pem server-key.pem undo_002
binlog.000002 ca.pem ib_buffer_pool ib_logfile1 mysql.ibd public_key.pem sys wordpress
(2)使用docker exec命令去调用mysqldump
[root@c720120 ~]# docker exec mysqlwp mysqldump --all-databases --password=wordpressdocker > wordpress.backup
mysqldump: [Warning] Using a password on the command line interface can be insecure.
查看我们的备份文件,如下所示:
·1.11 在容器和Docker host之间共享数据
(1)
[root@c720120 tmp]# ls -l
total 0
-rw-r--r-- 1 root root 0 May 27 10:19 data
(2)运行一个容器,并挂载当前目录到容器的/cookbook中去,并在容器的cookbook文件夹下创建文件,然后在Docker host中查看是否存在这个文件。
[root@c720120 tmp]# docker run -it -v "$PWD":/cookbook ubuntu:14.04 /bin/bash
root@568bfa25fd31:/# touch /cookbook/foobar
root@568bfa25fd31:/# exit
exit
(3)查看是否有我们刚才创建的文件
[root@c720120 tmp]# ls -l foobar
-rw-r--r-- 1 root root 0 May 27 10:21 foobar
注意:默认我们挂载的目录是以读写方式,假如我们想以只读的方式的话,需要明确指定。
使用如下格式:
-v "$PWD":/cookbook:ro
检查挂载的对应信息:使用如下命令:
$ docker inspect -f {{.Mounts}} 44d71a605b5b
[{ /Users/sebastiengoasguen/Desktop /cookbook true}]
如果我们在挂载卷时没有指定本地的位置时,根据不同的环境可能默认的位置不一样,这里我们来查看我们的环境的默认位置,我们的位置是/var/lib/docker/volumes/a198d9fac794b46bacd0a587f7bf4347fd6c617e287181c223ca257b35c1647b/_data /cookbook
[root@c720120 tmp]# docker run -it -v /cookbook ubuntu:14.04 /bin/bash
root@be58bd9b2cd1:/#
root@be58bd9b2cd1:/#
root@be58bd9b2cd1:/# touch /cookbook/foobar
root@be58bd9b2cd1:/# ls cookbook/
foobar
root@be58bd9b2cd1:/# exit
exit
d[root@c720120 tmp]# docker inspect -f {{.Mounts}} be58
[{volume a198d9fac794b46bacd0a587f7bf4347fd6c617e287181c223ca257b35c1647b /var/lib/docker/volumes/a198d9fac794b46bacd0a587f7bf4347fd6c617e287181c223ca257b35c1647b/_data /cookbook local true }]
和其它容器之间共享数据,使用--volumes-from选项。
案例:
(1)创建一个容器,并挂载目录 [root@c720120 _data]# docker run -v /data --name data ubuntu:14.04
(2)查看容器/data目录与docker host之间的对应关系
[root@c720120 _data]# docker inspect -f {{.Mounts}} data[{volume 605c56152f8892a7ac07120669ee01ef20ac62f00baaf47f56aca6a19b10b91e /var/lib/docker/volumes/605c56152f8892a7ac07120669ee01ef20ac62f00baaf47f56aca6a19b10b91e/_data /data local true }]
(3)创建另外一个容器,在此我们使用--volumes-from选项,并创建一个文件
[root@c720120 _data]# docker run -ti --volumes-from data ubuntu:14.04 /bin/bash
root@5a5d99af6149:/# touch /data/foobar
(4)在docker-host查看是否有该文件,记着查询的目录 应该是第一个容器的映射的本地目录位置。
[root@c720120 _data]# ls /var/lib/docker/volumes/605c56152f8892a7ac07120669ee01ef20ac62f00baaf47f56aca6a19b10b91e/_data
foobar
使用copy命令可以把数据从容器中复制到Docker host,或从docker host复制到容器中都可以。
(1)创建一个容器,并创建一个文件
[root@c720120 _data]# docker run -d --name testcopy ubuntu:14.04 sleep 360
[root@c720120 _data]# docker exec -ti testcopy /bin/bash
[root@c720120 _data]# docker exec -ti testcopy /bin/bash
root@90d654f31d93:/# cd /root/
root@90d654f31d93:~# echo ‘I am in the container‘ > file.txt
root@90d654f31d93:~# exit
(2)从docker容器复制文件到docker主机的当前目录,并查看我们的文件内容,是不是在容器中创建的文件。
[root@c720120 _data]# docker cp testcopy:/root/file.txt .
[root@c720120 _data]# cat file.txt
I am in the container
(3)从主机复制文件到容器中
[root@c720120 _data]# echo ‘I am in the host‘ > host.txt
[root@c720120 _data]# docker cp host.txt testcopy:/root/host.txt
注意:在docker1.8之前,是不支持从docker主机复制文件到容器中的,那么我们可以使用以下命令代替。
$ echo ‘I am in the host‘ > host.txt
$ docker exec -i textcopy sh -c ‘cat > /root/host.txt‘ < host.txt
$ docker exec -i testcopy sh -c ‘cat /root/host.txt‘
技术讨论群:190029784
标签:docker文件复制 数据库备份 wordpress安装 容器间数据共享
原文地址:http://blog.51cto.com/aishangwei/2120732