Docker QuickStart

来自Jack's Lab
跳转到: 导航, 搜索

目录

1 Install

$ sudo apt-get install docker.io docker-compose
$ sudo usermod -aG docker $USER



2 Pull image from docker hub

comcat@aisrv:/work/api.noduino/database$ docker pull mysql/mysql-server:5.5
5.5: Pulling from mysql/mysql-server
33a3a5e469a8: Pull complete
afd5b2a51aab: Pull complete
d3fcc85ceffc: Pull complete
9c58bb63c013: Pull complete
Digest: sha256:db8aecdf439d2a5dcc509cca80deabb28a8b7370725d0c71f46957d0e257ffd6
Status: Downloaded newer image for mysql/mysql-server:5.5

comcat@aisrv:/work/api.noduino/database$ docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
emqwebhook             20170505            1a2e9eb03836        44 hours ago        33.65 MB
<none>                 <none>              3fc8aea36f2d        46 hours ago        3.994 MB
nginx                  latest              46102226f2fd        10 days ago         109.4 MB
emqttd-docker-v2.1.2   latest              a799525a9a73        2 weeks ago         34.3 MB
mysql/mysql-server     5.5                 89418f92e739        3 weeks ago         168.9 MB
alpine                 3.5                 4a415e366388        9 weeks ago         3.987 MB



3 Run container

comcat@aisrv:/work/api.noduino/database$ docker run --name cos-mysql -e MYSQL_ROOT_PASSWORD=root -d mysql/mysql-server:5.5
beb67efbda72bb356a2630624eff965e89d5477cf7f596176fa45cb036d7db75

comcat@aisrv:/work/api.noduino/database$ docker exec -it cos-mysql mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.55 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

comcat@aisrv:/work/api.noduino/database$ docker exec -it cos-mysql bash



4 RM container

comcat@aisrv:/work/api.noduino/database$ docker ps -a
comcat@aisrv:/work/api.noduino/database$ docker rm beb67efbda72
beb67efbda72

comcat@aisrv:/work/api.noduino/database$ docker pull mysql/mysql-server:5.6

comcat@aisrv:/work/api.noduino/database$ docker ps -a
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS                        PORTS               NAMES
987ab5054b83        mysql/mysql-server:5.6   "/entrypoint.sh mysql"   18 minutes ago      Up 18 minutes                 3306/tcp            mysql
52df2e0d29b2        emqttd-docker-v2.1.2     "/opt/emqttd/start.sh"   10 days ago         Exited (137) 45 minutes ago                       emq20



5 Export port

comcat@aisrv:/work/api.noduino/database$ docker run -tid --name emq20 -p 1883:1883 -p 8083:8083 -p 8883:8883 -p 8084:8084 -p 18083:18083 emqttd-docker-v2.1.2
docker: Error response from daemon: Conflict. The name "/emq20" is already in use by container 52df2e0d29b28509b2daae48a345cb1a44b2b5ed3ff268507cf223059d9b
You have to remove (or rename) that container to be able to reuse that name..

comcat@aisrv:/work/api.noduino/database$ docker start emq20
emq20



6 Passing options to the server

$ docker run --name test -e MYSQL_ROOT_PASSWORD=root -v $(pwd)/cos-db:/var/lib/mysql -d mysql/mysql-server:5.6 --explicit_defaults_for_timestamp=true



7 Mount host dir

  • place the dumped data file tempdb-xxx.sql in host dir cos-db/
  • not support the sym link
comcat@aisrv:/work/api.noduino/database$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=root -v $(pwd)/cos-db:/var/lib/mysql -d mysql/mysql-server:5.6
987ab5054b83a14a2249650715353b4b9dddb0e78fcbc514ae37be975ca4c145
comcat@aisrv:/work/api.noduino/database$ docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS               NAMES
987ab5054b83        mysql/mysql-server:5.6   "/entrypoint.sh mysql"   3 seconds ago       Up 2 seconds        3306/tcp            mysql

comcat@aisrv:/work/api.noduino/database$ ls cos-db
auto.cnf  ibdata1  ib_logfile0  ib_logfile1  mysql  mysql.sock  performance_schema  tempdb20170407.sql
comcat@aisrv:/work/api.noduino/database$ docker exec -it mysql bash
bash-4.2# ls /var/lib/mysql/
auto.cnf  ib_logfile0  ib_logfile1  ibdata1  mysql  mysql.sock  performance_schema  tempdb20170407.sql
bash-4.2# head /var/lib/mysql/tempdb20170407.sql
head: cannot open '/var/lib/mysql/tempdb20170407.sql' for reading: No such file or directory
bash-4.2# exit
exit
comcat@aisrv:/work/api.noduino/database$ rm cos-db/tempdb20170407.sql
comcat@aisrv:/work/api.noduino/database$ cp ../backup/tempdb20170407.sql cos-db/
comcat@aisrv:/work/api.noduino/database$ docker exec -it mysql bash
bash-4.2# head /var/lib/mysql/tempdb20170407.sql
-- MySQL dump 10.13  Distrib 5.5.49, for debian-linux-gnu (x86_64)
--
-- Host: localhost    Database: tempdb
-- ------------------------------------------------------
-- Server version   5.5.49-0ubuntu0.14.04.1

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;



8 dump all databases

$ docker exec mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > all.sql



9 dump table column

$ cat cos-db/getdevid.sql
select dvid from temp where productid='NNN1234' and uploadtime>'2017-03-01 00:00:00';

$ docker exec mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" tempdb </var/lib/mysql/getdevid.sql > /var/lib/mysql/alldevid'



10 Restore Database

$ cp cos-tempdb-170507.sql test/
$ docker run --name test -e MYSQL_ROOT_PASSWORD=root -v $(pwd)/test:/var/lib/mysql -d mysql/mysql-server:5.6  --explicit_defaults_for_timestamp=true

$ docker exec test sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" < /var/lib/mysql/cos-tempdb-170507.sql';



11 Custom config file

$ docker exec -it cosdb cat /etc/my.cnf > my.cnf
$ docker --name cosdb -e MYSQL_ROOT_PASSWORD=root -v $(pwd)/my.cnf:/etc/my.cnf -v $(pwd)/test:/var/lib/mysql -d mysql/mysql-server:5.6  --explicit_defaults_for_timestamp=true



12 Using the data volume





13 Notes

$ docker run --name mydb -e MYSQL_ROOT_PASSWORD=root -v $(pwd)/mydb:/var/lib/mysql -d mysql/mysql-server:5.6  --explicit_defaults_for_timestamp=true
$ docker exec mydb sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" < /var/lib/mysql/cos-tempdb-170507.sql';
$ docker run --name nginx-php -v $(pwd)/wxmp:/var/www/html --link mydb:mysql -d richarvey/nginx-php-fpm

comcat@aisrv:/work/api.noduino/wxmp$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
7fcde0337aa1        bridge              bridge              local
11451da05352        host                host                local
b91f895fe12c        none                null                local
comcat@aisrv:/work/api.noduino/wxmp$ docker network inspect --format='{{json .Containers}}' 7fcde0337aa1 | python -m json.tool
{
    "03535b5be172ce05c8ab5b43b8cdd80f9d04e3af8f47ba4911ea8fdc0161b87a": {
        "EndpointID": "d78fce5086d81cc0f9835018a50309a36b179bffb1a65848e2f2d74f5dc4e049",
        "IPv4Address": "172.17.0.2/16",
        "IPv6Address": "",
        "MacAddress": "02:42:ac:11:00:02",
        "Name": "mydb"
    },
    "2d7d38840458c6666980453c16c3798cdab4655e97b470b1f145494705de344a": {
        "EndpointID": "de8090170c4e6a73c401a8175375cc595a7c50a9afcbb50b04e11cdf1c0af239",
        "IPv4Address": "172.17.0.3/16",
        "IPv6Address": "",
        "MacAddress": "02:42:ac:11:00:03",
        "Name": "nginx-php"
    }
}

# export port
comcat@aisrv:/work/api.noduino/wxmp$ docker run -p 80:80 --name nginx-php -v $(pwd)/wxmp:/var/www/html --link mydb:mysql -d richarvey/nginx-php-fpm
054878b224c2ec32b67d80bbba50d605566bf1b6f19a9921c898dbd73d504647



14 nignx-php

Install PHP Modules

To install and configure extra PHP modules in this image, first of all drop into the container:

 docker exec -t -i nginx /bin/bash

Then configure and install your module:

 /usr/local/bin/docker-php-ext-configure sockets
 /usr/local/bin/docker-php-ext-install sockets

Now restart php-fpm:

supervisorctl restart php-fpm

We may include a env var to do this in the future.



15 Export Container

$ sudo docker export <CONTAINER ID> > /home/export.tar

16 Restore Container

$ cat /home/export.tar | sudo docker import - api-elec:latest


17 Commit

comcat@aisrv:/work/docker$ docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                     NAMES
e1bfd19e8626        srv-xda-api:0.9         "docker-php-entrypoin"   7 minutes ago       Up 7 minutes        443/tcp, 9000/tcp, 0.0.0.0:8000->80/tcp   xda-srv
comcat@aisrv:/work/docker$ docker stop e1
e1
comcat@aisrv:/work/docker$ docker commit e1b srv-xda-api:0.9.2
sha256:f39c64c92367be2569d98ce889d4863dbb85f30bc425ecd0fdeea21bb43eb486
comcat@aisrv:/work/docker$ docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
srv-xda-api               0.9.2               f39c64c92367        5 seconds ago       345.2 MB
srv-xda-api               0.9                 a160e6e42d1c        9 minutes ago       345.2 MB

comcat@aisrv:/work/docker$ docker run -d -e DB_HOST="192.168.1.20" -p 8000:80 srv-xda-api:0.9.2
4e6bfec25da6d33d3b9a3aa03b62f07ce2f8e61528e7292b140da99d80cef767
comcat@aisrv:/work/docker$ docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                     NAMES
4e6bfec25da6        srv-xda-api:0.9.2       "docker-php-entrypoin"   3 seconds ago       Up 2 seconds        443/tcp, 9000/tcp, 0.0.0.0:8000->80/tcp   fervent_stonebraker

18 Reference



































个人工具
名字空间

变换
操作
导航
工具箱