4

Summary

docker run doesn't seem to build a container (but it also doesn't throw an error) despite docker build successfully building the container image.

Input and Output

1. Successful docker image creation..

$ docker build -t minitwitter:latest .
...
Successfully built da191988e0db
Successfully tagged minitwitter:latest
$ docker images
REPOSITORY    TAG         IMAGE ID        CREATED         SIZE
minitwitter   latest      da191988e0db    6 seconds ago   173MB
python        3.7-alpine  b11d2a09763f    9 days ago      98.8MB

2. ..and docker run completes without error..

$ docker run --name minitwitter -d -p 8000:5000 --rm minitwitter:latest
e8835f1b4c72c8e1a8736589c74d56ee2d12ec7bcfb4695531759fb1c2cf0e48

3. ..but docker container doesn't seem to exist.

$ docker ps
CONTAINER ID  IMAGE  COMMAND  CREATED  STATUS  PORTS  NAMES

And navigating to the port where the app should be exposed, http://localhost:8000, returns the connection error ERR_CONNECTION_REFUSED.

Docker file, boot.sh

The Dockerfile and boot.sh files are pretty simple I think:

Dockerfile

FROM python:3.7-alpine

RUN adduser -D minitwitter

WORKDIR /home/minitwitter

COPY requirements.txt requirements.txt
RUN python -m venv env
RUN env/bin/pip install -r requirements.txt
RUN env/bin/pip install gunicorn

COPY app app
COPY migrations migrations
COPY minitwitter.py config.py boot.sh ./
RUN chmod a+x boot.sh

ENV FLASK_APP minitwitter.py

RUN chown -R minitwitter:minitwitter ./
USER minitwitter

EXPOSE 5000
ENTRYPOINT ["./boot.sh"]

boot.sh

# BOOTS A DOCKER CONTAINER
#!/bin/sh
source env/bin/activate
flask db upgrade
exec gunicorn -b :5000 --access-logfile - --error-logfile - minitwitter:app
va01
  • 301
  • 2
  • 7
  • You may want to try running it `docker run --interactive --tty ....` rather than detached to more easily view the logs and any exceptions. Alternatively, `docker container ls --all` should show the exited container too. You can then `docker container logs [CONTAINER-ID]` to see what happened. – DazWilkin Oct 31 '19 at 01:14
  • Customarily I'd `source /env/bin/activate` after creating the virtualenv and *before* `pip --requirements` etc. Could you not fold most of the `boot.sh` into your `Dockerfile` and have the `ENTRYPOINT ["gunicorn","-b",":5000",....]` – DazWilkin Oct 31 '19 at 01:24
  • @DazWilkin `docker container ls --all` shows no containers. That suggests to me that a container simply isn't being created. Do you agree? What'd you recommend given this additional fact? – va01 Oct 31 '19 at 02:04
  • Correct. It appears the container is not being created (correctly). – DazWilkin Oct 31 '19 at 02:05
  • Hmmm.... although you have `docker run ... --rm ...` too which removes completed containers. Drop that too. – DazWilkin Oct 31 '19 at 02:06
  • Aha! Okay I removed the `--rm` arg and, perhaps as you expected, the container shows up. That is, it seems the container is created and lives for 2 seconds before exiting. `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7dd1d493a1e4 minitwitter:latest "./boot.sh" 41 seconds ago Exited (1) 39 seconds ago minitwitter` – va01 Oct 31 '19 at 02:15
  • This helped narrow the problem space -- thank you! Do you have a recommendation on how to further identify what's causing it to be so short-lived? – va01 Oct 31 '19 at 02:18
  • What do you get from `docker container logs 7dd`? (**NB** you can use any uniquely identifying subset of the container ID) – DazWilkin Oct 31 '19 at 02:48
  • My working theory is that, by activating the virtualenv *afterwards*, you're borking it. I think you should activate the virtualenv before the pip install etc. – DazWilkin Oct 31 '19 at 02:54
  • @DazWilkin Placing the shebang (`#!/bin/sh`) in the _first_ line of `boot.sh` solved it for me (I've documented this in the Answer below). – va01 Oct 31 '19 at 03:33
  • The log was: `standard_init_linux.go:211: exec user process caused "exec format error"`. I investigated that error and ultimately found the answer listed below. – va01 Oct 31 '19 at 03:35

1 Answers1

1

Place the 'shebang' -- #!/bin/sh -- on the first line of the boot.sh shell script.

How I found this answer: This blog post which refers to this Stackoverflow post.

The problem: the original script has a comment on the first line and the shebang on the second line.

Note: The title of the 'Question' is misleading: a docker container was built. The container, however, was short-lived and given I used the -rm option in the docker run command, the container was deleted after it terminated within 2 seconds; this is why it didn't appear in the docker images -a command.

va01
  • 301
  • 2
  • 7