简体   繁体   中英

Can't Dockerize Elm

I've been having trouble dockerizing an Elm application. I have, as far as I can tell, created a complete and working Docker file ... but it doesn't work.

I'll explain.

So I have my scripts run in 3 files.

First is the start script dockerBuild.sh

docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker rmi $(docker images -q)

docker build -t elmapp .
docker run -p 8000:8000 elmapp

This just kills all previous docker image and builds and runs elmapp .

Next I have my Dockerfile that is being called:

FROM node:latest

RUN npm install -g yarn
RUN yarn global add elm

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY . /usr/src/app
RUN chmod 777 runElm.sh

EXPOSE 8000
CMD sh runElm.sh

This produces the desired output and runs runElm.sh .

There are two alternative ways that I have the runElm file set up - they both produce the same result.

  • Alternative 1

    elm-reactor --address=0.0.0.0 --port=8000

This simply calls elm-reactor on the following html file:

<!DOCTYPE HTML>
<html>
<head>
  <link rel="stylesheet" href="style.css">
  <script src="/_compile/src/Main.elm"></script>
  <style>
    body, html{
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
    }
  </style>
</head>
<body>
  <script>
    var app = Elm.Main.fullscreen()
  </script>
</body>
</html>
  • Alternative 2

Another possible way to compile an Elm app is to transpile the code to javascript and run the javascript in an html doc. Here the runElm file would be like this:

elm-make ./src/Main.elm --output=main.js
python -m SimpleHTTPServer 8000

And the html would look like this:

<!DOCTYPE HTML>
<html>

  <head>
    <meta charset="UTF-8">
    <title>Elm • Play</title>
    <link rel="stylesheet" href="style.css">
    <style>
      body, html{
        height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>

  <body>
    <div id="main"></div>
  </body>

  <script type="text/javascript" src="./src/main.js"></script>
  <script>
    var node = document.getElementById('main');
    var app = Elm.Main.embed(node);
  </script>

</html>

The problem:

The issue here is that when I navigate to localhost:8000 after my build queue is finished I get 404. However, if I run either of the two above alternatives not in a Dockerfile I get my program to compile just fine. Usually the problem is that ports are not properly exposed, but here I've exposed ports 8000 in my dockerfile, in my docker run, and it's the same port as is running on my SimpleHTTPServer and elm-reactor servers. I've looked at various other ways to get Docker to play nice with Elm and I've had difficulties.

Does anyone have any suggestions?

EDIT:

CLOSED - Not sure what I did, but after going through and making sure everything was formatted correctly it now works. Must have just been tired when I wrote the original code. Thanks everyone!

if this is an httpserver i guess it should be running on port 80 ,when you specified the port 8000 that means you are exposing the container port not the host machine port,you can do port mapping in either ways

  1. same way you did and like that the port 8000 on the container will be mapped to a random port on the host.
  2. you can specify this by adding this the cmd which you use to create the container -p 8000:80 , 8000 on host machine, 80 on container

NOTE

The EXPOSE instruction informs Docker that a certain port is to be exposed when a container is started:

EXPOSE port1 port2 …

Even after exposing ports, while starting a container, you still need to provide port mapping using the -p flag to "docker run" cmd . This instruction is useful when you want to map your container exposed port to a custom host machine port

The port command looks up the public-facing port that is bound to an exposed port in the container:

$ docker port CONTAINER PRIVATE_PORT

if you want to know which is bound to container port run the following

docker port CONTAINER

elm-reactor listens on localhost by default, not all interfaces. This may prevent you from reaching it from outside the container, try: elm-reactor -a 0.0.0.0

If you get 404, that means the web server is running but it can't find the file you are requesting. So make sure your paths are correct, and check the output from your web server ( docker logs YOURCONTAINERNAME )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM