I. Pull and Run a official image

1. Pull a MySQL docker image

docker pull mysql:latest

docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    mysql               latest              d435eee2caa5        2 weeks ago         456MB

2. Run a MySQL docker container

# -d,       as daemon mode
# -name,    naming the container
# -p,       mapping the network port
# -e,       assign an environment variable

docker run --name c_mysql -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mysql mysql:latest
    241fd5e41ae17791df7cf43deddd9aca70dadb2c0e20f2ca0c4b27fa214db1da

docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
    241fd5e41ae1        mysql:latest        "docker-entrypoint.s…"   15 seconds ago      Up 14 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   c_mysql

4. Connect to MySQL container OS

docker exec -it c_mysql /bin/bash
    root@241fd5e41ae1:/# 

5. Stop the MySQL container

docker stop c_mysql

docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS               NAMES
    241fd5e41ae1        mysql:latest        "docker-entrypoint.s…"   4 minutes ago       Exited (0) 1 second ago                       c_mysql

II. Build and Run a customized image

1. Write a Dockerfile

vi Dockerfile
FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD mysql
ENV MYSQL_DATABASE db1
ENV MYSQL_USER u1
ENV MYSQL_PASSWORD p1
ADD script.sql /docker-entrypoint-initdb.d
EXPOSE 3306

vi script.sql
use db1
create table t1(c1 varchar(20));
insert into t1 values('hello world');

2. Build a customized docker image

docker build -t martin/mysql .

docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    martin/mysql        latest              2c4056278d3f        8 seconds ago       456MB
    mysql               latest              d435eee2caa5        2 weeks ago         456MB

3. Run a container with the customized image

docker run -d --name c_mysql2 -p 3306:3306 martin/mysql
    10c9d96a572e7305c443171fe9340eb8fc935155825a3d9c13e56eaff598c9f6

docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
    241fd5e41ae1        mysql:latest        "docker-entrypoint.s…"   12 minutes ago      Exited (0) 7 minutes ago                       c_mysql
    10c9d96a572e        martin/mysql        "docker-entrypoint.s…"   23 seconds ago      Exited (1) 8 seconds ago                       c_mysql2

docker start c_mysql2
docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                     PORTS                               NAMES
    10c9d96a572e        martin/mysql        "docker-entrypoint.s…"   About a minute ago   Up 1 second                0.0.0.0:3306->3306/tcp, 33060/tcp   c_mysql2
    241fd5e41ae1        mysql:latest        "docker-entrypoint.s…"   13 minutes ago       Exited (0) 9 minutes ago                                       c_mysql

4. Connect to MySQL container OS

docker exec -it c_mysql2 /bin/bash
root@8cc858df8e40:/# mysql -uu1 -pp1
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases; 
+--------------------+
| Database           |
+--------------------+
| db1                |
| information_schema |
+--------------------+
2 rows in set (0.01 sec)

mysql> use db1
Database changed

mysql> show tables; 
+---------------+
| Tables_in_db1 |
+---------------+
| t1            |
+---------------+
1 row in set (0.00 sec)

mysql> select * from t1; 
+-------------+
| c1          |
+-------------+
| hello world |
+-------------+
1 row in set (0.00 sec)