Skip to content

Commit 3382911

Browse files
committed
update image name
1 parent 7aef2d7 commit 3382911

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

Docker-Compose.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
services:
22
docker-hosts:
3-
image: djsisson/docker-hosts:latest
3+
image: ghcr.io/djsisson/docker-hosts:latest
44
restart: unless-stopped
55
volumes:
66
- /var/run/docker.sock:/var/run/docker.sock:ro
7-
- /etc/hosts:/tmp/hosts
7+
- /etc/hosts:/tmp/hosts:rw

main.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bufio"
55
"context"
6+
"io"
67
"os"
78
"strings"
89
"sync"
@@ -117,7 +118,9 @@ func updateHostsFile() {
117118
lines := []string{}
118119
BeginText := "### BEGIN DOCKER CONTAINERS ###"
119120
EndText := "### END DOCKER CONTAINERS ###"
120-
file, err := os.Open("/tmp/hosts")
121+
dstPath := "/tmp/hosts"
122+
tmpPath := "/tmp/hosts.tmp"
123+
file, err := os.Open(dstPath)
121124
if err != nil {
122125
println("Error opening hosts file")
123126
return
@@ -147,18 +150,26 @@ func updateHostsFile() {
147150
}
148151
lines = append(lines, EndText)
149152
file.Close()
150-
tmpfile, err := os.Create("/tmp/hosts.tmp")
153+
tmpfile, err := os.Create(tmpPath)
154+
151155
if err != nil {
152156
println("Error creating temp hosts file")
153157
return
154158
}
159+
155160
for _, line := range lines {
156161
tmpfile.WriteString(line + "\n")
157162
}
158-
tmpfile.Close()
159-
err = os.Rename("/tmp/hosts.tmp", "/tmp/hosts")
163+
tmpfile.Seek(0, 0)
164+
defer tmpfile.Close()
165+
dst, err := os.Create(dstPath)
160166
if err != nil {
161-
println("Error renaming hosts file ", err)
167+
println("Error creating hosts file")
168+
return
169+
}
170+
defer dst.Close()
171+
if _, err = io.Copy(dst, tmpfile); err != nil {
172+
println("Error updating hosts file")
162173
return
163174
}
164175
}

0 commit comments

Comments
 (0)