Docker beginner gotchas

I started using Docker recently and there was one thing I didn’t understand – initially.

So, when running your Dockerfile if you don’t have a CMD command the container will continue to run. However, when you include CMD command the container will exit unless you run one of your programs in the foreground. So, you’ll have to do something like this:

CMD bash -c "apachectl -D FOREGROUND"

if you are running Apache, of course, that will be different if you are running different software.

Another thing is that the CMD is where you want to run you dependencies installation, like PHP’s composer intall and NOT in the RUN commands. Here is how my CMD looked like in the end:

CMD bash -c "composer install && chmod -R 777 /path/to/project/storage/ /path/to/project/bootstrap/ && php /path/to/project/artisan migrate --force && apachectl -D FOREGROUND"

Note that I am a complete noob when it comes to Docker so take this with a grain of salt. This is only my noob understanding.

Validating array input fields with Laravel

I was working on project which had to validate array input fields, which looked like so:

<input type="text" name="tags[0][id]" value="5" />
<input type="text" name="tags[1][id]" value="6" />

The normal way to validate this is by using a rule like this:

"tags.*.id" => ["required", "exists:tags:id"]

but in this case the required doesn’t work. So, you have to do this as well:

"tags" => ["required"]

I discovered this somewhere on the internet but I can’t remember where so credit goes to Unknown.