0

I'm trying to create a dockerfile to build a LAMP server for development using the alpine-lamp base.

FROM glats/alpine-lamp
ENV MYSQL_ROOT_PASSWORD=password
RUN apk add wget && \
    apk add php7-simplexml && \
    mysql -u root -ppassword -e "CREATE DATABASE IF NOT EXISTS mydb"

When I run the docker image build -t testing . command to build my image I get an error:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)

Is my mysql command being run before the "parent" dockerfile has completed running? I've been able to remove these lines and start the container, and then run the identical mysql command from the cli, so I'm thinking the mysql server part of the parent lamp stack just didn't get completely run. Is there a way to ensure my commands run after the parent?

user101289
  • 9,888
  • 15
  • 81
  • 148

1 Answers1

0

At image build time, mysql will not be running. That is why you see the error message about not being able to connect.

This type of thing isn't something that should be run at build-time. Put the database initialization into an entrypoint script instead.

wmorrell
  • 4,988
  • 4
  • 27
  • 37
  • thanks for that. I guess the question then becomes "what if the parent container has an entrypoint script already"? Is there some way for the child container's entrypoint script to run in addition to the parent's? – user101289 Mar 21 '20 at 13:53
  • There can be only one entrypoint defined; but that does not mean the replacement entrypoint cannot call the original entrypoint. Add your own, then have it do `exec /path/to/original/entrypoint.sh`. – wmorrell Mar 21 '20 at 20:48
  • thanks wmorell. However, I need to run the other command first, and it doesn't exit. I added a new question here: https://stackoverflow.com/questions/60789358/dockerfile-execute-additional-scripts-in-entrypoint – user101289 Mar 21 '20 at 21:18