Tag Archives: docker
Postgres in docker: random «unique violation» errors
While running postgres in docker (for development, of course), from time to time I just run into errors like «Duplicate Key Value Violates Unique Constraint». I found the solution (thanks to this answer, for sure — https://stackoverflow.com/a/47089825/801426): if you have a table called «[table]», just do this
1 |
SELECT setval('[table]_id_seq', (SELECT MAX(id) FROM [table])); |
Gitlab-ci: build go app with docker as a docker image
Preparations [TO BE UPDATED LATER] My gitlab-ci.yml
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
variables: PACKAGE_PATH: /go/src/gitlab.com/[username_for_gitlab]/[project_name] # https://docs.gitlab.com/ee/ci/docker/using_docker_build.html CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest stages: - dep - test - build # A hack to make Golang-in-Gitlab happy .anchors: - &inject-gopath mkdir -p $(dirname ${PACKAGE_PATH}) && ln -s ${CI_PROJECT_DIR} ${PACKAGE_PATH} && cd ${PACKAGE_PATH} dep: stage: dep image: golang:1.13.1-alpine before_script: - *inject-gopath script: go mod tidy && go mod vendor artifacts: name: "vendor-$CI_PIPELINE_ID" paths: - vendor/ expire_in: 1 hour test: stage: test dependencies: - dep image: docker:17 services: - docker:dind before_script: - *inject-gopath script: docker build --target=test . lint: stage: test dependencies: - dep image: docker:17 services: - docker:dind before_script: - *inject-gopath script: docker build --target=lint . build: stage: build dependencies: - dep - lint - test image: docker:17 services: - docker:dind before_script: - *inject-gopath - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY registry script: - docker build --target=release --pull -t $CONTAINER_TEST_IMAGE . - docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE - docker push $CONTAINER_TEST_IMAGE |
My Dockerfile
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 |
FROM golang:1.13.1-alpine AS build WORKDIR /go/src/gitlab.com/[username_for_gitlab]/[project_name] COPY . . RUN go get -u github.com/jessevdk/go-assets-builder RUN apk add --no-cache make RUN make build #some pareparations and after that: go build -i -o ./[path_to_bin] ./cmd/[...] FROM alpine:3.10 AS release COPY --from=build /go/src/gitlab.com/[username_for_gitlab]/[project_name]/[path_to_bin] /usr/local/bin/[appname] RUN apk add --no-cache make bash curl tzdata EXPOSE 3000 3001 ENTRYPOINT ["/usr/local/bin/[appname]"] FROM golang:1.13.1-alpine AS test ENV CGO_ENABLED=0 WORKDIR /go/src/gitlab.com/[username_for_gitlab]/[project_name] COPY . . RUN apk add --no-cache make bash RUN make test FROM golangci/golangci-lint:v1.20 AS lint WORKDIR /go/src/gitlab.com/[username_for_gitlab]/[project_name] COPY . . RUN make lint-full # to check things locally # docker build --target=check_container . # docker images # docker run -it <hash> /bin/bash FROM golang:1.13.1-alpine AS check_container WORKDIR /go/src/gitlab.com/[username_for_gitlab]/[project_name] COPY . . RUN apk add --no-cache make bash RUN /bin/bash FROM release |
Pulling from your private registry Create deploy token as described here. After that, you can do this:
1 2 3 |
docker login -u [deploy_username] -p [deploy_token] registry.gitlab.com docker pull registry.gitlab.com/[username_for_gitlab]/[project_name]:master docker run -d --name [container_name_you_like] --env-file [path-to-.env] --net host --rm registry.gitlab.com/[username_for_gitlab]/[project_name]:master |
Useful links: https://dev.to/hypnoglow/how-to-make-friends-with-golang-docker-and-gitlab-ci-4bil https://docs.gitlab.com/ee/ci/docker/using_docker_build.html https://medium.com/@tonistiigi/advanced-multi-stage-build-patterns-6f741b852fae https://docs.gitlab.com/ee/ci/yaml/ (full description of gitlab-ci.yml) https://about.gitlab.com/blog/2017/11/27/go-tools-and-gitlab-how-to-do-continuous-integration-like-a-boss/ https://gitlab.com/hypnoglow/example-go-docker-gitlab/blob/master/.gitlab-ci.yml Somewhat useful: https://blog.lwolf.org/post/how-to-build-tiny-golang-docker-images-with-gitlab-ci/ https://angristan.xyz/build-push-docker-images-gitlab-ci/ https://docs.gitlab.com/ee/user/packages/container_registry/#use-images-from-gitlab-container-registry https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-centos-7 (it says to do sudo usermod -aG docker …
docker-compose example for ceph, kafka, postgres cluster
That is how it can be done:
docker simple starter
Build and run (on linux do not forget sudo):
1 2 |
docker image build -t <image-name> . docker container run --env "DOCKER=true" --env "NODE_ENV=test" --env "NODE_PORT=8000" --mount "type=bind,source=/etc/nginx/sites-enabled,target=/sites-enabled" --env "PATH_CONF=/sites-enabled" -p 8000:8000 --rm --name <container-name> --network=host <image-name> |
Login into container
1 |
docker container exec -it <container-name> bash |
docker-compose for starting aerospike and memcache
Place this config wherever you like (in your project) — for instance, ~/myproj/docker/docker-compose.yml:
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 |
version: "2" services: #aerspike as: image: aerospike/aerospike-server volumes: - ./aerospike/etc:/opt/aerospike/etc ports: - 3000:3000 command: [asd, --config-file, /opt/aerospike/etc/docker.conf] memcached: image: memcached ports: - 11211:11211 volumes: #volume for etcd dicovery-data: driver: local #volume for database db-data: driver: local |
And this config for aerospike should be placed in a directory ~/myproj/docker/aerospike/etc/docker.conf After that you should run it like that:
1 2 |
cd ~/myproj/docker docker-compose up -d |
When you finished, just run:
1 2 |
cd ~/myproj/docker docker-compose down |
https://docs.docker.com/compose/reference/up/
Запустить memcached в докере
Под мак.
1 2 3 4 5 6 |
docker pull memcached:alpine docker run --name memcached -d --restart=always --publish 11211:11211 memcached:alpine telnet localhost 11211 ctrl+] docker ps docker stop <id> |
https://hub.docker.com/_/memcached/ https://github.com/sameersbn/docker-memcached Вдогонку — как смотреть статистику в мемкэше.
1 2 3 4 5 6 |
telnet localhost 11211 > stats > stats items > set key 0 900 4 data > get key |
https://blog.elijaa.org/2010/05/21/memcached-telnet-command-summary/ http://www.alphadevx.com/a/90-Accessing-Memcached-from-the-command-line
Docker. Полезные команды
Обновить версию образа докера
1 |
sudo docker pull [path-to-registry]:master |
Запустить shell-команду внутри докер-контейнера
1 |
sudo docker run -ti [path-to-registry]:master ls -la /views/desktop/pm-widgets |
Как собрать и запустить свой докер
1 2 3 4 5 6 7 |
cd ~/Documents/docker/ git clone git@github.com:bullgare/docker-golang.git cd ./docker-golang/ docker build -t "bullgare:golang" . docker run -d bullgare:golang docker ps docker exec -ti <container id> /bin/bash |
Скопировать файлы с контейнера:
1 |
docker cp <containerId>:/file/path/within/container /host/path/target |