3

I'm new to docker. I need to create a docker image with python, postgresql and redis. I know usually this is not a good practice, since most of the time I should have 3 images and put them together with docker compose. But my case is a little bit special, I need to run my TeamCity build step in a docker wrapper. Seems like you can only specify one docker image for a docker wrapper. So I end up creating this Dockerfile with everything I need.

FROM python:3.8

# install redis
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4EB27DB2A3B88B8B && \
    apt-get update && \
    apt-get install -y redis

# install postgresql
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list && \
    wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
    apt-get update && \
    apt-get install -y postgresql-12 postgresql-contrib-12

# set up postgresql
USER postgres
RUN /etc/init.d/postgresql start && \
    psql --command "CREATE USER test_user WITH PASSWORD '123456';" && \
    psql --command "CREATE DATABASE test_db WITH OWNER = test_user;"

# start postgresql and redis when container is started
ENTRYPOINT [ "/bin/sh", "-c", "/etc/init.d/postgresql restart && /etc/init.d/redis restart" ]

The image is built successfully. However, when I try to run the image, nothing happens. I think something is wrong with the ENTRYPOINT statement. I googled around and tried a few things, nothing works.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Fei Qu
  • 999
  • 2
  • 12
  • 26
  • 2
    Use `docker-compose` for multiple services – Arkadip Bhattacharya Jul 13 '22 at 05:30
  • https://docs.docker.com/config/containers/multi-service_container/ – ericfossas Jul 13 '22 at 05:43
  • Docker is really bad with error messages regarding entry point problems. I suggest you create a script inside the container, and your entrypoint will just call that script. There may be issues with the several commands you are trying to run. – Queeg Jul 13 '22 at 06:34
  • ok, for the long run. if you see this in the future use logs to check the issue i.e. docker logs imageName. this can solve your 80% of problems – bananas Jul 13 '22 at 07:51
  • @bananas thx for the tips about log, I took a look and it did help me solve some issues. – Fei Qu Jul 14 '22 at 04:46

1 Answers1

1

I assume, that the docker container simply exited and that is expected.

The container stays running as long the start up process stays running.

When you restart a service, the init process starts the services in background and the init process simply exits.

Sine the init process exits so does the container, try adding a process that stays running.

E.g

ENTRYPOINT /etc/init.d/postgresql restart && /etc/init.d/redis restart && tail -f /dev/null
Tolis Gerodimos
  • 3,782
  • 2
  • 7
  • 14