How to run MySQL Database via a Docker Container

Using MySQL server via container is a lot simpler and easier to use than the original route; all we need to do is just pull the appropriate image which contains MySQL installed and connect to it.

How do you view logs for a container?

To view logs for running containers, we can use LOG command.

  > docker logs --follow

The --follow flag will continue streaming the new output from the container's STDOUT and STDERR onto the terminal.

What are restart policies?

docker restart policies dictate how a container should behave when it is shutted down by some factor.

There are four restart-policies:

  • No - Never restart when stopped by some means
  • Always - Always restart no matter how it is stopped
  • On-Failure - Restart only when the container stops with an erroneous code
  • Unless-Stopped - Restart always unless stopped manually with a KILL or STOP

What do you mean by port mapping in containers?

Port Mapping is a term used to link a specific port within the container where the application is presumed to be running to a port of the host system where the container is running. Since docker runs containers in an isolated virtual environment, it is not possible for the user to directly access the port within a docker container from outside. By mapping ports, we instruct docker to pass all the requests to a specific host port to a port inside the container.

> docker run -p <host_port>:<container_port> <container_id>

Example:

> docker run -p 8080:80 nginx