{"id":18469509,"url":"https://github.com/miteshsharma/dockermysqlgo","last_synced_at":"2025-04-08T10:32:47.070Z","repository":{"id":57602514,"uuid":"163140685","full_name":"MiteshSharma/DockerMysqlGo","owner":"MiteshSharma","description":"Running mysql docker container using go ","archived":false,"fork":false,"pushed_at":"2022-11-22T04:00:59.000Z","size":279,"stargazers_count":16,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-23T11:11:54.974Z","etag":null,"topics":["docker","docker-mysql","go","mysql"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MiteshSharma.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-26T05:40:58.000Z","updated_at":"2024-04-12T17:33:25.000Z","dependencies_parsed_at":"2023-01-23T16:00:21.319Z","dependency_job_id":null,"html_url":"https://github.com/MiteshSharma/DockerMysqlGo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MiteshSharma%2FDockerMysqlGo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MiteshSharma%2FDockerMysqlGo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MiteshSharma%2FDockerMysqlGo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MiteshSharma%2FDockerMysqlGo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MiteshSharma","download_url":"https://codeload.github.com/MiteshSharma/DockerMysqlGo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247824259,"owners_count":21002238,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","docker-mysql","go","mysql"],"created_at":"2024-11-06T10:10:39.975Z","updated_at":"2025-04-08T10:32:45.980Z","avatar_url":"https://github.com/MiteshSharma.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DockerMysqlGo\n\nRunning mysql docker container using go code\n\n### Steps to run mysql docker container using command line:\n\nStep 1: Check docker is installed by running command \"docker ps\". If docker is not installed, install docker from https://docs.docker.com/install/.\n\nStep 2: Start docker mysql container with user name \"gouser\", password \"gopassword\" and database name \"godb\" using command:\n\n```\ndocker run --name our-mysql-container -e MYSQL_ROOT_PASSWORD=root -e MYSQL_USER=gouser -e MYSQL_PASSWORD=gopassword -e MYSQL_DATABASE=godb -p 3306:3306 --tmpfs /var/lib/mysql mysql:5.7\n```\n\nStep 3: Check docker status using command with container name our-mysql-container: \n\n```\ndocker ps -a\n```\n\nStep 4: Once docker is up and running, connect using connection string:\n\n```\ngouser:gopassword@tcp(localhost:3306)/godb?charset=utf8\u0026parseTime=True\u0026loc=Local\n```\n\n### Docker using go\n\nWe are using same steps to run docker container using go.\n\ndocker.go : This file contains generic code to run any type of container image using docker.\n\nmysql.go : This file runs mysql container using docker.go\n\nredis.go : This file runs redis container using docker.go\n\n### environment variables\nThis library allows setting different environment variables required to run any image, this is passed as ContainerOptions Object\n\n### Exposing Internal Container ports\n\nThis library allows exposing multiple internal ports in the image, example you may have already MySQL running on the host machine on port 3306 and you want to spin a test MySQL on another port say 13306, in docker terminal this will look like\n```shell script\ndocker run -p 13306:3306\n```\nthis library allows easily exposing internal ports and mapping them to custom external ports via MappedPort Object\n\n### Run any Image\nThis library allows running any image, pass any number of environment variables, mount volume and expose multiple ports, with this you should be able to run test on any image\n\n### Example Redis Test Container\n\nCreate an afunction that extends `Container` Object then define the requirements for your image\n\n```shell script\ngo get -u github.com/mudphilo/go-docker@latest\n```\n\n```go\nimport (\n\t\"fmt\"\n\t\"github.com/go-redis/redis\"\n\tdocker \"github.com/mudphilo/go-docker\"\n\t\"github.com/stretchr/testify/assert\"\n\t\"testing\"\n)\n\nfunc StartRedisDocker() {\n\n\tport := 26973 // custom port\n\tpass := \"pass\" // custom password\n\timageName := \"bitnami/redis\"  // redis image name\n\n\tenvVar := map[string]string{\n\t\t\"REDIS_PASSWORD\": pass, // we set redis password via environment variables\n\t}\n\n    // lest do port mapping, this will enable us avoid port conflicts with host machine\n\tmappedPorts := docker.MappedPort{\n\t\tInternalPort: 6379,  // we want to expose default redis port to a custom port\n\t\tExposedPort:  port,\n\t}\n\n    // create your container options\n\tcontainerOption := docker.ContainerOption{\n\t\tName:              \"project-redis-1\",  // container name\n\t\tOptions:           envVar,\n\t\tMountVolumePath:   \"/var/lib/redis\", // mount volume\n\t\tContainerFileName: imageName,\n\t\tMappedPorts: []MappedPort{mappedPorts},\n\t}\n     \n    // \n\tm.Docker = docker.Docker{}\n\tm.Docker.Start(containerOption)\n\tm.Docker.WaitForStartOrKill(ContainerStartTimeout)\n    defer m.Docker.Stop()  // when done call this to destroy the container\n   \n    // go ahead with your testing\n  \n    uri := fmt.Sprintf(\"%s:%d\", \"127.0.0.1\", port)\n    \n    \tredisConfig := redis.Options{\n    \t\tMinIdleConns: 10,\n    \t\tIdleTimeout:  60 * time.Second,\n    \t\tPoolSize:     1000,\n    \t\tAddr:         uri,\n    \t}\n    \n    \tredisConfig.Password = pass\n    \n    \tclient := redis.NewClient(\u0026redisConfig)\n    \n    \ttestKey := \"test_key_name\"\n    \ttestData := \"test data here\"\n    \n    \t_, err := client.Set(testKey,testData,time.Minute * 5).Result()\n    \tassert.NoError(t,err)\n    \n    \tres, err := client.Get(testKey).Result()\n    \tassert.NoError(t,err)\n    \n    \tassert.Equal(t,testData,res)\n}\n```\n \n Hurrah you have done your unit tests","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiteshsharma%2Fdockermysqlgo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiteshsharma%2Fdockermysqlgo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiteshsharma%2Fdockermysqlgo/lists"}