Docker QuickStart

来自Jack's Lab
跳转到: 导航, 搜索
第392行: 第392行:
=== MySQL ===
=== MySQL ===
* 8.0.18, 8.0, 8, latest
* 8.0.18, 8.0, 8, latest
第407行: 第409行:
<source lang=bash>
<source lang=bash>
第421行: 第422行:
$ docker exec -i some-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql
$ docker exec -i some-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql
'''Example stack.yml for mysql:'''
# Use root/example as user/password credentials
version: '3.1'
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
      MYSQL_ROOT_PASSWORD: example
    image: adminer
    restart: always
      - 8080:8080

2019年11月19日 (二) 14:51的版本


1 Install


Aliyun (Old):

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



2 Quick Start 免费注册一个 Docker 账号

登录 docker hub:

comcat@icamgo:~$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to to create one.
Username: noduino
Login Succeeded

搜索镜像 docker search

comcat@icamgo:~$ docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relati...   8741      [OK]
mariadb                           MariaDB is a community-developed fork of M...   3063      [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Crea...   647                  [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   63
centurylink/mysql                 Image containing mysql. Optimized to be li...   61                   [OK]
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. ...   54
deitch/mysql-backup               REPLACED! Please use   41                   [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      35                   [OK]
tutum/mysql                       Base docker image to run a MySQL database ...   34
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic back...   28                   [OK]
prom/mysqld-exporter                                                              23                   [OK]
linuxserver/mysql                 A Mysql container, brought to you by Linux...   22
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   16
circleci/mysql                    MySQL is a widely used, open-source relati...   15
mysql/mysql-router                MySQL Router provides transparent routing ...   13
arey/mysql-client                 Run a MySQL client from a docker container      12                   [OK]
imega/mysql-client                Size: 36 MB, alpine:3.5, Mysql client: 10....   8                    [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 ima...   6
yloeffler/mysql-backup            This image runs mysqldump to backup data u...   6                    [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron t...   4                    [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  2                    [OK]
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                    [OK]
jelastic/mysql                    An image of the MySQL database server main...   1
monasca/mysql-init                A minimal decoupled init container for mysql    0
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Cu...   0                    [OK]

拉取官方 mysql 镜像 docker pull:

推送你修改后的 mysql 镜像 docker push:

$ docker push {username}/{镜像名}:{tag}

3 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

4 Run container

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

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

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


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

5 RM container

comcat@aisrv:/work/api.noduino/database$ docker ps -a
comcat@aisrv:/work/api.noduino/database$ docker rm 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   "/ mysql"   18 minutes ago      Up 18 minutes                 3306/tcp            mysql
52df2e0d29b2        emqttd-docker-v2.1.2     "/opt/emqttd/"   10 days ago         Exited (137) 45 minutes ago                       emq20

6 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

7 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

8 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
comcat@aisrv:/work/api.noduino/database$ docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS               NAMES
987ab5054b83        mysql/mysql-server:5.6   "/ 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
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 NAMES utf8 */;

9 dump all databases

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

10 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'

11 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';

12 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

13 Using the data volume

14 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": "",
        "IPv6Address": "",
        "MacAddress": "02:42:ac:11:00:02",
        "Name": "mydb"
    "2d7d38840458c6666980453c16c3798cdab4655e97b470b1f145494705de344a": {
        "EndpointID": "de8090170c4e6a73c401a8175375cc595a7c50a9afcbb50b04e11cdf1c0af239",
        "IPv4Address": "",
        "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

15 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.

16 Export Container

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

17 Restore Container

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

18 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,>80/tcp   xda-srv
comcat@aisrv:/work/docker$ docker stop e1
comcat@aisrv:/work/docker$ docker commit e1b srv-xda-api:0.9.2
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="" -p 8000:80 srv-xda-api:0.9.2
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,>80/tcp   fervent_stonebraker

19 Reference

19.1 PowerShell

19.2 MySQL

  • 8.0.18, 8.0, 8, latest
  • 5.7.28, 5.7, 5
  • 5.6.46, 5.6

Env variables:

$ docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.6

$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.6.46
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.6 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

$ docker run -it --rm mysql:5.6 --verbose --help

$ docker run -v /srv/data:/var/lib/mysql --user 1000:1000 --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.6
$ docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql
$ docker exec -i some-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql

Example stack.yml for mysql:

# Use root/example as user/password credentials
version: '3.1'


    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
      MYSQL_ROOT_PASSWORD: example

    image: adminer
    restart: always
      - 8080:8080

19.3 IIS

Create a Dockerfile with your website


RUN powershell -NoProfile -Command Remove-Item -Recurse C:\inetpub\wwwroot\*

WORKDIR /inetpub/wwwroot

COPY content/ .

You can then build and run the Docker image:

 $ docker build -t iis-site .
 $ docker run -d -p 8000:80 --name my-running-site iis-site

There is no need to specify an ENTRYPOINT in your Dockerfile since the microsoft/iis base image already includes an entrypoint application that monitors the status of the IIS World Wide Web Publishing Service (W3SVC).

Verify in the browser

With the current release, you can't use http://localhost to browse your site from the container host. This is because of a known behavior in WinNAT, and will be resolved in future. Until that is addressed, you need to use the IP address of the container.

Once the container starts, you'll need to finds its IP address so that you can connect to your running container from a browser. You use the docker inspect command to do that:

docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" my-running-site

You will see an output similar to this:

You can connect the running container using the IP address and configured port, in the example shown.

In addition to static content, IIS can run other workloads including but not limited to ASP.NET, ASP.NET Core, NodeJS, PHP, and Apache Tomcat.

For a comprehensive tutorial on running an ASP.NET app in a container, check out the tutorial on the docs site.

将 ASP.NET MVC 应用程序迁移到 Windows 容器

19.4 ASP.NET

Featured Tags
docker pull
docker pull
About This Image
ASP.NET is a high productivity framework for building Web Applications using Web Forms, MVC, Web API and SignalR.

This image contains:

Windows Server Core as the base OS
IIS 10 as Web Server
.NET Framework (multiple versions available)
.NET Extensibility for IIS
Watch dotnet/announcements for Docker-related .NET announcements.

How to use these Images
The .NET Framework Docker samples show various ways to use .NET Framework and Docker together.

Container sample: Run an ASP.NET application
You can quickly run a container with a pre-built sample ASP.NET Docker image, based on the [ASP.NET Docker sample].

Type the following Docker command:

docker run --name aspnet_sample --rm -it -p 8000:80
After the application starts, navigate to http://localhost:8000 in your web browser. You need to navigate to the application via IP address
instead of localhost for earlier Windows versions, which is demonstrated in View the ASP.NET app in a running container on Windows.

Related Repos
.NET Framework:

dotnet/framework/sdk: .NET Framework SDK
dotnet/framework/runtime: .NET Framework Runtime
dotnet/framework/wcf: Windows Communication Framework (WCF)
dotnet/framework/samples: .NET Framework, ASP.NET and WCF Samples
.NET Core:

dotnet/core: .NET Core
dotnet/core-nightly: .NET Core (Preview)
dotnet/core/samples: .NET Core Samples