Skip to main content
MySQL
Docker Containerization
©2020 Shrenik Parekh. All rights reserved.
1
2
©2020 Shrenik Parekh. All rights reserved.
Docker Introduction
● Container can be installed on Bare-metal, Physical / Virtual Machine,
Cloud instance
● Host could be Windows, Linux or Mac OS X
● Docker image run as a single process
● Docker images are available on Docker
One image can fire many containers
● Package an application with all dependencies
● Lightweight virtual machine
● Doesn’t require operating system
● Linux kernel libraries shared with containers
● Each container runs specific task
3
©2020 Shrenik Parekh. All rights reserved.
Docker Network
Docker Network drivers: Pluggable,
● Bridge – Default, used when application runs in standalone
mode
● Host – For standalone container, no network isolation between
container and docker host, use host network directly
● Overlay – Multi host networking
● Macvlam -
● Third party Network Plugins
Ref.:
https://docs.docker.com/network/
https://www.docker.com/blog/understanding-docker-networking-drivers-use-cases/
4
©2020 Shrenik Parekh. All rights reserved.
Bridge Network
● Default, simple to use, simple to troubleshoot, good choice for
developer
● The bridge driver is a local scope driver, it only provides
service discovery, IPAM (IP address management), and
connectivity on a single host
● Creates private network internal to the host
● External access granted by exposing ports
● Secures the network by managing rules that block connectivity
between different Docker networks
● Docker Engine creates the necessary Linux bridges, internal
interfaces, iptables rules
● Host routes to make this connectivity possible
5
©2020 Shrenik Parekh. All rights reserved.
Bridge Network
6
©2020 Shrenik Parekh. All rights reserved.
Bridge Network
Ref.: https://medium.com/@jamesemyn/basic-docker-networking-9130ab889359
7
©2020 Shrenik Parekh. All rights reserved.
Create Bridge Network
Create Docker bridge mybridge:
$docker network create -d bridge mybridge
Run Docker container db:
$docker run -d --net mybridge --name db redis
Run docker container web:
$docker run -d --net mybridge -e DB=db -p 8000:5000 --
name web chrch/web
8
©2020 Shrenik Parekh. All rights reserved.
Host Network
● For standalone container
● No network isolation between container and docker host
● Use host network directly
● Container does not get its own IP-address allocated
● port-mapping does not take effect if you specify
● The -p, --publish, -P, and --publish-all option are ignored, and
produce warning
● Host mode networking can be useful to optimize performance, and in
situations where a container needs to handle a large range of ports,
as it does not require network address translation (NAT), and no
“userland-proxy” is created for each port
● The host networking driver only works on Linux hosts
● Not supported on Docker Desktop for Mac, Docker Desktop for
Windows, or Docker EE for Windows Server
9
©2020 Shrenik Parekh. All rights reserved.
Host Network
Ref.: https://medium.com/@jamesemyn/basic-docker-networking-9130ab889359
10
©2020 Shrenik Parekh. All rights reserved.
Overlay Network
● Built in, multi host networking, swarm scope driver
● Operates across an entire Swarm or UCP cluster
● IPAM, service discovery, multi-host connectivity, encryption,
and load balancing are built right in
● Driver utilizes an industry-standard VXLAN data plane that
decouples the container network from the underlying physical
network (the underlay).
● Network policy, visibility, and security is controlled centrally
through the Docker Universal Control Plane (UCP).
11
©2020 Shrenik Parekh. All rights reserved.
Overlay Network
Ref.: https://www.docker.com/blog/understanding-docker-networking-drivers-use-cases/
12
©2020 Shrenik Parekh. All rights reserved.
MySQL Docker Container
Common commands use for MySQL running in container
Find Docker Version:
# docker –v
List Docker Container:
# docker ps –a
Fetch MySQL logs:
# docker logs <container_name> 2>&1 | grep GENERATED
Connect MySQL:
#docker exec -it <container_name> mysql -uroot -p
13
©2020 Shrenik Parekh. All rights reserved.
MySQL Docker Container
Connect MySQL file system in Docker:
#docker exec -it <container_name> bash
Backup MySQL databases using mysqldump:
# docker exec [MYSQL_CONTAINER] /usr/bin/mysqldump 
-u [MYSQL_USER] --password=[MYSQL_PASSWORD] 
--all-databases > backup.sql
Restore MySQL databases:
# cat backup.sql | docker exec -i [MYSQL_CONTAINER]
/usr/bin/mysql -u [MYSQL_USER] --password=[MYSQL_PASSWORD]
[MYSQL_DATABASE]
14
©2020 Shrenik Parekh. All rights reserved.
Master Slave Replication - Same Host
Ref.: http://khmel.org/docker-containers-with-mysql-master-slave-replication.html
15
©2020 Shrenik Parekh. All rights reserved.
Master Slave Replication - Different Host
16
©2020 Shrenik Parekh. All rights reserved.
Persistent MySQL Docker Container
17
©2020 Shrenik Parekh. All rights reserved.
Backup MySQL Database in Container
https://storageswiss.com/2016/05/19/backing-up-
databases-in-docker-containers/