mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-06-23 04:10:25 +00:00
NOISSUE - Flush Docker logs (#229)
* flush docker logs Signed-off-by: Sammy Oina <sammyoina@gmail.com> * show logs in realtime Signed-off-by: Sammy Oina <sammyoina@gmail.com> * add tty Signed-off-by: Sammy Oina <sammyoina@gmail.com> * remove duplicate Signed-off-by: Sammy Oina <sammyoina@gmail.com> * python3 Signed-off-by: Sammy Oina <sammyoina@gmail.com> * error check Signed-off-by: Sammy Oina <sammyoina@gmail.com> * remove capitalization Signed-off-by: SammyOina <sammyoina@gmail.com> --------- Signed-off-by: Sammy Oina <sammyoina@gmail.com> Signed-off-by: SammyOina <sammyoina@gmail.com>
This commit is contained in:
committed by
GitHub
parent
f848afeefd
commit
51b129c3a2
@@ -3,6 +3,7 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -89,7 +90,10 @@ func (d *docker) Run() error {
|
||||
|
||||
// Create and start the container.
|
||||
respContainer, err := cli.ContainerCreate(ctx, &container.Config{
|
||||
Image: dockerImageName,
|
||||
Image: dockerImageName,
|
||||
Tty: true,
|
||||
AttachStdout: true,
|
||||
AttachStderr: true,
|
||||
}, &container.HostConfig{
|
||||
Mounts: []mount.Mount{
|
||||
{
|
||||
@@ -112,6 +116,30 @@ func (d *docker) Run() error {
|
||||
return fmt.Errorf("could not start a Docker container: %v", err)
|
||||
}
|
||||
|
||||
stdout, err := cli.ContainerLogs(ctx, respContainer.ID, container.LogsOptions{ShowStdout: true, Follow: true})
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not read stdout from the container: %v", err)
|
||||
}
|
||||
defer stdout.Close()
|
||||
|
||||
go func() {
|
||||
if err := writeToOut(stdout, d.stdout); err != nil {
|
||||
d.logger.Warn(fmt.Sprintf("could not write to stdout: %v", err))
|
||||
}
|
||||
}()
|
||||
|
||||
stderr, err := cli.ContainerLogs(ctx, respContainer.ID, container.LogsOptions{ShowStderr: true, Follow: true})
|
||||
if err != nil {
|
||||
d.logger.Warn(fmt.Sprintf("could not read stderr from the container: %v", err))
|
||||
}
|
||||
defer stderr.Close()
|
||||
|
||||
go func() {
|
||||
if err := writeToOut(stderr, d.stderr); err != nil {
|
||||
d.logger.Warn(fmt.Sprintf("could not write to stderr: %v", err))
|
||||
}
|
||||
}()
|
||||
|
||||
statusCh, errCh := cli.ContainerWait(ctx, respContainer.ID, container.WaitConditionNotRunning)
|
||||
select {
|
||||
case err := <-errCh:
|
||||
@@ -121,28 +149,6 @@ func (d *docker) Run() error {
|
||||
case <-statusCh:
|
||||
}
|
||||
|
||||
stdout, err := cli.ContainerLogs(ctx, respContainer.ID, container.LogsOptions{ShowStdout: true})
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not read stdout from the container: %v", err)
|
||||
}
|
||||
defer stdout.Close()
|
||||
|
||||
err = writeToOut(stdout, d.stdout)
|
||||
if err != nil {
|
||||
d.logger.Warn(fmt.Sprintf("could not write to stdout: %v", err))
|
||||
}
|
||||
|
||||
stderr, err := cli.ContainerLogs(ctx, respContainer.ID, container.LogsOptions{ShowStderr: true})
|
||||
if err != nil {
|
||||
d.logger.Warn(fmt.Sprintf("could not read stderr from the container: %v", err))
|
||||
}
|
||||
defer stderr.Close()
|
||||
|
||||
err = writeToOut(stderr, d.stderr)
|
||||
if err != nil {
|
||||
d.logger.Warn(fmt.Sprintf("could not write to stderr: %v", err))
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err = cli.ContainerRemove(ctx, respContainer.ID, container.RemoveOptions{Force: true}); err != nil {
|
||||
d.logger.Warn(fmt.Sprintf("error could not remove container: %v", err))
|
||||
@@ -157,13 +163,14 @@ func (d *docker) Run() error {
|
||||
}
|
||||
|
||||
func writeToOut(readCloser io.ReadCloser, ioWriter io.Writer) error {
|
||||
content, err := io.ReadAll(readCloser)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not convert content from the container: %v", err)
|
||||
scanner := bufio.NewScanner(readCloser)
|
||||
for scanner.Scan() {
|
||||
if _, err := ioWriter.Write(scanner.Bytes()); err != nil {
|
||||
return fmt.Errorf("error writing to output: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := ioWriter.Write(content); err != nil {
|
||||
return fmt.Errorf("could not write to output: %v", err)
|
||||
if err := scanner.Err(); err != nil {
|
||||
return fmt.Errorf("error reading container logs error: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -12,4 +12,4 @@ COPY ./lin_reg.py /cocos/lin_reg.py
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
# command to be run when the docker container is started
|
||||
CMD ["python3", "/cocos/lin_reg.py"]
|
||||
CMD ["python3", "-u", "/cocos/lin_reg.py"]
|
||||
|
||||
Reference in New Issue
Block a user