Skip to content

Commit

Permalink
fix(cli): dockerfile app example uses provided port number
Browse files Browse the repository at this point in the history
Fixes a bug where the initialized docker file example used a hardcoded
port number instead of the port number provided in the wizard or by
command line arguments.
  • Loading branch information
jfeodor committed Sep 20, 2024
1 parent c2cbe26 commit 57f910b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/manifest/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (m *Manifest) BootstrapFiles(toolID string, basePath string) error {
return err
}
case m.Docker != nil:
err := m.Docker.bootstrapFiles(basePath)
err := m.Docker.bootstrapFiles(basePath, m.Port)
if errors.Is(err, ErrNoBootstrapDockerfileExists) {
output.Notify(
"Skipping Dockerfile bootstrapping",
Expand Down
9 changes: 5 additions & 4 deletions internal/manifest/bootstrap_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package manifest

import (
"errors"
"fmt"
"path/filepath"
)

Expand All @@ -10,14 +11,14 @@ const DockerfileLibraryKey = "dockerfile"
var ErrNoBootstrapDockerfileExists = errors.New("cannot bootstrap with pre-existing dockerfile")

const dockerExampleDockerfile = `FROM python:3.11-slim
EXPOSE 8501
EXPOSE %d
COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
COPY app.py /app/app.py
CMD ["streamlit", "run", "/app/app.py", "--server.port", "8501"]`
CMD ["streamlit", "run", "/app/app.py", "--server.port", "%d"]`

const dockerExampleAppPy = `import streamlit as st
Expand All @@ -37,7 +38,7 @@ st.markdown(

const dockerExampleRequirementsTxt = "streamlit\n"

func (d Docker) bootstrapFiles(basePath string) error {
func (d Docker) bootstrapFiles(basePath string, port uint) error {
dockerfilePath := filepath.Join(basePath, d.Dockerfile)
appPath := filepath.Join(basePath, "app.py")
requirementsPath := filepath.Join(basePath, "requirements.txt")
Expand All @@ -48,7 +49,7 @@ func (d Docker) bootstrapFiles(basePath string) error {
return ErrNoBootstrapDockerfileExists
}

if err := createAndWriteIfFileNotExist(dockerfilePath, dockerExampleDockerfile); err != nil {
if err := createAndWriteIfFileNotExist(dockerfilePath, fmt.Sprintf(dockerExampleDockerfile, port, port)); err != nil {
return err
}

Expand Down
5 changes: 4 additions & 1 deletion internal/manifest/bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package manifest

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -318,9 +319,11 @@ func TestBootstrapFilesPythonApp(t *testing.T) {
func TestBootstrapDockerApp(t *testing.T) {
t.Run("given no pre-existing dockerfile it bootstraps example docker app", func(t *testing.T) {
appDir := t.TempDir()
port := uint(1234)
m := Manifest{
App: App{
CoverImage: "cover_image.png",
Port: port,
},
Docker: &Docker{
Dockerfile: "Dockerfile",
Expand All @@ -331,7 +334,7 @@ func TestBootstrapDockerApp(t *testing.T) {
err := m.BootstrapFiles("", appDir)

assert.NoError(t, err)
test.AssertFileContent(t, filepath.Join(appDir, "Dockerfile"), []byte(dockerExampleDockerfile))
test.AssertFileContent(t, filepath.Join(appDir, "Dockerfile"), []byte(fmt.Sprintf(dockerExampleDockerfile, port, port)))
test.AssertFileContent(t, filepath.Join(appDir, "app.py"), []byte(dockerExampleAppPy))
test.AssertFileContent(t, filepath.Join(appDir, "requirements.txt"), []byte(dockerExampleRequirementsTxt))
})
Expand Down

0 comments on commit 57f910b

Please sign in to comment.