Skip to content

Commit 29e136f

Browse files
committed
chore: complete initial version
1 parent 803ae07 commit 29e136f

File tree

12 files changed

+232
-59
lines changed

12 files changed

+232
-59
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
.gitignore
1212
*docker-compose*
1313
Makefile
14+
*.md

.drone.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pipeline:
2+
publish:
3+
image: plugins/docker
4+
group: production
5+
repo: joseluisq/alpine-mysql-client
6+
dockerfile: ./docker/Dockerfile
7+
username:
8+
from_secret: dockerhub_username
9+
password:
10+
from_secret: dockerhub_password
11+
auto_tag: true
12+
when:
13+
event: tag
14+
15+
notification:
16+
image: plugins/slack
17+
webhook:
18+
from_secret: slack_webhook
19+
channel: ci-cd
20+
when:
21+
status: [success, failure]

Dockerfile

Lines changed: 0 additions & 31 deletions
This file was deleted.

LICENSE.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Jose Quintana <https://git.io/joseluisq>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
run:
2-
@bash -c "./test.sh"
3-
.PHONY: run
4-
51
build:
6-
@docker build -t alpine-mysql-client:latest .
2+
@docker build -t alpine-mysql-client:latest -f docker/Dockerfile .
73
.PHONY: build
84

95
exec:
106
@docker run --rm -it \
7+
--user mysql \
118
--name alpine-mysql-client \
12-
--volume $(PWD):/root/sample \
13-
--workdir /root/sample \
9+
--volume $(PWD):/home/mysql/sample \
10+
--workdir /home/mysql/sample \
1411
alpine-mysql-client:latest \
1512
mysql_exporter .env
1613
.PHONY: exec
14+
15+
release:
16+
# 1. Get latest git tag
17+
$(eval GIT_TAG := $(shell git tag -l --contains HEAD))
18+
19+
# 2. Update docker files to latest tag
20+
./docker/version.sh $(GIT_TAG)
21+
22+
# 3. Commit and push to latest tag
23+
git add docker/Dockerfile
24+
git commit docker/Dockerfile -m "$(GIT_TAG)"
25+
git push origin master
26+
git push origin $(GIT_TAG)
27+
.ONESHELL: release

README.md

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Alpine / MySQL Client
1+
# Alpine / MySQL Client
22

33
> [MySQL client](https://dev.mysql.com/doc/refman/8.0/en/programs-client.html) tools on top of [Alpine Linux x86_64](https://hub.docker.com/_/alpine).
44
@@ -21,24 +21,28 @@ mysqldumpslow
2121
mysqlshow
2222
```
2323

24-
**Note:** For more details take a look at:
24+
For more details check it out:
2525

2626
- [MariaDB 10 - Clients & Utilities](https://mariadb.com/kb/en/clients-utilities/)
2727
- [MySQL 8 - Client Programs](https://dev.mysql.com/doc/refman/8.0/en/programs-client.html)
2828

29+
## User privileges
30+
31+
- Default user (unprivileged) is `mysql`.
32+
- `mysql` home directory is located at `/home/mysql`.
33+
- If you want a full privileged user try `root`. E.g append a `--user root` argument to `docker run`.
34+
2935
## Additional Tools
3036

31-
This image comes with some additional tools:
37+
This image comes with some additional tools.
3238

3339
### Exporter
3440

3541
`mysql_exporter` is a custom tool which exports a database script using `mysqldump`. Additionally it support gzip compression.
36-
It can be configured via a `.env` file.
42+
It can be configured via environment variables or using `.env` file.
3743

3844
#### Setup via environment variables
3945

40-
Setup can be done using a `.env` file.
41-
4246
```env
4347
DB_PROTOCOL=TCP
4448
DB_HOST=127.0.0.1
@@ -48,23 +52,69 @@ DB_EXPORT_FILE_PATH=database.sql.gz
4852
DB_EXPORT_GZIP=true
4953
DB_NAME=db
5054
DB_USERNAME=root
51-
DB_PASSWORD=""
55+
DB_PASSWORD="xyz"
5256
DB_ARGS=
5357
```
5458

55-
**Notes: **
59+
**Notes:**
5660

57-
- `DB_EXPORT_GZIP=true`: Compress the sql file using Gzip (optional)
61+
- `DB_EXPORT_GZIP=true`: Compress the sql file using Gzip (optional). If `false` or not defined then the exported file will be a `.sql` file.
62+
- `DB_ARGS`: can be used in order to pass more `mysqldump` arguments (optional).
63+
- An `.env` example file can be found at [./mysql_exporter.env](./mysql_exporter.env)
5864

59-
#### Export database using a Docker container
65+
#### Exporting a database using a Docker container
66+
67+
The following Docker commands create a container in order to export a database and then remove such container automatically.
68+
69+
**Note:** `mysql_exporter` supports enviroment variables or a `.env` file passed as argument.
6070

6171
```sh
6272
docker run --rm -it \
63-
--name joseluisq/alpine-mysql-client \
64-
--volume $(PWD):/root/sample \
65-
--workdir /root/sample \
66-
joseluisq/alpine-mysql-client \
67-
mysql_exporter .env
73+
--volume $(PWD):/home/mysql/sample \
74+
--workdir /home/mysql/sample \
75+
joseluisq/alpine-mysql-client:1 \
76+
mysql_exporter production.env
77+
78+
# Alpine / MySQL Client - Exporter
79+
# ================================
80+
# mysqldump Ver 10.17 Distrib 10.4.12-MariaDB, for Linux (x86_64)
81+
# Exporting database `mydb` to `mydb.sql.gz` file...
82+
# Database `mydb` was exported successfully!
83+
# 2.0M mydb.sql.gz
6884
```
6985

70-
__Note:__ `.env` is a custom file with the corresponding environment variables passed as argument.
86+
__Notes:__
87+
88+
- `--volume $(PWD):/home/mysql/sample` specificy a [bind mount directory](https://docs.docker.com/storage/bind-mounts/) from host to container.
89+
- `$(PWD)` is just my host working directory. Use your own path.
90+
- `/home/mysql/` is default home directory user (optional). View [User privileges](#user-privileges) section above.
91+
- `/home/mysql/sample` is a container directory that Docker will create for us.
92+
- `--workdir /home/mysql/sample` specificy the working directory used by default inside the container.
93+
- `production.env` is a custom env file path with the corresponding environment variables passed as argument. That file shoud available in your host working directory. E.g `$PWD` in my case.
94+
95+
#### Exporting a database using a Docker compose file
96+
97+
```yaml
98+
version: "3.3"
99+
100+
services:
101+
exporter:
102+
image: joseluisq/alpine-mysql-client:1
103+
env_file: .env
104+
command: mysql_exporter
105+
working_dir: /home/mysql/sample
106+
volumes:
107+
- ./:/home/mysql/sample
108+
networks:
109+
- default
110+
```
111+
112+
## Contributions
113+
114+
Feel free to send a [pull request](https://github.com/joseluisq/alpine-mysql-client/pulls) or file some [issue](https://github.com/joseluisq/alpine-mysql-client/issues).
115+
116+
## License
117+
118+
MIT license
119+
120+
© 2020 [Jose Quintana](https://git.io/joseluisq)

__mysqldump.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ MYEXPORT="\
3131
--password=\"$DB_PASSWORD\" \
3232
$DB_ARGS $DB_NAME $XDB_EXPORT"
3333

34-
echo "MySQL Tools - Exporter"
35-
echo "======================="
34+
echo "Alpine / MySQL Client - Exporter"
35+
echo "================================"
3636

3737
mysqldump --version
3838

3939
echo "Exporting database \`$DB_NAME\` to \`$XDB_EXPORT_FILE\` file..."
4040

4141
eval mysqldump $MYEXPORT
4242

43-
echo "Database \`$DB_NAME\` were exported successfully!"
43+
echo "Database \`$DB_NAME\` was exported successfully!"
4444
du -sh $XDB_EXPORT_FILE

docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: "3.3"
2+
3+
services:
4+
exporter:
5+
image: alpine-mysql-client:latest
6+
7+
# Uses on development
8+
env_file: .env
9+
10+
# Or uses on production
11+
# environment:
12+
13+
command: mysql_exporter
14+
working_dir: /home/mysql/sample
15+
volumes:
16+
- ./:/home/mysql/sample
17+
networks:
18+
- default

docker/tmpl.Dockerfile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
FROM alpine:$ALPINE_VERSION
2+
3+
LABEL maintainer="Jose Quintana <git.io/joseluisq>"
4+
5+
ARG USER_NAME
6+
ARG USER_HOME_DIR
7+
8+
ENV BUILD_DEPS="gettext" \
9+
RUNTIME_DEPS="libintl" \
10+
\
11+
USER_NAME=${USER_NAME:-mysql} \
12+
USER_HOME_DIR=${USER_HOME_DIR:-/home/${USER_NAME}}
13+
14+
RUN adduser -h ${USER_HOME_DIR} -s /sbin/nologin -u 1000 -D ${USER_NAME} && \
15+
\
16+
apk --no-cache add ca-certificates tzdata mysql-client nano dumb-init && \
17+
\
18+
set -ex; \
19+
apkArch="$(apk --print-arch)"; \
20+
case "$apkArch" in \
21+
armhf) arch='arm' ;; \
22+
aarch64) arch='arm64' ;; \
23+
x86_64) arch='amd64' ;; \
24+
*) echo >&2 "error: unsupported architecture: $apkArch"; exit 1 ;; \
25+
esac; \
26+
\
27+
apk add --update $RUNTIME_DEPS && \
28+
apk add --virtual build_deps $BUILD_DEPS && \
29+
\
30+
cp /usr/bin/envsubst /usr/local/bin/envsubst && \
31+
apk del build_deps
32+
33+
COPY ./__mysqldump.sh /usr/local/bin/__mysqldump.sh
34+
COPY ./mysql_exporter /usr/local/bin/mysql_exporter
35+
36+
RUN chmod +x /usr/local/bin/__mysqldump.sh && \
37+
chmod +x /usr/local/bin/mysql_exporter
38+
39+
USER ${USER_NAME}
40+
WORKDIR ${USER_HOME_DIR}
41+
VOLUME ${USER_HOME_DIR}
42+
EXPOSE 3306
43+
44+
ENTRYPOINT [ "/usr/bin/dumb-init" ]
45+
CMD [ "mysql" ]
46+
47+
# Metadata
48+
LABEL org.opencontainers.image.vendor="Jose Quintana" \
49+
org.opencontainers.image.url="https://github.com/joseluisq/alpine-mysql-client" \
50+
org.opencontainers.image.title="Alpine / MySQL Client" \
51+
org.opencontainers.image.description="MySQL client tools on top of Alpine Linux x86_64." \
52+
org.opencontainers.image.version="$VERSION" \
53+
org.opencontainers.image.documentation="https://github.com/joseluisq/alpine-mysql-client/blob/master/README.md"

docker/version.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
3+
set -e
4+
set -u
5+
6+
LATEST_TAG=$1
7+
BASE_PATH="$(pwd)/docker"
8+
9+
if [ $# -eq 0 ]; then
10+
echo "Usage: ./version.sh <tag or branch>"
11+
exit
12+
fi
13+
14+
export VERSION=$LATEST_TAG
15+
export ALPINE_VERSION=3.11
16+
17+
if [ ! -d "$BASE_PATH" ]; then
18+
echo "Directory no found for \"${BASE_PATH}\""
19+
exit 1
20+
fi
21+
22+
echo "Generating Dockerfile for Alpine Linux v$ALPINE_VERSION x86_64"
23+
24+
rm -rf "${BASE_PATH}/Dockerfile"
25+
26+
envsubst \$ALPINE_VERSION,\$VERSION <"${BASE_PATH}/tmpl.Dockerfile" >"${BASE_PATH}/Dockerfile"
27+
28+
echo "Dockerfile $VERSION were created successfully!"

0 commit comments

Comments
 (0)