Skip to content

Commit

Permalink
Cleanup & simplified README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
madharjan committed May 21, 2019
1 parent e95df01 commit 53fc8f4
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 79 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
include:
- stage: release
script:
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
- echo "${DOCKER_PASSWORD}" | docker login -u "${DOCKER_USERNAME}" --password-stdin
- make build DEBUG=false
- make release DEBUG=false
- make tag_latest DEBUG=false
- make release DEBUG=false
- curl -s -X POST "https://hooks.microbadger.com/images/${TRAVIS_REPO_SLUG}/jaJQb-O_tU-ZppG--6GHnJSaiBU="
1 change: 0 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

## 5.7

### Changes
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ENV MYSQL_VERSION ${MYSQL_VERSION}
RUN mkdir -p /build
COPY . /build

RUN /build/scripts/install.sh && /build/scripts/cleanup.sh
RUN chmod 755 /build/scripts/*.sh && /build/scripts/install.sh && /build/scripts/cleanup.sh

VOLUME ["/etc/mysql/conf.d", "/var/lib/mysql", "/var/log/mysql"]

Expand Down
44 changes: 30 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ VERSION = 5.7

DEBUG ?= true

.PHONY: all build run tests stop clean tag_latest release clean_images
DOCKER_USERNAME ?= $(shell read -p "DockerHub Username: " pwd; echo $$pwd)
DOCKER_PASSWORD ?= $(shell stty -echo; read -p "DockerHub Password: " pwd; stty echo; echo $$pwd)
DOCKER_LOGIN ?= $(shell cat ~/.docker/config.json | grep "docker.io" | wc -l)

.PHONY: all build run test stop clean tag_latest release clean_images

all: build

docker_login:
ifeq ($(DOCKER_LOGIN), 1)
@echo "Already login to DockerHub"
else
@docker login -u $(DOCKER_USERNAME) -p $(DOCKER_PASSWORD)
endif

build:
docker build \
--build-arg MYSQL_VERSION=$(VERSION) \
Expand All @@ -16,6 +27,8 @@ build:
-t $(NAME):$(VERSION) --rm .

run:
@if ! docker images $(NAME) | awk '{ print $$2 }' | grep -q -F $(VERSION); then echo "$(NAME) version $(VERSION) is not yet built. Please run 'make build'"; false; fi

rm -rf /tmp/mysql
mkdir -p /tmp/mysql/etc
mkdir -p /tmp/mysql/lib
Expand Down Expand Up @@ -44,30 +57,33 @@ run:

sleep 4

tests:
test:
sleep 10
./bats/bin/bats test/tests.bats

stop:
docker exec mysql /bin/bash -c "sv stop mysql" || true
docker exec mysql /bin/bash -c "sv stop mysql" 2> /dev/null || true
sleep 2
docker exec mysql /bin/bash -c "rm -rf /etc/mysql/conf.d/*" || true
docker exec mysql /bin/bash -c "rm -rf /var/lib/mysql//*" || true
docker stop mysql mysql_no_mysql mysql_default || true
docker exec mysql /bin/bash -c "rm -rf /etc/mysql/conf.d/*" 2> /dev/null || true
docker exec mysql /bin/bash -c "rm -rf /var/lib/mysql//*" 2> /dev/null || true
docker stop mysql mysql_no_mysql mysql_default 2> /dev/null || true

clean: stop
docker rm mysql mysql_no_mysql mysql_default || true
docker rm mysql mysql_no_mysql mysql_default 2> /dev/null || true
rm -rf /tmp/mysql || true
docker images | grep "^<none>" | awk '{print$3 }' | xargs docker rmi || true
docker images | grep "<none>" | awk '{print$3 }' | xargs docker rmi 2> /dev/null || true

publish: docker_login run test clean
docker push $(NAME)

tag_latest:
docker tag $(NAME):$(VERSION) $(NAME):latest

release: run tests clean tag_latest
@if ! docker images $(NAME) | awk '{ print $$2 }' | grep -q -F $(VERSION); then echo "$(NAME) version $(VERSION) is not yet built. Please run 'make build'"; false; fi
release: docker_login run test clean tag_latest
docker push $(NAME)
@echo "*** Don't forget to create a tag. git tag $(VERSION) && git push origin $(VERSION) ***"
curl -s -X POST https://hooks.microbadger.com/images/$(NAME)/fvNsVmJPHGNMhZSH-XYz2Klt1gE=

clean_images:
docker rmi $(NAME):latest $(NAME):$(VERSION) || true
clean_images: clean
docker rmi $(NAME):latest $(NAME):$(VERSION) 2> /dev/null || true
docker logout


67 changes: 25 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Docker container for MySQL Server based on [madharjan/docker-base](https://githu

## MySQL Server 5.7 (docker-mysql)

### Environment

| Variable | Default | Example |
|-----------------|--------------|----------------|
| DISABLE_MYSQL | 0 | 1 (to disable) |
Expand All @@ -21,57 +23,37 @@ Docker container for MySQL Server based on [madharjan/docker-base](https://githu

## Build

### Clone this project

```bash
# clone project
git clone https://github.com/madharjan/docker-mysql
cd docker-mysql
```

### Build Containers

```bash
# login to DockerHub
docker login

# build
make

# tests
make run
make tests
make clean
make test

# tag
make tag_latest

# release
make release
```

### Tag and Commit to Git

```bash
git tag 5.7
git push origin 5.7
# clean
make clean
```

## Run Container
## Run

### Prepare folder on host for container volumes
**Note**: update environment variables below as necessary

```bash
# prepare foldor on host for container volumes
sudo mkdir -p /opt/docker/mysql/etc/conf.d
sudo mkdir -p /opt/docker/mysql/lib/
sudo mkdir -p /opt/docker/mysql/log/
```

### Run `docker-mysql`

```bash
# stop & remove previous instances
docker stop mysql
docker rm mysql

# run container
docker run -d \
-e MYSQL_DATABASE=mydb \
-e MYSQL_USERNAME=user \
Expand All @@ -84,9 +66,9 @@ docker run -d \
madharjan/docker-mysql:5.5
```

## Run via Systemd
## Systemd Unit File

### Systemd Unit File - basic example
**Note**: update environment variables below as necessary

```txt
[Unit]
Expand All @@ -102,46 +84,47 @@ ExecStartPre=-/bin/mkdir -p /opt/docker/mysql/lib
ExecStartPre=-/bin/mkdir -p /opt/docker/mysql/log
ExecStartPre=-/usr/bin/docker stop mysql
ExecStartPre=-/usr/bin/docker rm mysql
ExecStartPre=-/usr/bin/docker pull madharjan/docker-mysql:5.5
ExecStartPre=-/usr/bin/docker pull madharjan/docker-mysql:5.7
ExecStart=/usr/bin/docker run \
-e MYSQL_DATABASE=mydb \
-e MYSQL_USERNAME=user \
-e MYSQL_PASSWORD=pass \
-p 3306:3306 \
-v /opt/docker/mysql/etc/conf.d:/etc/mysql/conf.d \
-v /opt/docker/mysql/lib/:/var/lib \
-v /opt/docker/mysql/lib/:/var/lib/mysql \
-v /opt/docker/mysql/log:/var/log/mysql \
--name mysql \
madharjan/docker-mysql:5.5
madharjan/docker-mysql:5.7
ExecStop=/usr/bin/docker stop -t 2 mysql
[Install]
WantedBy=multi-user.target
```

### Generate Systemd Unit File
## Generate Systemd Unit File

| Variable | Default | Example |
|---------------------|------------------|------------------------------------------------------------------|
| PORT | 3306 | 8080 |
| VOLUME_HOME | /opt/docker | /opt/data |
| VERSION | 1.10.3 | latest |
| MYSQL_DATABASE | temp | mydb |
| MYSQL_USERNAME | mysql | user |
| MYSQL_PASSWORD | mysql | pass |
| NAME | mysql | docker-mysql |
| MYSQL_DATABASE | temp | mydb |
| MYSQL_USERNAME | mysql | user |
| MYSQL_PASSWORD | mysql | pass |

```bash
docker run --rm -it \
# generate postgresql.service
docker run --rm \
-e PORT=3306 \
-e VOLUME_HOME=/opt/docker \
-e VERSION=5.7 \
-e NAME=docker-mysql \
-e MYSQL_DATABASE=mydb \
-e MYSQL_USERNAME=user \
-e MYSQL_PASSWORD=pass \
madharjan/docker-mysql:5.7 \
/bin/sh -c "mysql-systemd-unit" | \
mysql-systemd-unit | \
sudo tee /etc/systemd/system/mysql.service

sudo systemctl enable mysql
Expand Down
40 changes: 24 additions & 16 deletions bin/mysql-systemd-unit
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@ if [ "${DEBUG}" = true ]; then
set -x
fi

VERSION=5.7

DEF_PORT=80
DEF_VOLUME_HOME=${VOLUME_HOME}
DEF_VERSION=1.10.3
DEF_NAME=mysql

DEF_MYSQL_DATABASE=temp
DEF_MYSQL_USERNAME=mysql
DEF_MYSQL_PASSWORD=mysql

PORT=${PORT:-$DEF_PORT}
VOLUME_HOME=${VOLUME_HOME:-$DEF_VOLUME_HOME}
VERSION=${VERSION:-$DEF_VERSION}
NAME=${NAME:-$DEF_NAME}

MYSQL_DATABASE=${MYSQL_DATABASE:-$DEF_MYSQL_DATABASE}
MYSQL_USERNAME=${MYSQL_USERNAME:-$DEF_MYSQL_USERNAME}
MYSQL_PASSWORD=${MYSQL_PASSWORD:-$DEF_MYSQL_PASSWORD}

if [ -z ${PORT} ]; then
PORT_LINE=""
else
PORT_LINE="-p ${PORT}:3306 "
fi

/bin/cat <<-EOF
[Unit]
Description=MySQL Server
Expand All @@ -30,25 +39,24 @@ After=docker.service
[Service]
TimeoutStartSec=0
ExecStartPre=-/bin/mkdir -p ${VOLUME_HOME}/mysql/etc/conf.d
ExecStartPre=-/bin/mkdir -p ${VOLUME_HOME}/mysql/lib
ExecStartPre=-/bin/mkdir -p ${VOLUME_HOME}/mysql/log
ExecStartPre=-/usr/bin/docker stop mysql
ExecStartPre=-/usr/bin/docker rm mysql
ExecStartPre=-/usr/bin/docker pull madharjan/docker-mysql:5.7
ExecStartPre=-/bin/mkdir -p ${VOLUME_HOME}/${NAME}/etc/conf.d
ExecStartPre=-/bin/mkdir -p ${VOLUME_HOME}/${NAME}/lib
ExecStartPre=-/bin/mkdir -p ${VOLUME_HOME}/${NAME}/log
ExecStartPre=-/usr/bin/docker stop ${NAME}
ExecStartPre=-/usr/bin/docker rm ${NAME}
ExecStartPre=-/usr/bin/docker pull madharjan/docker-mysql:${VERSION}
ExecStart=/usr/bin/docker run \\
-e MYSQL_DATABASE=${MYSQL_DATABASE} \\
-e MYSQL_USERNAME=${MYSQL_USERNAME} \\
-e MYSQL_PASSWORD=${MYSQL_PASSWORD} \\
-p ${PORT}:3306 \\
-v ${VOLUME_HOME}/mysql/etc/conf.d:/etc/mysql/conf.d \\
-v ${VOLUME_HOME}/mysql/lib/:/var/lib \\
-v ${VOLUME_HOME}/mysql/log:/var/log/mysql \\
--name mysql \\
madharjan/docker-mysql:5.7
ExecStop=/usr/bin/docker stop -t 2 mysql
${PORT_LINE}-v ${VOLUME_HOME}/${NAME}/etc/conf.d:/etc/mysql/conf.d \\
-v ${VOLUME_HOME}/${NAME}/lib/:/var/lib/mysql \\
-v ${VOLUME_HOME}/${NAME}/log:/var/log/mysql \\
--name ${NAME} \\
madharjan/docker-mysql:${VERSION}
ExecStop=/usr/bin/docker stop -t 2 ${NAME}
[Install]
WantedBy=multi-user.target
Expand Down
5 changes: 3 additions & 2 deletions services/20-mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ if [ "${DEBUG}" = true ]; then
fi

MYSQL_DATABASE=${MYSQL_DATABASE:-temp}
MYSQL_USERNAME=${MYSQL_DB_USERNAME:-mysql}
MYSQL_PASSWORD=${MYSQL_DB_PASSWORD:-mysql}
MYSQL_USERNAME=${MYSQL_USERNAME:-mysql}
MYSQL_PASSWORD=${MYSQL_PASSWORD:-mysql}

MYSQL_CHARSET=${MYSQL_CHARSET:-utf8}
MYSQL_COLLATION=${MYSQL_COLLATION:-utf8_unicode_ci}
Expand Down Expand Up @@ -38,6 +38,7 @@ else
rm -f /etc/service/mysql/down
fi

sed -i -e '/bind-address/d' /etc/mysql/mysql.conf.d/mysqld.cnf
if [ -f "/etc/mysql/conf.d/mysqld-skip-name-resolv.cnf" ]; then
echo "mysqld-skip-name-resolv.cnf already exists"
else
Expand Down
2 changes: 1 addition & 1 deletion test/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}

@test "checking sql: mysql" {
skip "sample code"

run docker exec mysql /bin/bash -c "mysql -N -s -e 'SHOW DATABASES;' | grep mydb"
[ "$status" -eq 0 ]
[ "$output" = "mydb" ]
Expand Down

0 comments on commit 53fc8f4

Please sign in to comment.