Home docker
Post
Cancel

docker

Dumping docker commands while I learn it;

working with mysql docker container (ref: Docker Store)

Start a mysql server instance

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

… where some-mysql is the name you want to assign to your container, my-secret-pw is the password to be set for the MySQL root user and tag is the tag specifying the MySQL version you want. See the list above for relevant tags.

eg. for MySQL version 5.7

docker run --name my-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pwd -d mysql:5.7

show docker images that are running

1
2
3
4
    [root@lnhcp017adm]~# docker ps
    CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
    197d1ea09b0c        mysql:5.7           "docker-entrypoint.s   23 minutes ago      Up 5 minutes        3306/tcp            my-mysql

use docker ps -a to show all images

stop / start an image

1
2
3
4
5
6
7
    [root@lnhcp017adm]~# docker stop 197d1ea09b0c
    197d1ea09b0c
    [root@lnhcp017adm]~# docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    [root@lnhcp017adm]~# docker start 197d1ea09b0c
    197d1ea09b0c

run a shell inside the container

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    [root@lnhcp017adm]~# docker start 197d1ea09b0c
    197d1ea09b0c
    [root@lnhcp017adm]~# docker exec -it my-mysql bash
    root@197d1ea09b0c:/# mysql -pmy-secret-pwd
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.15 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>

connect to the instance from outside (if the tcp ports are open)

find the ip its running on via docker inspect

1
2
3
4
5
6
7
8
9
10
11
12
    [root@lnhcp017adm]~# docker inspect 197d1ea09b0c
    [
    {
        "Id": "197d1ea09b0cba5b4cbb9785a861eb7772e2520137fd67b36ad1b5b3084a4def",
        "Created": "2016-10-04T08:14:56.717041992Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
            "Running": true,
            "Paused": false,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
    "NetworkSettings": {
        "Bridge": "",
        "EndpointID": "1ce1359ce14567b5c1d68d5db946dbbf670a198233f13a656ca28a6564a50757",
        "Gateway": "172.17.42.1",
        "GlobalIPv6Address": "",
...
        "Ports": {
            "3306/tcp": null
        },
...
        "Env": [
            "MYSQL_ROOT_PASSWORD=my-secret-pwd",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
...
        "OnBuild": null,
        "Labels": {}
    }
}
]
1
2
So we can connect on 172.17.0.18

[sotirk@lnhcp017adm] ~ $ mysql -h 172.17.0.18 -u root -pmy-secret-pwd 1 \u21b5 Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.15 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>

1
2
3
4
5
6
7
8
9
## Makefiles

This makefile is designed to build and test a new commit before
deploying to k8s. The build step will replace a ${TAG} placeholder in
the template deployment file with the latest. What it doesn't do well is
clean up old images as the deepclean stage looks for the current image,
rather than the previous.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
NAME := cats-dogs
TAG := $$(git log -1 --format=%h)
IMG := ${NAME}:${TAG}
LATEST := ${NAME}:latest
K8S_FILES := k8s
K8S_BUILD := build_k8s

.PHONY: default
default: help

.PHONY: build
build:
    @docker build -t ${IMG} .
    @docker tag ${IMG} ${LATEST}

.PHONY: test
test:
    @docker run -d --rm --name ${NAME}-test ${NAME}:latest
    @ci/scripts/${NAME}-test.sh ${NAME}-test

.PHONY: clean
clean:
    @docker rm ${NAME} && echo "${NAME} container removed" || echo "container already removed"

.PHONY: deepclean
deepclean:
    @docker rm ${NAME} && echo "${NAME} container removed" || echo "container already removed"
    @docker rmi ${IMG} && echo "${IMG} image removed" || echo "image already removed"

.PHONY: rebuild
rebuild: deepclean build

.PHONY: build-k8s
build-k8s:
    TAG=${TAG} envsubst <${K8S_FILES}/cats-dogs-deployment.yaml > ${K8S_BUILD}/cats-dogs-deployment.yaml

.PHONY: deploy
deploy: deepclean build test build-k8s
    kubectl apply -f ${K8S_BUILD}/cats-dogs-deployment.yaml

.PHONY: help
help:
    @echo ''
    @echo 'Usage: make [TARGET]'
    @echo 'Targets:'
    @echo ' build   build the container'
    @echo ' run     run the container with interactive shell'
    @echo ' login     startup and attach to the container'
    @echo ' clean   remove container'
    @echo ' deepclean remove both container and image'
    @echo ' rebuild deepclean then build'
    @echo ' build-k8s create deployment file with latest tagged version of code'
    @echo ' deploy apply the new k8s deployment'
1
2
3
The test above calls this script which checks for a 200 code on a health
endpoint;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
CONTAINER=$1
if [ "$CONTAINER" = "" ]; then
  echo "${RED}Container name not passed"
  exit 1
fi

function stop {
  docker stop $1
}

IP=$(docker inspect -f '\{\{range.NetworkSettings.Networks\}\}\{\{.IPAddress\}\}\{\{end\}\}' $1)
echo -e "${GREEN}Pausing to let container start${NC}"
sleep 5
RESPONSE=$(curl --write-out '%{http_code}' --silent --output /dev/null  http://$IP/health)

if [ $RESPONSE -eq 200 ]; then
        echo -e "${GREEN}${RESPONSE}....passed${NC}"
        stop $CONTAINER
        exit 0
else
        echo -e "${RED}${RESPONSE}...failed!"
        stop $CONTAINER
        exit 1
fi ```
This post is licensed under CC BY 4.0 by the author.