Setup
I'm running Docker on my Ubuntu server and I'm trying create a Laravel container to run my website with artisan. The Laravel project is inside a GitHub Repository and I clone the project to the docker container with a dockerfile.
Problem
Laravel projects are dependent on the .env (environment files) which are not included in the repo project, for security reasons. So when I clone the repo to the docker container it doesn't include the .env file and thereby doesn't run the website properly. I have an .env file locally on my Ubuntu that I'm trying to COPY to the docker container Laravel project folder, ofcourse it doesn't work. This is because it's looking for the directory in the docker container's file structure.
Error
Step 6/11 : COPY /containers/.env .env
lstat containers/.env: no such file or directory
Question
How can I copy the .env file from the ubuntu server to the docker container with the COPY command?
file structure (Ubuntu) source from:
root/
containers/
- docker-compose
- .env
file structure (docker container) source to:
root/
var/www/
dockerfile
FROM hitalos/laravel
RUN git config --system http.sslverify false
RUN git clone repo /var/www
RUN git checkout test
COPY /containers/.env .env
# Run Compser Install
RUN composer install -d /var/www
RUN php /var/www/artisan key:generate
WORKDIR /var/www
CMD php /var/www/artisan serve --port=80 --host=0.0.0.0
EXPOSE 80
Simply copying the .env file is not going to work since you also have to run a source command on the file and then an export command to add each environment variable to your path.
Since you are using docker-compose then you can use the env_file like so in your docker-compose.yml:
env_file:
-.env
This should automatically set the values required by Laravel from your .env file when you build your conainer.
The path in COPY is relative to the Dockerfile
just change it to COPY containers/.env /var/www/.env
EDIT:
seems like you don't have the .env file at build time (image), only at runtime (container). That means, you have to mount the file when running the container.
Remove the COPY ... command from Dockerfile and instead run the container with
-v /containers/.env:/var/www/.env
so something like this:
docker run... -v /containers/.env:/var/www/.env ...
or change it in the compose yml file
Related
I Created a Laravel app and I am using a windows PC in combination with Docker and Laradock.
Docker runs on WSL2 but mounted to my windows drives (I know, it is very slow and I should switch to working in the Linux Filesystem).
I want to use Laravel's filesystem because I use envoyer to deploy my app.
Therefor I created a symlink from public/storage to storage/app/public (default) with php artisan storage:link in the workspace container.
My IDE (PHPStorm) correctly displays the files from the link.
Uploading images and thus moving them to the storage folder works as well.
The problem with Laradock (local development setup) is that if I use the URL from the asset() function (mywebsite.test/storage/file.txt for example), I get 404 Not Found.
This problem does not persist on my staging linux server. Everything works fine over there.
Someone any idea what the problem could be?
Apparently the issue was that you should always make the symlink in the workspace container.
My process was the following:
docker-compose down
docker-compose up
docker ps to get workspace container id
docker exec -it [workspace-container-id] bash
php artisan storage:link in project folder. If this does not work, you can create it manualy from the public folder with ln -sf ../storage/app/public storage
I'm a beginner in working with docker especially docker compose. Currently, creation my initial easy docker environment, I run into the first error and I've no clue why.
I tried to search for a solution in stackoverflow but found nothing that could help me.
Starting my docker with "docker-compose up" I get the following error:
$ docker-compose up
Removing errorinstance_app_1
Recreating 8a358dfcb306_8a358dfcb306_8a358dfcb306_errorinstance_app_1 ...
Recreating 8a358dfcb306_8a358dfcb306_8a358dfcb306_errorinstance_app_1 ... error
ERROR: for 8a358dfcb306_8a358dfcb306_8a358dfcb306_errorinstance_app_1 Cannot start service app: oci runtime error: container_linux.go:265: starting container process caused "exec: \"./run.sh\": stat ./run.sh: no such file or directory"
ERROR: for app Cannot start service app: oci runtime error: container_linux.go:265: starting container process caused "exec: \"./run.sh\": stat ./run.sh: no such file or directory"
ERROR: Encountered errors while bringing up the project.
So. Following my folder structure:
Project
docker-compose.yml
Docker
Java
Dockerfile
src
run.sh
Following my docker-compose.yml:
version: '2'
services:
app:
build:
dockerfile: ./Docker/Java/Dockerfile
context: .
volumes:
- ./src:/usr/local/etc/
working_dir: /usr/local/etc/
command: ./run.sh
And following my docker file:
FROM java:7-jdk-alpine
# WORKDIR /usr/local/etc
run.sh
echo "Hello world."
Yes, I know that I could do that solution only in a docker-compose file. But in the future I need to extend the Dockerfile.
Can someone help me respectively does anyone see the issue?
The problem is with the base docker image you are using in dockerfile:
FROM java:7-jdk-alpine
You are trying to start container by running run.sh bash script. But the above image doesn't support bash itself
For reference, you can see the documentation of above image in docker hub page here. Quoting the necessary portion here:
java:alpine
...
To minimize image size, it's uncommon for additional related tools
(such as git or bash) to be included in Alpine-based images. Using
this image as a base, add the things you need in your own Dockerfile
(see the alpine image description for examples of how to install
packages if you are unfamiliar).
That's about the problem.
Now, I can think of 2 solutions:
Just use java:7-jdk as base image instead of java:7-jdk-alpine
Install bash on top of the base image java:7-jdk-alpine by changing dockerfile to:
FROM java:7-jdk-alpine
RUN apk update && apk upgrade && apk add bash
#WORKDIR /usr/local/etc
*source of steps to install bash in alpine linux is here
It looks like docker compose can't find your run.sh file. This file needs to be included in your docker image.
Change your Dockerfile to the following, then rebuild the image with docker build -t <YOUR_IMAGE_NAME> ..
FROM java:7-jdk-alpine
ADD run.sh /usr/local/etc/run.sh
Once your image is rebuilt, run docker-compose up again.
The easiest way to tackle the problem is to execute a bash session in the container, then inside the container, you have to check if the file exists in the
indicated path if the file is not in the path, it must be included when you create the image into the docker file or through a volume inside de docker-compose.
Another thing to check is the relative path you are using. It will be clear when you check the existence of the file inside de docker container
docker exec -it CONTAINER_NAME bash
I recommend you to create a volume in the docker compose file, as it is the easier way, and also the best way.
there is a question that I want to do you, why are you putting the Dockerfile file inside a Java path?
It is not a good idea o guideline to follow
The correct way is to put your dockerfile file into an environment folder, in such a way the dockerfile file is not related to the java source of your application
I got this Error quite a lot and after a lot of investigation, it looked like some images were corrupted.
Deleting those and rebuilding solve the problem. It was not docker installation or configuration itself.
I have a .NET Core Web API with a Dockerfile and I want in the Dockerfile, from my Windows host container current user directory to copy a file to the docker container.
Basically, I've tried the following command:
FROM microsoft/aspnetcore:1.1
ARG source
WORKDIR /app
EXPOSE 80
COPY ${source:-obj/Docker/publish} .
ADD C:/Users/MyUser/custom_directory/myfile /root/other_directory/myfile
ENTRYPOINT ["dotnet", "MyApi.dll"]
But I get the following error:
Service 'myservice' failed to build: lstat C:/Users/MyUser/custom_directory/myfile: no such file or directory
How can I fix this error?
You can ADD (copy) only files from a source directory. In this case Visual Studio project directory.
Copy myfile to project directory and add a new line "myfile" to .dockerignore file. Default VS dockerignore file is configured to ignore all files except obj/Docker/publish/* and obj/Docker/empty/.
For better understanding read this:
https://docs.docker.com/engine/reference/builder/#dockerignore-file
https://docs.docker.com/engine/reference/builder/#add
I installed docker toolbox 1.11.2 and Laradock v.2 cloned from GitHub.
Everything seems to work except the laradock_workspace_1. When is generated it does not create files on the host machine (Windows 7 64-bit). In the docker-compose.yml I have tried playing with the volumes as suggested here
### Laravel Application Code Container ######################
volumes_source:
build: ./volumes/application
volumes:
- ../:/var/www/laravel
If I change the last line to ../.. then run docker-compose up, docker exec -it laradock_workspace_1 ls and I can see that it is traversing the folders on the host machine. I just don't see any files.
My goal here is to make the actual Laravel code external so I can edit them on the host machine and use git.
I can use the Kitematic app to make the changes I want but they seem lost if I do a docker-compose down. (and I get errors about things still being in use.)
I'm new to docker so any help is appreciated.
First, make sure your docker-machine is running. If it is, then follow below:
Open up Virtualbox GUI and right click your docker vm, and select settings, then go to Shared Folders.
Change the c\users to whatever folder your code lies in, like this:
This will mount your desired folder to /c/Users in the docker-machine vm.
After this, change the docker-compose.yml in the laradock folder to this:
### Laravel Application Code Container ######################
volumes_source:
build: ./volumes/application
volumes:
- /c/Users/pomodoro.xyz/code:/var/www/laravel
The logic behind this is, since we are running the docker in a VM, the docker-compose command looks for folder in the VM, not in the windows machines. Thats why we have provided the VM machine path to the docker-compose file.
I am using EB on AWS to deploy a dockerfile.
Currently I deploy to scripts:
The dockerfile and a run.sh file which starts a server.
The dockerfile roughly looks like this
FROM ubuntu:14.04
MAINTAINER xy
[...install a java server...]
ADD run.sh /run.sh
RUN chmod +x /*.sh
EXPOSE 8080
CMD ["/run.sh"]
run.sh starts the java server.
I would like to set the --no-cache flag for the docker. Where can I set that?
You can't specify docker build's --no-cache because eb doesn't allow you to.
A workaround is to build the image locally (using --no-cache). Then use docker push to publish your image to Docker hub public registry.
Your Dockerfile could be simplified (untested) down to:
FROM custom_java_server_build:latest
MAINTAINER xy
EXPOSE 8080
CMD ["/run.sh"]
It does sound like you're creating a large image, you might be able to mitigate this by turning the entire install sequence into a single RUN statement. Don't forget to delete all your temporary files too.
You use --no-cache only in the docker build step. If the run script doesn't build the image, then you need to find what is building it and tell that process to use no-cache.