Docker doesn't copy new images - asp.net

I have a problem regarding Docker.
When I'm deploying a new version of my app image, the images i have added to the images folder in my wwwroot folder aren't copied..
My Dockerfile looks like this:
FROM microsoft/aspnetcore-build:1.0-projectjson
WORKDIR /app-src
COPY . .
RUN dotnet restore
RUN dotnet publish src/Test -o /app
EXPOSE 5000
WORKDIR /app
ENTRYPOINT ["dotnet", "Test.dll"]
And my docker-compose:
version: '3.8'
services:
app:
image: <dockeruser>/<imagename>:<tag>
links:
- db
environment:
ConnectionStrings__Dataconnection: "Host=db;Username=Username;Password=Password;Database=db"
ports:
- "5000:5000"
volumes:
- ~/data/images:/app/wwwroot/images
db:
image: postgres:9.5
ports:
- "31337:5432"
volumes:
- ~/data/db:/var/lib/postgresql/data/pgdata
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: db
PGDATA: /var/lib/postgresql/data/pgdata
My current version of docker is:
Docker version 18.09.7, build 2d0083d and docker-compose docker-compose version 1.26.2, build eefe0d31
The exact same files (except for the docker-compose version was set to 2 in docker-compose.yml) worked previously on docker version Docker version 17.03.0-ce, build 60ccb22 and docker-compose version docker-compose version 1.9.0, build 2585387
I store my new images in my repos wwwroot/images folder, and then push them to the repo, and then dockerhub automatically builds an image from the new commit. On the server i then pull the new docker-image and run the docker-compose down -v command followed by docker-compose up -d but the images is not available in the app afterwards.
Disclaimer: This is a project I have overtaken and I'm aware of some of the very old software versions.

Your images may be in your container image, but since you are doing a bind mount whatever is in your server’s “~/data/images” directory will basically “override/replace” what’s in your image when the container is created.
Try removing the volume from the app service, basically remove this:
volumes:
- ~/data/images:/app/wwwroot/images
The other thing you can try is to manually copy the images to the “~/data/images” directory on the server.

Related

Is it possible to run ui tests in codeception in the background?

I'm new to codeception and wonder, is it possible to run ui tests in the background, without opening test web browser every time?
I suspect, that I should change something in acceptance.suite.yml, but not sure what.
I would appreciate any help.
You can use headless browser. This will execute all the test flow almost exactly as it would work on regular UI mode while there will not be opened visual browser.
You can learn more about this here and in more similar resources.
You can use Docker to virtualize the WebDriver and Selenium.
Create two different files in the root directory. The Dockerfile will generate a container with PHP and composer to run your codeception tests in it.
Dockerfile
FROM php:8.0-cli-alpine
RUN apk -U upgrade --no-cache
# install composer
COPY --from=composer:2.2 /usr/bin/composer /usr/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER=1
ENV PATH="${PATH}:/root/.composer/vendor/bin"
WORKDIR /app
COPY composer.json composer.lock ./
RUN composer install --no-dev --no-scripts --no-progress \
&& composer clear-cache
COPY . /app
RUN composer dump-autoload --optimize --classmap-authoritative \
&& composer clear-cache
The second file is docker-compose.yml which is using a preconfigured Selenium image and bound your PHP codeception tests to one network, so that the container can talk with each other over the needed ports (4444 and 7900)
docker-compose.yml
---
version: '3.4'
services:
php:
build: .
depends_on:
- selenium
volumes:
- ./:/usr/src/app:rw,cached
selenium:
image: selenium/standalone-chrome:4
shm_size: 2gb
container_name: selenium
ports:
- "4444:4444"
- "7900:7900"
environment:
- VNC_NO_PASSWORD=1
- SCREEN_WIDTH=1920
- SCREEN_HEIGHT=1080
If you setup docker and your codeception project correctly, you can run these containers in the background.
docker-compose up -d
and execute your tests:
vendor/bin/codecept run
If you want to see, what the test is doing, you can visit http://localhost:7900 to connect to the browser in the container and you can see, what the test is executing.
If you are using the WebDriver module to run your tests with codeception, there is an option to configure your browser in headless mode.
It won't open any windows and the tests will run in the background without bothering you.
There is an example with chrome :
modules:
enabled:
- WebDriver
config:
WebDriver:
url: 'http://myapp.local'
browser: chrome
window_size: 1920x1080
capabilities:
chromeOptions:
args: ["--headless", "--no-sandbox"]

How to copy local wp-content files to Wordpress container using Dockerfile

I have a Wordpress container that I would like to copy local folders (with files) to at startup
I have local files in folders:
/html/wp-content/plugins
/html/wp-content/themes
/html/wp-content/uploads
I have a Dockerfile with:
FROM wordpress
COPY ./html/wp-content/plugins/ /var/www/html/wp-content/plugins
COPY ./html/wp-content/themes/ /var/www/html/wp-content/themes
COPY ./html/wp-content/uploads/ /var/www/html/wp-content/uploads
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["apache2-foreground"]
Although the COPY is succeeding (the build passes), the files are not showing up in these destination folders.
If I change the destination folders to be on any path before /html, eg if I put the destination to be /var or /var/www, then the files copy and I can see them in the container.
I checked out this old post here, which mentions that the /html folder is actually mounted as a volume at startup, and so I need to copy the files into this folder first
/usr/src/wordpress/wp-content
and then at startup these folders will be automatically copied across to /var/www/html/wp-content/. (This would explain why copying directly does not seem to work)
I tried that too, and while my local folders are indeed copied into these folders (I can see them in the container), they are then not copied across to /var/www/html/content at startup!
Is it possible to copy the local files directly into the /var/www/html folders via the dockerfile?
If not, how can I ensure that if copying to /usr/src/wordpress/wp-content, the folders will be copied across to /var/www/html/wp-content/ at startup?
(Some posts I've looked through that don't work, as this seems to be particular to Wordpress, and not Dockerfile COPY on its own:)
Dockerfile copy keep subdirectory structure
Docker is not copying subdirectory into Container
How to copy folders to docker image from Dockerfile?
https://www.serverlab.ca/tutorials/containers/docker/how-to-host-your-wordpress-site-with-docker/
Wordpress docker copy theme into exposed folder
Today I ran into the same problem. I wanted to add theme when building the image by using COPY. However, it did not work. This was because I already set my wp-content folder as a volume. It seems you cannot COPY into a volume.
The discussion in the following link helped me realise this:
https://github.com/docker-library/wordpress/issues/146
Below I have added my WordPres Dockerfile and docker-compose file. After commenting out the volume everything worked as expected.
I sure hope you do not need it anymore after two years, haha.
Reference: https://docs.docker.com/samples/wordpress/
Dockerfile
FROM wordpress:latest
WORKDIR /var/www/html
COPY ./wp-content/ ./wp-content/
RUN chmod -R 755 ./wp-content/
docker-compose.yml
version: "3"
services:
db:
build:
context: .
dockerfile: ./compose/local/db/Dockerfile
image: new_db_image_name
container_name: new_db_image_name
command: '--default-authentication-plugin=mysql_native_password'
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=somewordpress
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
expose:
- 3306
- 33060
wordpress:
build:
context: .
dockerfile: ./compose/local/wordpress/Dockerfile
image: new_wordpress_image_name
container_name: new_wordpress_image_name
# volumes:
# - wp-content:/var/www/html/wp-content
ports:
- 80:80
restart: always
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
volumes:
db_data:
# wp-content:

How do I go about installing a certificate on a nanoserver-1709 based image on a docker container?

I've been looking around this image for any cert managers but I can't find anything that will help me install a .cer certificate. The 1709 nanoserver image doesn't come with powershell so in order to use that I would have to do a multi-staged build with the microsoft/windowsservercore image but I'm not quite sure how I'd go about doing this, I can't seem to find anything through google that will help.
If anybody knows a way to install the cert with or withour a multi-stage build that would be very appreciated.
For those interested, here's my docker-compose.yml and Dockerfile
docker-compose.yml
version: '3'
services:
myapp:
image: myapp
ports:
- "5000:80"
build:
context: .
dockerfile: MyApp\Dockerfile
container_name: "myapp"
hostname: "myapp"
depends_on:
- db
db:
image: "microsoft/mssql-server-windows-express"
environment:
SA_PASSWORD: ""
ACCEPT_EULA: "Y"
container_name: "myapp"
hostname: "myapp"
Dockerfile
FROM microsoft/aspnetcore:2.0-nanoserver-1709 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0-nanoserver-1709 AS build
WORKDIR /src
COPY MyApp.sln ./
COPY MyApp/MyApp.csproj MyApp/
RUN dotnet restore -nowarn:msb3202,nu1503
COPY . .
WORKDIR /src/MyApp
RUN dotnet restore
RUN dotnet ef database update
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyApp.dll"]
Thanks!
On some images, there is a tool called certoc.exe that allows you to import a certificate (usage: certoc.exe -addstore root my_root_certificate.cer).
This tool is not present on the 1709 images, but is present on images such as microsoft/nanoserver:sac2016.
To sum-up, the best way I found to deal with that is to change the Dockerfile to contain something like:
FROM microsoft/nanoserver:sac2016 as tool
COPY --from=tool /Windows/System32/certoc.exe .
USER ContainerAdministrator
RUN certoc.exe -addstore root my_root_certificate.cer
Complete example available here: https://pvlerick.github.io/2018/11/how-to-run-an-https-asp.net-core-app-using-test-certificates-in-nanoserver-1709-1803-with-docker
A huge thanks to Joshua Chini here: https://joshuachini.com/2018/02/08/how-to-import-an-enterprise-certificate-into-a-windows-container/

Docker Compose Link Containers for Phpunit with Wordpress, MySQL

I would like to use a docker-compose app to run unit tests on a wordpress plugin.
Following (mostly) this tutorial I have created four containers:
my-wpdb:
image: mariadb
ports:
- "8081:3306"
environment:
MYSQL_ROOT_PASSWORD: dockerpass
my-wp:
image: wordpress
volumes:
- ./:/var/www/html
ports:
- "8080:80"
links:
- my-wpdb:mysql
environment:
WORDPRESS_DB_PASSWORD: dockerpass
my-wpcli:
image: tatemz/wp-cli
volumes_from:
- my-wp
links:
- my-wpdb:mysql
entrypoint: wp
command: "--info"
my-phpunit:
image: phpunit/phpunit
volumes_from:
- my-wp
links:
- my-wpdb
This tutorial got me as far as creating the phpunit files (xml, tests, bin, .travis), with the exception that I had to install subversion manually:
docker exec wp_my-wp_1 apt-get update
docker exec wp_my-wp_1 apt-get install -y wget git curl zip vim
docker exec wp_my-wp_1 apt-get install -y apache2 subversion libapache2-svn libsvn-perl
And run the last part of bin/install-wp-tests.sh manually in the database container:
docker exec wp_my-wpdb_1 mysqladmin create wordpress_test --user=root --password=dockerpass --host=localhost --protocol=tcp
I can run phpunit: docker-compose run --rm my-wp phpunit --help.
I can specify the config xml file:
docker-compose run --rm my-wp phpunit --configuration /var/www/html/wp-content/plugins/my-plugin/phpunit.xml.dist
However, the test wordpress installation is installed in the my-wp container's /tmp directory: /tmp/wordpress-tests-lib/includes/functions.php
I think I have to link the my-phpunit containers /tmp to the one in my-wp?
This doesn't answer my question, but as a solution to the problem, there is a github repo for a Docker Image that provides the wanted features: https://github.com/chriszarate/docker-wordpress as well as a wrapper you can invoke it through: https://github.com/chriszarate/docker-wordpress-vip
I wonder if for the initial set-up (noted in the question), it might make more sense to add Phpunit to the Wordpress container, rather than making a separate container for it.

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