COPY doesn't work on Docker prod - nginx

My Dockerfile looks like this:
FROM nginx
COPY dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/
My docker-compose.yml file looks like this:
version: '2'
services:
portfolio:
build: .
ports:
- "80:80"
When I run docker-compose up -d on my Mac, Im able to verify that the nginx.conf gets copied into the container.
However, when I run docker-compose up -d on my digital ocean prod machine, the nginx.conf file doesn't get copied over! Instead, I find the default nginx.conf file in /etc/nginx.
What am I missing here?

OK, the following worked:
clearing all images and containers, docker-compose down, then running docker-compose up worked! Docker now copies over my nginx.conf file just file!
To anyone else who is stuck with the same scenario follow this guide

Related

How to update wordpress on docker

I'm running a php-fpm wordpress container.
The wordpress source files are mounted in a named volume "wordpress" shared with the Nginx container.
Everything is running well except when i need to update wordpress to a new version. The code inside the named volume persists. It is normal for a named volume...
I could manually delete the volume but there must be a better way.
My dockerfile:
FROM wordpress:4.9.5-php5.6-fpm-alpine
My docker-compose.yml
version: '3.1'
services:
php:
build: ./docker/php/
restart: unless-stopped
volumes:
- wordpress:/var/www/html
- ./web/wp-content/:/var/www/html/wp-content/
- ./web/wp-config.php:/var/www/html/wp-config.php
environment:
- DEBUG=${DEBUG:-0}
- MYSQL_USER=$MYSQL_USER
- MYSQL_PASSWORD=$MYSQL_PASSWORD
- MYSQL_DATABASE=$MYSQL_DATABASE
nginx:
image: nginx:1-alpine
restart: unless-stopped
expose:
- 80
volumes:
- wordpress:/var/www/html
- ./web/wp-content/:/var/www/html/wp-content/
- ./docker/nginx/site.conf:/etc/nginx/conf.d/default.conf
- ./docker/nginx/wordpress.conf:/etc/nginx/wordpress.conf
environment:
- VIRTUAL_HOST=localhost
mysql:
image: mysql:5.6
restart: unless-stopped
environment:
- MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD
- MYSQL_USER=$MYSQL_USER
- MYSQL_PASSWORD=$MYSQL_PASSWORD
- MYSQL_DATABASE=$MYSQL_DATABASE
volumes:
- mysql:/var/lib/mysql
volumes:
wordpress: {}
mysql: {}
networks:
default:
external:
name: wordpress
Looking forward to reading your suggestions
Thank you
When the wordpress container comes up it checks for the existence of files at /var/www/html and copies only if not present. So in your case may you can update the entrypoint script to check the wordpress version in the wp-includes/version.php in the /var/www/html and the files in the container and then make a decision to replace the new files.
Edit:
According to this just deletion of index.php or wp-includes/version.php should copy the files from container again. Or may you can update your entrypoint script to copy files to /var/www/html all the time, but that may cause issues if you choose to scale the wordpress layer.
Thank you for your help.
It worked.
Here is the code i'm using.
I overriden the entrypoint in dockerfile
COPY check-wordpress-version.sh /usr/local/bin/
ENTRYPOINT ["check-wordpress-version.sh"]
Here is the content of check-wordpress-version.sh to check wordpress current version.
VOLUME_VERSION="$(php -r 'require('"'"'/var/www/html/wp-includes/version.php'"'"'); echo $wp_version;')"
echo "Volume version : "$VOLUME_VERSION
echo "WordPress version : "$WORDPRESS_VERSION
if [ $VOLUME_VERSION != $WORDPRESS_VERSION ]; then
echo "Forcing WordPress code update..."
rm -f /var/www/html/index.php
fi
docker-entrypoint.sh php-fpm
Wordpress seems to have addressed this under this issue.
I notice you are using a custom wp-config.php. Most likely, you can use the WORDPRESS_CONFIG_EXTRA for this rather than mounting wp-config.php.
Theoretically (per the link above), updating the image should update the database, but I have not confirmed.
Based on this, my stack.yml/docker-compose.yml looks like this:
environment:
WORDPRESS_CONFIG_EXTRA: |
define( 'AUTOMATIC_UPDATER_DISABLED', true );
volumes:
- "./themes:/var/www/html/wp-content/themes/"
- "./plugins:/var/www/html/wp-content/plugins/"
- "./uploads:/var/www/html/wp-content/uploads/"
It's easier solution.
You have to edit wp-config.php file by add define('FS_METHOD','direct'); to the end of file.
Save the file and run update. From now, you don't need FTP server to update your WordPress.
Remember! Before update make a backup :)
To expend on #Bigbenny's answer, my Dockerfile looked like the following:
FROM wordpress:latest
WORKDIR /var/www/html
COPY . /var/www/html
COPY check-wordpress-version.sh /usr/local/bin/
RUN chmod 755 /usr/local/bin/check-wordpress-version.sh
ENTRYPOINT ["/usr/local/bin/check-wordpress-version.sh"]
Two things to notice here:
I had to chmod 755 the file or I would get a permissions denied error
I placed the script inside the /usr/local/bin because for some reason when I would just use ENTRYPOINT["check-wordpress-version.sh"], the file wouldn't be found by the container.
I also, slightly tweaked the script to look like:
#!/bin/sh
VOLUME_VERSION="$(php -r 'require('"'"'/var/www/html/wp-includes/version.php'"'"'); echo $wp_version;')"
echo "Volume version : "$VOLUME_VERSION
echo "WordPress version : "$WORDPRESS_VERSION
if [ $VOLUME_VERSION != $WORDPRESS_VERSION ]; then
echo "Forcing WordPress code update..."
rm -f /var/www/html/index.php
rm -f /var/www/html/wp-includes/version.php
fi
docker-entrypoint.sh apache2-foreground
For my use-case, I had to use apache2-foreground rather than php-fpm; I also deleted the /var/www/html/wp-includes/version.php file.
Finally, in my docker-compose, instead of the using the image directive; I used build: ./wordpress.
I hope this helps!😁
My answer applied to the official docker wordpress image. So
probably off topic but might help someone.
If you are using docker-compose you can pull the latest image using this command.
docker pull wordpress
I believe this will update your core docker image. Any other local project which you docker-compose up -d with this yml image setting as this will use the latest update.
services:
wordpress:
image: wordpress:latest
If you currently running the image will you need to docker-compose down and docker-compose up -d to invoke the update.

Docker: How can I have sqlite db changes persist to the db file?

FROM golang:1.8
ADD . /go/src/beginnerapp
RUN go get -u github.com/gorilla/mux
RUN go get github.com/mattn/go-sqlite3
RUN go install beginnerapp/
VOLUME /go/src/beginnerapp/local-db
WORKDIR /go/src/beginnerapp
ENTRYPOINT /go/bin/beginnerapp
EXPOSE 8080
The sqlite db file is in the local-db directory but I don't seem to be using the VOLUME command correctly. Any ideas how I can have db changes to the sqlite db file persisted?
I don't mind if the volume is mounted before or after the build.
I also tried running the following command
user#cardboardlaptop:~/go/src/beginnerapp$ docker run -p 8080:8080 -v ./local-db:/go/src/beginnerapp/local-db beginnerapp
docker: Error response from daemon: create ./local-db: "./local-db" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
EDIT: Works with using /absolutepath/local-db instead of relative path ./local-db
You are not mounting volumes in a Dockerfile.
VOLUME tells docker that content on those directories can be mounted via docker run --volumes-from
You're right. Docker doesn't allow relative paths on volumes on command line.
Run your docker using absolute path:
docker run -v /host/db/local-db:/go/src/beginnerapp/local-db
Your db will be persisted in the host file /host/db/local-db
If you want to use relative paths, you can make it work with docker-compose with "volumes" tag:
volumes:
- ./local-db:/go/src/beginnerapp/local-db
You can try this configuration:
Put the Dockerfile in a directory, (e.g. /opt/docker/myproject)
create a docker-compose.yml file in the same path like this:
version: "2.0"
services:
myproject:
build: .
volumes:
- "./local-db:/go/src/beginnerapp/local-db"
Execute docker-compose up -d myproject in the same path.
Your db should be stored in /opt/docker/myproject/local-db
Just a comment. The content of local-db (if any) will be replaced by the content of ./local-db path (empty). If the container have any information (initialized database) will be a good idea to copy it with docker cp or include any init logic on an entrypoint or command shell script.

Linking exposed port of docker with with the default nginx port

I have this Dockerfile:
FROM alpine:3.4
RUN apk update
RUN apk add nginx
RUN apk update
RUN cp index.html /var/lib/nginx/html/
EXPOSE 8080
Now, how can I access the file index.html on lets say port 9000 on localhost? I got puzzled. Please ask if I am not clear. Just an outline to solution is highly appreciated.
The main application is nginx so start from an nginx Dockerfile and simply copy your index.html to it.
Assuming that you have index.html in your local directory (where the Dockerfile is located).
FROM nginx:1.10-alpine
COPY ./index.html /var/lib/nginx/html
Build using
docker build -t mywebserver:latest .
Then your docker-compose.yml file could look like:
version: "2"
services:
mywebserver:
image: mywebserver:latest
ports:
- "8080:80"
command: ["nginx", "-g", "daemon off;"]
And build the containers using
docker-compose up -d
The command could also be skipped but it's a good practice to include the actual command in the service definition.

Why I can't see my files inside a docker container?

I'm a Docker newbie and I'm trying to setup my first project.
To test how to play with it, I just cloned one ready-to-go project and I setup it (Project repo).
As the guide claims if I access a specific url, I reach the homepage. To be more specific a symfony start page.
Moreover with this command
docker run -i -t testdocker_application /bin/bash
I'm able to login to the container.
My problem is if I try to go to the application folder through bash, the folder that I shared with my host is empty.
I tried with another project, but the result is the same.
Where I'm wrong?
Here some infos about my env:
Ubuntu 12.04
Docker version 1.8.3, build f4bf5c7
Config:
application:
build: code
volumes:
- ./symfony:/var/www/symfony
- ./logs/symfony:/var/www/symfony/app/logs
tty: true
Looks like you have a docker-compose.yml file but are running the image with docker. You don't actually need docker-compose to start a single container. If you just want to start the container your command should look like this:
docker run -ti -v $(pwd)/symfony:/var/www/symfony -v $(pwd)/logs/symfony:/var/www/symfony/app/logs testdocker_application /bin/bash
To use your docker-compose.yml start your container with docker-compose up. You would also need to add the following to drop into a shell.
stdin_open: true
command: /bin/bash

Docker permissions development environment using a host mounted volume

I'm using docker-compose to set up a portable development environment for a bunch of symfony2 applications (though nothing I want to do is specific to symfony). I've decided to have the source files on the local machine exposed as a data volume with all the other dependencies in docker. This way developers can edit on the local file-system.
Everything works great, except that after running the app my cache and log files and the files created by composer in the /vendor directory are now owned by root.
I've read about this problem and some possible approaches here:
Changing permissions of added file to a Docker volume
But I can't quite quite tease out what changes I have to make in my docker-compose.yml file so that when my symphony container starts with docker-compose up any files that are created have the permissions of the user on the host machine.
I'm posting the file for reference, worker is where php, etc. live:
source:
image: symfony/worker-dev
volumes:
- $PWD:/var/www/app
mongodb:
image: mongo:2.4
ports:
- "27017:27017"
volumes_from:
- source
worker:
image: symfony/worker-dev
ports:
- "80:80"
- mongodb
volumes_from:
- source
volumes:
- "tmp/:/var/log/nginx"
One of the solutions is to execure the commands inside your container. I've tried multiple workarounds for the same issue I faced in the past. I find executing the command inside the container the most user-friendly.
Example command: docker-compose run CONTAINER_NAME php bin/console cache:clear. You may use make, ant or any modern tool to keep the commands short.
Example with Makefile:
all: | build run test
build: | docker-compose-build
run: | composer-install clear-cache
############## docker compose
docker-compose-build:
docker-compose build
############## composer
composer-install:
docker-compose run app composer install
composer-update:
docker-compose run app composer update
############## cache
clear-cache:
docker-compose run app php bin/console cache:clear
docker-set-permissions:
docker-compose run app chown -R www-data:www-data var/logs
docker-compose run app chown -R www-data:www-data var/cache
############## test
test:
docker-compose run app php bin/phpunit
Alternatively, you may introduce a .env file which contains a environment variables and then user one of the variables to run usermod command in the Docker container.

Resources