Docker – How to Connect to a MySQL Running Container

1. What we want to achieve:

We used the official mysql image from Docker Hub (https://hub.docker.com/_/mysql/), and created a running database container.

Now we need to connect to the MySQL database using MySQL command line client, to do some database operation.

 

2. Solution1:

Bash into the running container and run MySQL client.

2.1 Bash into the running MySQL container:

$ docker exec -t -i <container_name> /bin/bash

2.2 Run MySQL client:

$ mysql -u “<useranme>” -p

 

3. Solution2:

Use MySQl Command Line Client.

Reference: MySQL Image Official Doc

In the official guide, it states:

The following command starts another mysql container instance and runs the mysql command line client against your original mysql container, allowing you to execute SQL statements against your database instance:

$ docker run -it --link some-mysql:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'

The variables here are really confusing.  Here is a more readable version of this command:

$ docker run -it --link <container_name>:<image_name> --rm <image_name> sh -c 'exec mysql -h"<container_ip_address>" -P"<mysql_port_number>" -u"<mysql_username>" -p"<mysql_password>"'

Now, lets use an example to explain how to find all the variables.

 

3.1 Get container_name, image_name, mysql_port_number:

$ docker ps

1

We can get:

container_name: romuiweb_db_1

image_name: mysql

container_port_number: 3306

 

3.2 Get container_ip_address:

$ docker inspect –format ‘{{ .NetworkSettings.IPAddress }}’ <container_id>

1

 

3.3 Get mysql_username, mysql_password:

When we start the mysql image, we set environment variables to decide the username and password.

username is default to root.

password is set by MYSQL_ROOT_PASSWORD, and we can find it in docker-compose.yml file:

1

So we can get:

mysql_username: root,

mysql_password: password.

 

3.4 Create the real command:

Now we have all the values of the variables. We can construct the real command:

$ docker run -it –link romuiweb_db_1:mysql –rm mysql sh -c ‘exec mysql -h”172.17.0.2″ -P”3306″ -u”root” -p”password”‘

 

3.5 Run the command, gain access to MySQL:

1

 

If it works, skip the following problem solving part.

 

3.6 Problem Solving:

(In this problem, we are not using romuiweb_db_1 we used earlier. We switch to docker_db_1. The Ip Address is different now)

docker: Error reponse from daemon: Cannot link to <container_name>, as it does not belong to the default work.

$ docker run -it –link docker_db_1:mysql –rm mysql sh -c ‘exec mysql -h”172.20.0.3″ -P”3306″ -u”root” -p”password”‘

1

This means that we are not running the mysql connect container on the same network the mysql container is currently running on. We need to connect it to the same network use –net option. Reference: https://docs.docker.com/engine/userguide/containers/networkingcontainers/

Step1: Find out which network mysql container is running on:

$ docker inspect docker_db_1

Find the “Networks”:

1

As we can see from the screenshot above, the network docker_db_1 is running on is “docker_default”. We need to connect to this network. So we change out command to this:

$ docker run -it –net=docker_default –link docker_db_1:mysql –rm mysql sh -c ‘exec mysql -h”172.20.0.3″ -P”3306″ -u”root” -p”password”‘

After adding the network argument, we successfully connected:

1

Advertisements

Docker – SSH to Docker Machine – remote host identification has changed!

Since I’ve been deleting a docker-machine and recreate constantly, and the docker-machine ip is dynamically assigned, I got the “warning: remote host identification has changed!” a lot.

Here is the solution:

http://stackoverflow.com/questions/20840012/ssh-remote-host-identification-has-change

 

And the following is the screenshot of my process:

1.could not connect, got error message

2.run solution commend

3.successfully connected

1

Docker – Overview

1. How does Docker work?
1.1 Build Docker Images that hold your applications.
1.2 Create Docker Containers from those Docker Images to run your applications.
1.3 Share Docker images via Docker Hub or your own registry.

 

2.Docker container lifecycle

– Conception
Build an image from a dockerfile
– Birth
Run (create + start) a container
– Reproduction
Run a new container from an image
– Sleep
Kill a running container
– Wake
Start a stopped container
– Death
RM (delete) a stopped container
-Exitinction
RMI (delete) a container image

 

3.Docker Commands (Used in Dockerfile)

Attention: all relative paths are relative to the Dockerfile

FROM    Specifies a source image for our new image that we plan to build. (mandatory)

MAINTAINER    Tells us who built/maintains the image.

RUN   Executes a command.

CMD   Default command to run when a container is launched.

EXPOSE   Lists the network ports to open when a container is launched from this image.

ADD   Adds files and contents from host into the image.

Adds files as owned by ‘root’ with permissions 0755.

COPY   Copies the files.

The source directory must be inside the context of build (one of directories/sub-directories of the current directory where Dockerfile is located).

VOLUME   Creates a data volume mount point at the specified path.

Data volumes can be shared and reused between containers.

Data volumes persist until all containers referencing them are destroyed.

ENV   Specifies environment variables that should be set in any container launched from the image.

ENTRYPOINT   The first command/script that gets executed.

Docker – Commands Reference

1.General Docker command

List info about the Docker Client:

$ docker version

Get docker command help:

—- List all commands:

$ docker –help

—- For a specific command:

$ docker <command> –help

— List networks:

$ docker network ls

 

2.Containers

— List containers:

—- List containers that are up:

$ docker ps

—- List the last started container:

$ docker ps -l

—- List all the containers:

$ docker ps -a

Start a container:

$ docker start <container_id>

— Stop containers:

—- Stop a container:

$ docker stop <container_id>

—- Stop all containers:

$ docker stop $(docker ps -a -q)

Restart a container:

$ docker restart <container_id>

— Remove a container:

$ docker rm <container_id>

— Delete all containers:

$ docker rm $(docker ps -a -q)

— Run a container(creates and starts the container):

—- Run a container:

$ docker run <image_name> <command>

For ex:

$ docker run ubuntu /bin/echo ‘Hello world’

—- Run a container in interactive mode:

$ docker run -t -i <image_name> <command>

This will launch a Bash shell inside our container.

—- Run a container in daemonized mode:

$ docker run -d <image_name> <command>

This will run the container and put it in the background, to daemonize it.

Commit (save) a container state

$ docker commit <container> <some_name>

— Get container info:

$ docker inspect <container_id>

Returns a JSON document with configuration and status info.

We can also narrow down the info we want to return by requesting a specific element.

For example, this gives the IP address of the container:

$ docker inspect –format ‘{{ .NetworkSettings.IPAddress }}’ <container_id>

Return container standard output:

$ docker logs <container_id>

Acts like tail -f:

$ docker logs -f <container_id>

Examine processes inside a container:

$ docker top <container_name>

— Show container size:

$ docker ps -s

 

3.Images

— Create an image:

—- Use Dockerfile:

$ docker build -t <username/image_name:tag> .

The -t flag is to identify the new docker image as belonging to <username/image_name:tag>.

. is to indicate use the Dockerfile in the current directory.

—- Use commit command:

$ docker commit -m “<message>” -a “<author>” <container_id> <docker_hub_username>/<image_name>:<tag>

For ex: $ docker commit -m “Added json gem” -a “Kate Smith” 0b2616b0e5a8 ouruser/sinatra:v2

— List images:

$ docker images

— Remove an image:

$ docker rmi <image_id>

— Delete all images:

$ docker rmi $(docker images -q)

Pull an image

$ docker pull <image_name>

Pulls an image from docker hub/specified host.

— Build an image

$ docker build -t <host/img_name:tag> .

Builds an image with specified hostname as prefix.

— Push an image

$ docker push <username/image_name>

Pushes the docker image to the specified host/docker hub.

 

4.docker-compose

docker-compose build:

Build the containers that the web app and db will live in, and link them up.

— docker-compose run:

Run one-off commands for services. For example, to see environment variables are available to the web service:

$ docker-compose run web env

— docker-compose up:

Build, link, and start services.

 

5.docker-machine

–Create a docker machine:

$ docker-machine create –driver virtualbox <docker_machine_name>

–List docker machines:

$ docker-machine ls

–Restart docker machine:

$ docker-machine restart <docker_machine_name>

–Stop docker machine:

$ docker-machine stop <docker_machine_name>

–Connect to a docker machine:

$docker-machine ssh <docker_machine_name>

 

6.Useful commands

Check iptables:

$ sudo iptables -t nat -L -n

— Find the PID of the process:

$ ps aux | grep <process_name>

— Kill a process:

$ kill <pid>

Docker – 3 Ways to Access Docker VM on Windows

1.Docker Quickstart Terminal

1

Start “Docker Quickstart Terminal” and type in

$ docker-machine ssh <docker machine name>

1

 

2. gitBash

Start “gitBash” and type in

$ docker-machine ssh <docker machine name>

1

 

3. Powershell or CMD

Here use powershell as an example:

3.1 Get VM IP

$ docker-machine ip <docker_machine_name>

1

3.2 SSH to VM

username: docker

password: tcuser

$ssh docker@<ip>

1

 

Docker – Install Docker-Compose on Windows

Reference Docker Compose official installation guide: https://docs.docker.com/compose/install/

 

Actually Docker-Compose is not supported by Windows. What this post will talk about is how to use Docker-Compose on Windows.

Pre-requisite: Docker Machine has been installed.

Solution: Install Docker-Compose inside linux Virtual Machine.

 

1. SSH to VM:

$ docker-machine ssh

 

2.Run as root user

$ sudo -i

 

3.Run the following two commands (warning: Don’t use the command in “Install as a container” section from the official guide. It doesn’t work!)

$ curl -L https://github.com/docker/compose/releases/download/1.6.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

$ chmod +x /usr/local/bin/docker-compose

 

4. Exit root mode

$ exit

 

5. Test Docker-Compose has been installed correctly

$ Docker-Compose –version