Install PHP composer in existing docker image - docker

I'm running docker-letsencrypt through a docker-compose.yml file. It comes with PHP. I'm trying to run PHP composer with it. I can install composer while being in the container through bash, but that won't stick when I recreate the container. How do I keep a permanent install of composer in an existing container that doesn't come with compose by default?
My docker-compose.yml looks like this:
version: "3"
services:
letsencrypt:
image: linuxserver/letsencrypt
container_name: letsencrypt
cap_add:
- NET_ADMIN
ports:
- "80:80"
- "443:443"
environment:
- PUID=1000
- PGID=1000
- EMAIL=<mailadress>
- URL=<tld>
- SUBDOMAINS=<subdomains>
- VALIDATION=http
- TZ=Europe/Paris
volumes:
- /home/ubuntu/letsencrypt:/config
I did find the one-line installer for composer:
php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
I could add this to command in my docker-compose.yml, but that would reinstall composer even on container restarts right?

You're right about your comment about the command option, it will indeed be run every time you launch your container.
One workaround would be to create your own dockerfile, as follow :
FROM linuxserver/letsencrypt
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
(RUN directives are only run during the build step).
You should then modify your docker-compose.yml :
...
build: ./dir
#dir/ is the folder where your Dockerfile resides
#use the dockerfile directive if you use a non-default naming convention
#or if your Dockerfile isn't at the root of your project
container_name: letsencrypt
...

Related

'(root) additional property nginx is not allowed' while installing an app in Salesforce using Docker

I'm following this tutorial https://trailhead.salesforce.com/en/content/learn/modules/user-interface-api/install-sample-app?trail_id=force_com_dev_intermediate and I have never used docker before.
Steps I followed:
Cloned the repo
Installed docker for windows and it is perfectly installed.
Tried to run this cmd on the repo docker-compose build && docker-compose up -d
While running this cmd, I'm getting the same error.
E:\Salesforce\RecordViewer>docker-compose build && docker-compose up -d
(root) Additional property nginx is not allowed
I found this answer: https://stackoverflow.com/a/38717336/279771
Basically I needed to add services: to the docker-compose.yml so it looks like this:
services:
web:
build: .
command: 'bash -c ''node app.js'''
working_dir: /usr/src/app
environment:
PORT: 8050
NGINX_PORT: 8443
volumes:
- './views:/app/user/views:ro'
nginx:
build: nginx
ports:
- '8080:80'
- '8443:443'
links:
- web:web
volumes_from:
- web

Grails App running in Docker Container not using Local Packages

I'm currently trying to run our app that's in Grails 2.3.11 through docker-compose with the database. I have the database up and running without issue, and also the app container sets up grails and starts the compilation process, but it goes on to downloading all the packages every time I stop and restart the package. This becomes an issue because we have to download so many packages (And there's a bunch of errors we have to work around because Grails 2). I've tried to mount my local grails folders into the container to have it run off of those but it seems to not be having any success. Is there something obvious I'm doing wrong, or some way I can easily check where the issue might be?
I'm also attempting to map all local database information into the mysql container with issue. But I haven't looked into it much yet, if you see an obvious issue there that would be helpful.
docker-compose.yml:
version: '2'
services:
grails:
image: ibbrussell/grails:2.3.11
command: run-app
volumes:
- ~/.m2:/home/developer/.m2
- ~/.gradle:/home/developer/.gradle
- ~/.grails:/home/developer/.grails
- ./:/app
ports:
- "8080:8080" #Grails default port
- "5005:5005" #Grails debug port
links:
- db
deploy:
resources:
limits:
memory: 4G
reservations:
memory: 4G
db:
image: mysql:5.6
container_name: grails_mysql
ports:
- "3306:3306"
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
MYSQL_DATABASE: grails
volumes:
- "/usr/local/mysql/data:/var/lib/mysql"
Dockerfile:
FROM java:8
# Set customizable env vars defaults.
ENV GRAILS_VERSION 2.3.11
# Install Grails
WORKDIR /usr/lib/jvm
RUN wget https://github.com/grails/grails-core/releases/download/v$GRAILS_VERSION/grails-$GRAILS_VERSION.zip && \
unzip grails-$GRAILS_VERSION.zip && \
rm -rf grails-$GRAILS_VERSION.zip && \
ln -s grails-$GRAILS_VERSION grails
# Setup Grails path.
ENV GRAILS_HOME /usr/lib/jvm/grails
ENV PATH $GRAILS_HOME/bin:$PATH
ENV GRAILS_OPTS="-XX:MaxPermSize=4g -Xms4g -Xmx4g"
# Create App Directory
RUN mkdir /app
# Set Workdir
WORKDIR /app
# Set Default Behavior
ENTRYPOINT ["grails"]
So the mapping I was using ended up not being correct. I was going off a file mapping from 1 article and ended up working after trying another working mapping. I made the switch below:
original:
volumes:
- ~/.m2:/home/developer/.m2
- ~/.gradle:/home/developer/.gradle
- ~/.grails:/home/developer/.grails
- ./:/app
new:
volumes:
- ~/.m2:/root/.m2
- ~/.gradle:/root/.gradle
- ~/.grails:/root/.grails
- ./:/app

Running docker-compose up, stuck on a "infinite" "creating...[container/image]" php and mysql images

I'm new to Docker, so i don't know if it's a programming mistake or something, one thing i found strange is that in a Mac it worked fine, but running on windows, doesn't.
docker-compose.yml
version: '2.1'
services:
db:
build: ./backend
restart: always
ports:
- "3306:3306"
volumes:
- /var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=123
- MYSQL_DATABASE=demo
- MYSQL_USER=user
- MYSQL_PASSWORD=123
php:
build: ./frontend
ports:
- "80:80"
volumes:
- ./frontend:/var/www/html
links:
- db
Docker file inside ./frontend
FROM php:7.2-apache
# Enable mysqli to connect to database
RUN docker-php-ext-install mysqli
# Document root
WORKDIR /var/www/html
COPY . /var/www/html/
Dockerfile inside ./backend
FROM mysql:5.7
COPY ./demo.sql /docker-entrypoint-initdb.d
Console:
$ docker-compose up
Creating phpsampleapp_db_1 ... done
Creating phpsampleapp_db_1 ...
Creating phpsampleapp_php_1 ...
It stays forever like that, i tried a bunch of things.
I'm using Docker version 17.12.0-ce. And enabled Linux container mode.
I think i don't need the "version" and "services", but anyway.
Thanks.
In my case, the fix was simply to restart Docker Desktop. After that all went smoothly

Docker-compose volumes doesn't copy any files

I'm in Fedora 23 and i'm using docker-compose to build two containers: app and db.
I want to use that docker as my dev env, but have to execute docker-compose build and up every time i change the code isn't nice. So i was searching and tried the "volumes" option but my code doesn't get copied to docker.
When i run docker-build, a "RUN ls" command doesn't list the "app" folder or any files of it.
Obs.: in the root folder I have: docker-compose.yml, .gitignore, app (folder), db (folder)
ObsĀ¹.: If I remove the volumes and working_dir options and instead I use a "COPY . /app" command inside the app/Dockerfile it works and my app is running, but I want it to sync my code.
Anyone know how to make it work?
My docker-compose file is:
version: '2'
services:
app:
build: ./app
ports:
- "3000:3000"
depends_on:
- db
environment:
- DATABASE_HOST=db
- DATABASE_USER=myuser
- DATABASE_PASSWORD=mypass
- DATABASE_NAME=dbusuarios
- PORT=3000
volumes:
- ./app:/app
working_dir: /app
db:
build: ./db
environment:
- MYSQL_ROOT_PASSWORD=123
- MYSQL_DATABASE=dbusuarios
- MYSQL_USER=myuser
- MYSQL_PASSWORD=mypass
Here you can see my app container Dockerfile:
https://gist.github.com/jradesenv/d3b5c09f2fcf3a41f392d665e4ca0fb9
Heres the output of the RUN ls command inside Dockerfile:
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
A volume is mounted in a container. The Dockerfile is used to create the image, and that image is used to make the container. What that means is a RUN ls inside your Dockerfile will show the filesystem before the volume is mounted. If you need these files to be part of the image for your build to complete, they shouldn't be in the volume and you'll need to copy them with the COPY command as you've described. If you simply want evidence that these files are mounted inside your running container, run a
docker exec $container_name ls -l /
Where $container_name will be something like ${folder_name}_app_1, which you'll see in a docker ps.
Two things, have you tried version: '3' version two seems to be outdated. Also try putting the working_dir into the Dockerfile rather than the docker-compose. Maybe it's not supported in version 2?
This is a recent docker-compose I have used with volumes and workdirs in the respective Dockerfiles:
version: '3'
services:
frontend:
build:
context: ./frontend
dockerfile: Dockerfile.dev
ports:
- 3001:3001
volumes:
- ./frontend:/app
networks:
- frontend
backend:
build: .
ports:
- 3000:3000
volumes:
- .:/app
networks:
- frontend
- backend
depends_on:
- "mongo"
mongo:
image: mongo
volumes:
- ./data/db:/data/db
ports:
- 27017:27017
networks:
- backend
networks:
frontend:
backend:
You can extend or override docker compose configuration. Please follow for more info: https://docs.docker.com/compose/extends/
I had this same issue in Windows!
volumes:
- ./src/:/var/www/html
In windows ./src/ this syntax might not work in regular command prompt, so use powershell instead and then run docker-compose up -d.
it should work if it's a mounting issue.

Writable folder permissions in docker

I have a docker setup with some websites for localhost. I use Smarty as my template engine and it requires to have a writable templates_c folder. Any idea how I can make this folder writable?
The error is as following:
PHP Fatal error: Smarty error: unable to write to $compile_dir
'/var/www/html/sitename.local/httpdocs/templates_c'.
Be sure $compile_dir is writable by the web server user. in
/var/www/html/sitename.local/httpdocs/libs/Smarty.class.php on
line 1093
I know this could be set manually with linux but I am looking for an automatic global solution since I have many websites who have this issue
Also worth mentioning I am using a pretty clean docker-compose.yml
php56:
build: .
dockerfile: /etc/docker/dockerfile_php_56
volumes:
- ./sites:/var/www/html
- ./etc/php:/usr/local/etc/php
- ./etc/apache2/apache2.conf:/etc/apache2/conf-enabled/apache2.conf
- ./etc/apache2/hosts.conf:/etc/apache2/sites-enabled/hosts.conf
ports:
- "80:80"
- "8080:8080"
links:
- mysql
mysql:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=MY_PASSWORD
- MYSQL_DATABASE=YOUR_DATABASE_NAME
volumes:
- ./etc/mysql:/docker-entrypoint-initdb.d
With a small dockerfile for basics:
FROM php:5.6-apache
RUN /usr/local/bin/docker-php-ext-install mysqli mysql
RUN docker-php-ext-configure mysql --with-libdir=lib/x86_64-linux-gnu/ \
&& docker-php-ext-install mysql
RUN a2enmod rewrite
https://github.com/wesleyd85/docker-php7-httpd-apache2-mysql (but then with php 5.6)
I solved the same problem with the solution here: Running docker on Ubuntu: mounted host volume is not writable from container
Just need to add:
RUN chmod a+rwx -R project-dir/smarty.cache.dir
to Dockerfile

Resources