How to use php artisan serve inside docker container? - laravel

I create a php-composer image using dockerfile:
FROM php:7
RUN apt-get update
RUN apt-get install curl
RUN curl -sS https://getcomposer.org/installer -o composer-setup.php
RUN php composer-setup.php --install-dir=/usr/local/bin --filename=composer
RUN apt-get install -y git
And I run following commands to create a container and start a laravel app.
docker run -p 127.0.0.1:3000:8000 --name MyTest -dt php-composer to create a container
docker cp laravelApp/ d4bbb5d36312:/usr/
docker exec -it MyTest bash
cd usr/laravelApp
php artisan serve
After thet, container's terminal will show the success info:
Laravel development server started: <http://127.0.0.1:8000>
But when I access 127.0.0.1:3000 at local browser, I get nothing.
So is it possible that simply run php artisan serve to start a laravel app inside docker container?
Or I must to using nginx or apache to run it?

This can be done so:
$ docker container run -it --rm -v /host/path/laravel:/app -p 3000:8000 php bash
$ cd /app
$ php artisan serve --host 0.0.0.0
By default containers start in bridge network, inside which the host available by the address 0.0.0.0.
When you start Docker, a default bridge network (also called bridge) is created automatically, and newly-started containers connect to it unless otherwise specified.
https://docs.docker.com/network/bridge
Or so (only Linux):
$ docker container run -it --rm --network host -v /host/path/laravel:/app php bash
$ cd /app
$ php artisan serve (or php artisan serve --port 3000)
If you use the host network driver for a container, that container’s network stack is not isolated from the Docker host.
https://docs.docker.com/network/host

You can debug the issue with two commands:
Run this on the host machine to check if the port mapping is correct:
docker port MyTest
Run this on the host machine to check the output from inside your container:
docker exec MyTest curl 127.0.0.0:8000
You should see the raw HTTP response of your Laravel application.

Related

How to run a script in docker-compose start process without overriding ENTRYPOINT or CMD of Docker Image?

I want to start my docker container with a docker-compose command.
The underlying docker image CMD should just be executed regularly, and I want to append my script at some point.
When reading about executing shell commands in docker, entrypoint is brought up.
But according to the documentation start of the image + appended script without overriding entrypoint or cmd is not possible through entrypoint (https://docs.docker.com/compose/compose-file/#entrypoint):
Compose implementations MUST clear out any default command on the Docker image - both ENTRYPOINT and CMD instruction in the Dockerfile - when entrypoint is configured by a Compose file.
A similar question was asked here, but the answer did not address the issue:
docker-compose, how to run bash commands after container has started, without overriding the CMD or ENTRYPOINT in the image docker is pulling in?
Another option would be to copy & edit the dockerfile of the pulled image, but that would not be great for future imports:
docker-compose: run a command without overriding anything
What I actually want to do, is coupling the install of php & composer to the docker-compose up process.
Here is my docker-compose file:
version: '3.8'
services:
jenkins:
image: jenkins/jenkins:2.361.1
restart: always
privileged: true
user: root
ports:
- "8080:8080"
- "50000:50000"
container_name: "aaa-jenkins"
volumes:
- "./jenkins_configuration:/var/jenkins_home"
- "/var/run/docker.sock:/var/run/docker.sock"
- "/usr/bin/docker:/usr/bin/docker"
My script looks something like this:
#!/bin/bash
apt update
apt -y install php
apt -y install php-xml php-zip php-curl php-mbstring php-xdebug
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer

How to run a cloned Laravel project with sail on Windows?

I cloned an existing Laravel project from git which was dockerized with Sail. As vendor is in the .gitignore, I need to rebuild it before I can use Sail to run my app. Acording to the Laravel doc (https://laravel.com/docs/8.x/sail#installing-composer-dependencies-for-existing-projects) i need to get my dependencies using this command.
docker run --rm \
-u "$(id -u):$(id -g)" \
-v $(pwd):/var/www/html \
-w /var/www/html \
laravelsail/php81-composer:latest \
composer install --ignore-platform-reqs
Problem is both cmd and powershell seem to struggle with the $'s, it seams that they expect an applet name, and I can't manage to run this. What am I missing ?
The error I am getting with PS is
id : The term "id" is not recognized as the name of a cmdlet, function, script file or operable program.
In cmd, i got
docker: Error response from daemon: create $(pwd): "$(pwd)" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed.
I also tried with git bash and got
docker: invalid reference format: repository name must be lowercase.
Only execute the command in the WSL2 Distribution (as example, Ubuntu)
First, Open the wsl console from PowerShell pointing the project folder
wsl -d ubuntu
And then execute the docker command for execute Laravel sail
docker run --rm \
-u "$(id -u):$(id -g)" \
-v $(pwd):/var/www/html \
-w /var/www/html \
laravelsail/php81-composer:latest \
composer install --ignore-platform-reqs
I recommend check your php version for available laravel sail compatibility
When using the laravelsail/phpXX-composer image, you should use the same version of PHP that you plan to use for your application (74, 80, or 81).
Font: Laravel Sail

Running "docker-compose run --rm composer update" not working in Jenkins pipeline

I have a Jenkins pipeline just for learning purposes, which should build a Laravel app via docker-compose. The "docker-compose --build" step is working fine, but next it is running "docker-compose run --rm composer update", which then stops, no error or output.
When I run the command manually after accessing the server via SSH, the command runs with no issues.
Composer service in docker-compose file:
composer:
build:
context: .
dockerfile: composer.dockerfile
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
user: laravel
entrypoint: ['composer', '--ignore-platform-reqs']
networks:
- laravel
Build step in jenkinsfile:
stage('Build') {
steps {
echo 'Building..'
sh 'chmod +x scripts/jenkins-build.sh'
sh './scripts/jenkins-build.sh'
}
}
Command in shell script:
print "Building docker app"
sudo docker-compose up -d --build site # works fine
sudo chown jenkins -R ./
print "Running composer"
sudo docker-compose run --rm composer update # hangs in jenkins but works in cmd?
View in Jenkins:
Same command working on same server, via cmd:
I know there are some bad practices in here, but this is just for learning purposes. Jenkins server is running Ubuntu 20.04 on AWS EC2 instance.
In the end I resorted to installing composer directly into my PHP docker image. Therefore instead of running the composer service, I now use docker exec php composer update.
From what I can see, any services that were used via docker-compose run did not work in the Jenkins pipeline. In my case, these were all services that were only running whilst performing some action (like composer update), so maybe that is why Jenkins did not like it.

Unable to deploy the Docker Image of Laravel application on Heroku

I started to learn the Docker. I am a complete beginner to Docker. Now, what I am doing is trying to deploy a Docker image of Laravel application onto the Heroku. I have installed a Laravel project. My Laravel project has only one page, a welcome page showing a message. That's it. I am just trying to test Docker. I have created a Docker image for my Laravel project and successfully run it on my laptop as follow.
I created a Dockerfile in the project root folder with the following content.
FROM php:7
RUN apt-get update -y && apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo mbstring
WORKDIR /app
COPY . /app
RUN composer install
CMD php artisan serve --host=0.0.0.0 --port=8181
EXPOSE 8181
Then I built the image like this
docker build -t waiyanhein/laravel c:/xampp/htdocs/docker_laravel
Then I run it locally by running the following command.
docker run –p 8181:8181 waiyanhein/laravel
Everything was working. Then I tried to deploy the image to Heroku. I followed this link, https://devcenter.heroku.com/articles/container-registry-and-runtime. As in the link, I logged into heroku.
heroku container:login
Login succeeded. Then I create the app running this command.
heroku create dockerlaravelwai
The command was successful and this is the result.
Then I pushed it as a next step as in the link mention by running the following command.
heroku container:push web
when I run the above command, I got the following error.
» Error: Missing required flag:
» -a, --app APP app to run command against
» See more help with --help
What went wrong? How can I easily deploy the Laravel Docker's image to Heroku?
Its asking for you to specify the app name
heroku container:push web --app dockerlaravelwai

Docker executing a command when running an image

I'm using Laravel with docker. Inside my dockerfile I have this command:
RUN php artisan config:cache
This is useful for production since it cache the configuration. When I'm developing I need to be able to change the configuration quite often, so every time I run the image I need to execute
RUN php artisan config:clear
is there a way to run the docker image with a given command? i.e.:
docker run my_image "php artisan config:clear"
(If you're wondering, I'm doing docker run instead of docker start quite often since I'm building the image quite often.)
I'd like to avoid using CMD inside dockerfile since it's not really needed.
Thanks
If you want to run the command inside a currently running container you can use the exec command:
$ docker exec yourapp_web_1 php artisan config:clear

Resources