Setup MySQL Docker Container

Docker is becoming mainstreamline to package and deploy self sufficient application containers. It wrap up a piece of software in a complete filesystem that contains everything it needs to run: code, runtime, system tools, system libraries – anything you can install on a server. This guarantees that it will always run the same, regardless of the environment it is running in. The same Linux kernel and libraries can be shared between multiple containers running on the host.

Please visit my blog previous blog Install Docker to setup docker.

Let’s Started

Docker hub provides images for all the software and tools. let’s find out the mysql docker image.

MySQL Docker Images

There are many MySQL Docker images available, but it is always better to go with the official release. As they are highy optimised, bug free and stable. You can download to your computer using pull command. After that we can list down the docker images.

krishna@ubuntu:~$ sudo docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
6d827a3ef358: Pull complete 
ed0929eb7dfe: Pull complete 
03f348dc3b9d: Pull complete 
fd337761ca76: Pull complete 
7e6cc16d464a: Pull complete 
ca3d380bc018: Pull complete 
23c12ddae61f: Pull complete 
14553c628372: Pull complete 
c9445076b453: Pull complete 
8feabd297745: Pull complete 
835cd3cde5d5: Pull complete 
Digest: sha256:3ea679cbde178e346dcdeb538fd1ea4f1af256020ebeb464ccb72a1646a2ba6d
Status: Downloaded newer image for mysql:latest
krishna@ubuntu:~$
krishna@ubuntu:~$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               latest              d5127813070b        7 days ago          407MB
hello-world         latest              48b5124b2768        3 months ago        1.84kB
krishna@ubuntu:~$

Run MySQL Container

After an image has been downloaded, you may then run a container using the downloaded image with the run subcommand. If an image has not been downloaded when docker is executed with the run subcommand, the Docker client will first download the image, then run a container using it.

krishna@ubuntu:~$ sudo docker run --name=mysql_server --env="MYSQL_ROOT_PASSWORD=Password" mysql &
[1] 6325
krishna@ubuntu:~$ Initializing database
2017-04-19T07:39:43.666494Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-04-19T07:39:43.847632Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-04-19T07:39:43.889557Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-04-19T07:39:43.973632Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5ae4d996-24d3-11e7-ac7d-0242ac110002.
2017-04-19T07:39:43.975971Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-04-19T07:39:43.976496Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2017-04-19T07:39:44.472158Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:44.472322Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:44.472468Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:44.472517Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:44.472617Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
Database initialized
Initializing certificates
Generating a 2048 bit RSA private key
......................+++
......................................................+++
unable to write 'random state'
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
.............................................+++
...............+++
unable to write 'random state'
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
..........................+++
..........+++
unable to write 'random state'
writing new private key to 'client-key.pem'
-----
Certificates initialized
MySQL init process in progress...
2017-04-19T07:39:47.170658Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-04-19T07:39:47.172546Z 0 [Note] mysqld (mysqld 5.7.18) starting as process 87 ...
2017-04-19T07:39:47.176285Z 0 [Note] InnoDB: PUNCH HOLE support available
2017-04-19T07:39:47.176374Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-04-19T07:39:47.176394Z 0 [Note] InnoDB: Uses event mutexes
2017-04-19T07:39:47.176412Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2017-04-19T07:39:47.176425Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-04-19T07:39:47.176438Z 0 [Note] InnoDB: Using Linux native AIO
2017-04-19T07:39:47.176641Z 0 [Note] InnoDB: Number of pools: 1
2017-04-19T07:39:47.176737Z 0 [Note] InnoDB: Using CPU crc32 instructions
2017-04-19T07:39:47.179065Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2017-04-19T07:39:47.188199Z 0 [Note] InnoDB: Completed initialization of buffer pool
2017-04-19T07:39:47.191169Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2017-04-19T07:39:47.204591Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2017-04-19T07:39:47.213538Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2017-04-19T07:39:47.213715Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2017-04-19T07:39:47.235947Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2017-04-19T07:39:47.236896Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2017-04-19T07:39:47.236976Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2017-04-19T07:39:47.238469Z 0 [Note] InnoDB: Waiting for purge to start
2017-04-19T07:39:47.289800Z 0 [Note] InnoDB: 5.7.18 started; log sequence number 2535558
2017-04-19T07:39:47.290160Z 0 [Note] Plugin 'FEDERATED' is disabled.
2017-04-19T07:39:47.298336Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2017-04-19T07:39:47.298500Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2017-04-19T07:39:47.299414Z 0 [Note] InnoDB: Buffer pool(s) load completed at 170419 7:39:47
2017-04-19T07:39:47.299686Z 0 [Warning] CA certificate ca.pem is self signed.
2017-04-19T07:39:47.305550Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:47.305666Z 0 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:47.305703Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:47.305724Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:47.306748Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:47.311989Z 0 [Note] Event Scheduler: Loaded 0 events
2017-04-19T07:39:47.312147Z 0 [Note] mysqld: ready for connections.
Version: '5.7.18' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server (GPL)
2017-04-19T07:39:47.312169Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check. 
2017-04-19T07:39:47.312199Z 0 [Note] Beginning of list of non-natively partitioned tables
2017-04-19T07:39:47.322969Z 0 [Note] End of list of non-natively partitioned tables
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
2017-04-19T07:39:49.642500Z 5 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:49.642604Z 5 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:49.642641Z 5 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:49.642662Z 5 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:49.642702Z 5 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:49.643788Z 0 [Note] Giving 0 client threads a chance to die gracefully
2017-04-19T07:39:49.643820Z 0 [Note] Shutting down slave threads
2017-04-19T07:39:49.643833Z 0 [Note] Forcefully disconnecting 0 remaining clients
2017-04-19T07:39:49.643847Z 0 [Note] Event Scheduler: Purging the queue. 0 events
2017-04-19T07:39:49.643885Z 0 [Note] Binlog end
2017-04-19T07:39:49.644429Z 0 [Note] Shutting down plugin 'ngram'
2017-04-19T07:39:49.644458Z 0 [Note] Shutting down plugin 'BLACKHOLE'
2017-04-19T07:39:49.644472Z 0 [Note] Shutting down plugin 'partition'
2017-04-19T07:39:49.644483Z 0 [Note] Shutting down plugin 'ARCHIVE'
2017-04-19T07:39:49.644493Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
2017-04-19T07:39:49.644504Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
2017-04-19T07:39:49.644515Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
2017-04-19T07:39:49.644525Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
2017-04-19T07:39:49.644535Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
2017-04-19T07:39:49.644545Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
2017-04-19T07:39:49.644555Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
2017-04-19T07:39:49.644565Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
2017-04-19T07:39:49.644575Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
2017-04-19T07:39:49.644586Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
2017-04-19T07:39:49.644608Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
2017-04-19T07:39:49.644619Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
2017-04-19T07:39:49.644629Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
2017-04-19T07:39:49.644639Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
2017-04-19T07:39:49.644649Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
2017-04-19T07:39:49.644659Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
2017-04-19T07:39:49.644670Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
2017-04-19T07:39:49.644680Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
2017-04-19T07:39:49.644690Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
2017-04-19T07:39:49.644700Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
2017-04-19T07:39:49.644711Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
2017-04-19T07:39:49.644721Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
2017-04-19T07:39:49.644731Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
2017-04-19T07:39:49.644741Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
2017-04-19T07:39:49.644751Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
2017-04-19T07:39:49.644762Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
2017-04-19T07:39:49.644772Z 0 [Note] Shutting down plugin 'INNODB_CMP'
2017-04-19T07:39:49.644782Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
2017-04-19T07:39:49.644793Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
2017-04-19T07:39:49.644803Z 0 [Note] Shutting down plugin 'INNODB_TRX'
2017-04-19T07:39:49.644813Z 0 [Note] Shutting down plugin 'InnoDB'
2017-04-19T07:39:49.644871Z 0 [Note] InnoDB: FTS optimize thread exiting.
2017-04-19T07:39:49.644955Z 0 [Note] InnoDB: Starting shutdown...

2017-04-19T07:39:49.746056Z 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool
2017-04-19T07:39:49.746510Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 170419 7:39:49
2017-04-19T07:39:51.270391Z 0 [Note] InnoDB: Shutdown completed; log sequence number 12131125
2017-04-19T07:39:51.271707Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
2017-04-19T07:39:51.271735Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
2017-04-19T07:39:51.271752Z 0 [Note] Shutting down plugin 'MyISAM'
2017-04-19T07:39:51.271768Z 0 [Note] Shutting down plugin 'CSV'
2017-04-19T07:39:51.271781Z 0 [Note] Shutting down plugin 'MEMORY'
2017-04-19T07:39:51.271793Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
2017-04-19T07:39:51.271823Z 0 [Note] Shutting down plugin 'sha256_password'
2017-04-19T07:39:51.271835Z 0 [Note] Shutting down plugin 'mysql_native_password'
2017-04-19T07:39:51.271994Z 0 [Note] Shutting down plugin 'binlog'
2017-04-19T07:39:51.272551Z 0 [Note] mysqld: Shutdown complete


MySQL init process done. Ready for start up.

2017-04-19T07:39:51.453330Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-04-19T07:39:51.454385Z 0 [Note] mysqld (mysqld 5.7.18) starting as process 1 ...
2017-04-19T07:39:51.456972Z 0 [Note] InnoDB: PUNCH HOLE support available
2017-04-19T07:39:51.457021Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-04-19T07:39:51.457038Z 0 [Note] InnoDB: Uses event mutexes
2017-04-19T07:39:51.457055Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2017-04-19T07:39:51.457068Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-04-19T07:39:51.457079Z 0 [Note] InnoDB: Using Linux native AIO
2017-04-19T07:39:51.457277Z 0 [Note] InnoDB: Number of pools: 1
2017-04-19T07:39:51.457379Z 0 [Note] InnoDB: Using CPU crc32 instructions
2017-04-19T07:39:51.458535Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2017-04-19T07:39:51.465104Z 0 [Note] InnoDB: Completed initialization of buffer pool
2017-04-19T07:39:51.466885Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2017-04-19T07:39:51.478972Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2017-04-19T07:39:51.486072Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2017-04-19T07:39:51.486213Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2017-04-19T07:39:51.506308Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2017-04-19T07:39:51.507066Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2017-04-19T07:39:51.507103Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2017-04-19T07:39:51.507449Z 0 [Note] InnoDB: Waiting for purge to start
2017-04-19T07:39:51.558664Z 0 [Note] InnoDB: 5.7.18 started; log sequence number 12131125
2017-04-19T07:39:51.559252Z 0 [Note] Plugin 'FEDERATED' is disabled.
2017-04-19T07:39:51.562374Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2017-04-19T07:39:51.562595Z 0 [Warning] CA certificate ca.pem is self signed.
2017-04-19T07:39:51.564136Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2017-04-19T07:39:51.564223Z 0 [Note] IPv6 is available.
2017-04-19T07:39:51.564244Z 0 [Note] - '::' resolves to '::';
2017-04-19T07:39:51.564268Z 0 [Note] Server socket created on IP: '::'.
2017-04-19T07:39:51.564682Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2017-04-19T07:39:51.566760Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:51.568776Z 0 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:51.568850Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:51.568872Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:51.570718Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-04-19T07:39:51.568732Z 0 [Note] InnoDB: Buffer pool(s) load completed at 170419 7:39:51
2017-04-19T07:39:51.575230Z 0 [Note] Event Scheduler: Loaded 0 events
2017-04-19T07:39:51.575393Z 0 [Note] mysqld: ready for connections.
Version: '5.7.18' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
2017-04-19T07:39:51.575414Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check. 
2017-04-19T07:39:51.575431Z 0 [Note] Beginning of list of non-natively partitioned tables
2017-04-19T07:39:51.583725Z 0 [Note] End of list of non-natively partitioned tables
krishna@ubuntu:~$ 

Looks good. Our MySQL Container is running now. As per the details from above logs.

Let’s verify the status of the container.

krishna@ubuntu:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a2e44d353cbb        mysql               "docker-entrypoint..."   4 minutes ago       Up 4 minutes        3306/tcp            mysql_server
krishna@ubuntu:~$ 

MySQL container is now running and accessible on port 3306 of that container. Use the following command to see what happened during the container startup:

krishna@ubuntu:~$ sudo docker logs mysql_server

Connecting to Container

Next, we retrieve the IP address of that container in order to access it. Run the inspect command:

krishna@ubuntu:~$ sudo docker inspect mysql_server
[
 {
 "Id": "a2e44d353cbbd073195d7dd919348a5d3e77d1b0b192ff52dd9aa12b29db0a6c",
 "Created": "2017-04-19T07:39:43.408104524Z",
 "Path": "docker-entrypoint.sh",
 "Args": [
 "mysqld"
 ],
 "State": {
 "Status": "running",
 "Running": true,
 "Paused": false,
 "Restarting": false,
 "OOMKilled": false,
 "Dead": false,
 "Pid": 6409,
 "ExitCode": 0,
 "Error": "",
 "StartedAt": "2017-04-19T07:39:43.612970897Z",
 "FinishedAt": "0001-01-01T00:00:00Z"
 },
 "Image": "sha256:d5127813070b9b8d5600d69afc04965570dd97dbcd07dcbf51a37e26b74e2d5c",
 "ResolvConfPath": "/var/lib/docker/containers/a2e44d353cbbd073195d7dd919348a5d3e77d1b0b192ff52dd9aa12b29db0a6c/resolv.conf",
 "HostnamePath": "/var/lib/docker/containers/a2e44d353cbbd073195d7dd919348a5d3e77d1b0b192ff52dd9aa12b29db0a6c/hostname",
 "HostsPath": "/var/lib/docker/containers/a2e44d353cbbd073195d7dd919348a5d3e77d1b0b192ff52dd9aa12b29db0a6c/hosts",
 "LogPath": "/var/lib/docker/containers/a2e44d353cbbd073195d7dd919348a5d3e77d1b0b192ff52dd9aa12b29db0a6c/a2e44d353cbbd073195d7dd919348a5d3e77d1b0b192ff52dd9aa12b29db0a6c-json.log",
 "Name": "/mysql_server",
 "RestartCount": 0,
 "Driver": "aufs",
 "MountLabel": "",
 "ProcessLabel": "",
 "AppArmorProfile": "docker-default",
 "ExecIDs": null,
 "HostConfig": {
 "Binds": null,
 "ContainerIDFile": "",
 "LogConfig": {
 "Type": "json-file",
 "Config": {}
 },
 "NetworkMode": "default",
 "PortBindings": {},
 "RestartPolicy": {
 "Name": "no",
 "MaximumRetryCount": 0
 },
 "AutoRemove": false,
 "VolumeDriver": "",
 "VolumesFrom": null,
 "CapAdd": null,
 "CapDrop": null,
 "Dns": [],
 "DnsOptions": [],
 "DnsSearch": [],
 "ExtraHosts": null,
 "GroupAdd": null,
 "IpcMode": "",
 "Cgroup": "",
 "Links": null,
 "OomScoreAdj": 0,
 "PidMode": "",
 "Privileged": false,
 "PublishAllPorts": false,
 "ReadonlyRootfs": false,
 "SecurityOpt": null,
 "UTSMode": "",
 "UsernsMode": "",
 "ShmSize": 67108864,
 "Runtime": "runc",
 "ConsoleSize": [
 0,
 0
 ],
 "Isolation": "",
 "CpuShares": 0,
 "Memory": 0,
 "NanoCpus": 0,
 "CgroupParent": "",
 "BlkioWeight": 0,
 "BlkioWeightDevice": null,
 "BlkioDeviceReadBps": null,
 "BlkioDeviceWriteBps": null,
 "BlkioDeviceReadIOps": null,
 "BlkioDeviceWriteIOps": null,
 "CpuPeriod": 0,
 "CpuQuota": 0,
 "CpuRealtimePeriod": 0,
 "CpuRealtimeRuntime": 0,
 "CpusetCpus": "",
 "CpusetMems": "",
 "Devices": [],
 "DeviceCgroupRules": null,
 "DiskQuota": 0,
 "KernelMemory": 0,
 "MemoryReservation": 0,
 "MemorySwap": 0,
 "MemorySwappiness": -1,
 "OomKillDisable": false,
 "PidsLimit": 0,
 "Ulimits": null,
 "CpuCount": 0,
 "CpuPercent": 0,
 "IOMaximumIOps": 0,
 "IOMaximumBandwidth": 0
 },
 "GraphDriver": {
 "Data": null,
 "Name": "aufs"
 },
 "Mounts": [
 {
 "Type": "volume",
 "Name": "ecaf1940cc942f5cdd4439e8f4945cbd32f7a6276e01754f4c2c1d71bdda742e",
 "Source": "/var/lib/docker/volumes/ecaf1940cc942f5cdd4439e8f4945cbd32f7a6276e01754f4c2c1d71bdda742e/_data",
 "Destination": "/var/lib/mysql",
 "Driver": "local",
 "Mode": "",
 "RW": true,
 "Propagation": ""
 }
 ],
 "Config": {
 "Hostname": "a2e44d353cbb",
 "Domainname": "",
 "User": "",
 "AttachStdin": false,
 "AttachStdout": true,
 "AttachStderr": true,
 "ExposedPorts": {
 "3306/tcp": {}
 },
 "Tty": false,
 "OpenStdin": false,
 "StdinOnce": false,
 "Env": [
 "MYSQL_ROOT_PASSWORD=Password",
 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
 "GOSU_VERSION=1.7",
 "MYSQL_MAJOR=5.7",
 "MYSQL_VERSION=5.7.18-1debian8"
 ],
 "Cmd": [
 "mysqld"
 ],
 "ArgsEscaped": true,
 "Image": "mysql",
 "Volumes": {
 "/var/lib/mysql": {}
 },
 "WorkingDir": "",
 "Entrypoint": [
 "docker-entrypoint.sh"
 ],
 "OnBuild": null,
 "Labels": {}
 },
 "NetworkSettings": {
 "Bridge": "",
 "SandboxID": "adf2ea533cff391d911d920043e23ea742163da57504f84edfa5c078213fde54",
 "HairpinMode": false,
 "LinkLocalIPv6Address": "",
 "LinkLocalIPv6PrefixLen": 0,
 "Ports": {
 "3306/tcp": null
 },
 "SandboxKey": "/var/run/docker/netns/adf2ea533cff",
 "SecondaryIPAddresses": null,
 "SecondaryIPv6Addresses": null,
 "EndpointID": "940be7be3cb15015af306c713d10eacab58a7de869d9bc032149831018f8dade",
 "Gateway": "172.17.0.1",
 "GlobalIPv6Address": "",
 "GlobalIPv6PrefixLen": 0,
 "IPAddress": "172.17.0.2",
 "IPPrefixLen": 16,
 "IPv6Gateway": "",
 "MacAddress": "02:42:ac:11:00:02",
 "Networks": {
 "bridge": {
 "IPAMConfig": null,
 "Links": null,
 "Aliases": null,
 "NetworkID": "e455cae4e0211f23a0cbe663aa8d4cc8da3f25f15a1abf588da61d373d3a5c08",
 "EndpointID": "940be7be3cb15015af306c713d10eacab58a7de869d9bc032149831018f8dade",
 "Gateway": "172.17.0.1",
 "IPAddress": "172.17.0.2",
 "IPPrefixLen": 16,
 "IPv6Gateway": "",
 "GlobalIPv6Address": "",
 "GlobalIPv6PrefixLen": 0,
 "MacAddress": "02:42:ac:11:00:02"
 }
 }
 }
 }
]
krishna@ubuntu:~$ 
krishna@ubuntu:~$ sudo docker inspect mysql_server | grep IPAddress
 "SecondaryIPAddresses": null,
 "IPAddress": "172.17.0.2",
 "IPAddress": "172.17.0.2",
krishna@ubuntu:~$ 

Let’s install mysql-client in order to acess MySQL Server

krishna@ubuntu:~$ sudo apt-get install mysql-client-5.7
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following additional packages will be installed:
 libaio1 mysql-client-core-5.7 mysql-common
The following NEW packages will be installed:
 libaio1 mysql-client-5.7 mysql-client-core-5.7 mysql-common
0 upgraded, 4 newly installed, 0 to remove and 46 not upgraded.
Need to get 8,204 kB of archives.
After this operation, 65.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us.archive.ubuntu.com/ubuntu xenial/main amd64 libaio1 amd64 0.3.110-2 [6,356 B]
Get:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mysql-client-core-5.7 amd64 5.7.17-0ubuntu0.16.04.2 [6,516 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mysql-common all 5.7.17-0ubuntu0.16.04.2 [15.7 kB] 
Get:4 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 mysql-client-5.7 amd64 5.7.17-0ubuntu0.16.04.2 [1,666 kB] 
Fetched 8,204 kB in 15s (518 kB/s) 
Selecting previously unselected package libaio1:amd64.
(Reading database ... 59922 files and directories currently installed.)
Preparing to unpack .../libaio1_0.3.110-2_amd64.deb ...
Unpacking libaio1:amd64 (0.3.110-2) ...
Selecting previously unselected package mysql-client-core-5.7.
Preparing to unpack .../mysql-client-core-5.7_5.7.17-0ubuntu0.16.04.2_amd64.deb ...
Unpacking mysql-client-core-5.7 (5.7.17-0ubuntu0.16.04.2) ...
Selecting previously unselected package mysql-common.
Preparing to unpack .../mysql-common_5.7.17-0ubuntu0.16.04.2_all.deb ...
Unpacking mysql-common (5.7.17-0ubuntu0.16.04.2) ...
Selecting previously unselected package mysql-client-5.7.
Preparing to unpack .../mysql-client-5.7_5.7.17-0ubuntu0.16.04.2_amd64.deb ...
Unpacking mysql-client-5.7 (5.7.17-0ubuntu0.16.04.2) ...
Processing triggers for libc-bin (2.23-0ubuntu5) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up libaio1:amd64 (0.3.110-2) ...
Setting up mysql-client-core-5.7 (5.7.17-0ubuntu0.16.04.2) ...
Setting up mysql-common (5.7.17-0ubuntu0.16.04.2) ...
update-alternatives: using /etc/mysql/my.cnf.fallback to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Setting up mysql-client-5.7 (5.7.17-0ubuntu0.16.04.2) ...
Processing triggers for libc-bin (2.23-0ubuntu5) ...
krishna@ubuntu:~$ 

krishna@ubuntu:~$ mysql -h 172.17.0.2 -uroot -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> 

Another Access Method

Your command prompt should change to reflect the fact that you’re now working inside the container and should take this form

root@a2e44d353cbb:/#

Now you may run any command inside the container.

krishna@ubuntu:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a2e44d353cbb        mysql               "docker-entrypoint..."   28 minutes ago      Up 28 minutes       3306/tcp            mysql_server
krishna@ubuntu:~$ 
krishna@ubuntu:~$ sudo docker exec -it mysql_server bash
root@a2e44d353cbb:/# 
root@a2e44d353cbb:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.18 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>

Conclusion

VOWW…. We now have a MySQL instance running in a container.

Docker allocates a dynamic IP address on every running container. Whenever a container is restarted, you will get a new IP address. You can get the IP address range from the Docker network interface in the Linux box. To sort this issue there is an option called –link

One Comment

  1. Krishna Prajapati

    I’m not using twitter at the moment.
    If you would like to follow, you can simply subscribe it http://exabig.com/blog/

Leave a Reply

Your email address will not be published. Required fields are marked *