Can't copy project folder on Windows 10 to docker container - docker

I'm learning about Docker and trying to up a container using php, apache and Lumen Framework. When I execute the command to build the container, returns success.
The problem is when I open the http://localhost:8080 and the page show me 403 - forbidden on apache. I access the container by ssh and I look on the folder /srv/app/ and there's no files. I think that the problem is the mapping of the folder root on the host machine Windows.
I'm using Windows 10;
Anyone can help me?
My DockerFile
FROM php:7.2-apache
LABEL maintainer="rIckSanchez"
COPY docker/php/php.ini /usr/local/etc/php/
COPY . /srv/app
COPY docker/apache/vhost.conf /etc/apache2/site-available/000-default.conf
My docker-compose file
version: '3'
services:
phpinfo:
build: .
ports:
- "8080:80"

Related

Dockerfile its not copying all files

I'm trying to run a discord.py bot on a docker container. But when I'm running the container, docker says that I'm "missing a module". The Dockerfile its not copying all the files/folders from the source code.
This is my directory:
These are the contents of my docker-compose.yml:
version: '3'
services:
bot:
build: .
restart: always
volumes:
- ./.env:/usr/src/app/.env
This is my Dockerfile:
FROM python:bullseye
WORKDIR /usr/app/src
COPY bot bot
CMD ["python", "-m", "bot"]
When I run # sudo docker compose up It fails with the following log:
Checking the docker image files, it seems like its copying all the contents inside of the bot folder, but its not copying the folder itself.
The code works fine if I run it outside of the container, so is not related to this discord bot code.
How can I fix this?
This is my first docker container I'm new really with this.
The correct syntax should be:
COPY bot bot/
By design, COPY always copies the contents of the directory if the source is a directory, and by adding the trailing / to the destination you tell docker that the destination is a directory, so it will create it for you if needed.
See the full documentation.

When running docker-compose remotely, an error occurs with mounting volumes

I am trying to run a project on docker-compose via a remote server. Everything works, but as soon as I add the item about mounting the volume, it gives an error:
Error response from daemon: invalid mount config for type "bind": invalid mount path: 'C:/Users/user/Projects/my-raspberry-test' mount path must be absolute
To run I use tools from PhpStorm.
The docker-compose.yml file itself looks like this:
version: "3"
services:
php:
image: php:cli
volumes:
- ./:/var/www/html/
working_dir: /var/www/html/
ports:
- 80:80
command: php -S 0.0.0.0:80
I checked by ssh:
Daemon is running,
Docker works (on a similar Dockerfile with the same tasks),
Docker-compose works (on the same file).
Also checked docker remote run using phpstorm and file:
FROM php:cli
COPY . /var/www/html/
WORKDIR /var/www/html/
CMD php -S 0.0.0.0:80
It didn’t give an error and it worked.
OS on devices:
PC: Windows 10
Server: Fedora Server
Without mounting the volume in docker-compose, everything starts. Maybe someone faced a similar problem?
php for an example.
The path must be absolute for the remote host and the project data itself must be loaded there. That is, you need to upload the project to a remote host.
I corrected everything like this:
volumes:
- /home/peter-alexeev/my-test:/var/www/html/

Mount folder to docker container via dockerfile or docker-compose.yml file?

I need to edit nginx.conf file in /etc/nginx/ folder from a service from within a docker container. Is there a way to do this through Dockerfile or docker-compose.yml file? All the solutions I have come across only mention using docker run command.
Well there are multiple ways, I assume you want your docker container to have specific files while running right? then I would recommend use in Dockerfile like this
COPY nginx.conf /etc/nginx/
I would highly suggest copy command because this copy of file will live along with image.
or you can mount this via docker-compose like this
services:
frontend:
build: ./nginx
volumes:
- ./nginx.conf:/etc/nginx/
container_name: nginx

Docker unable to COPY with relative path in WSL2

I have a project that has a docker-compose.yml set up to get it running locally for development purposes. It runs great on Linux (natively) and macOS (using Docker Desktop). I am just finishing getting it running on Windows using WSL2 and Docker Desktop 2.3.0.3 (that has proper WSL2 support). The problem is that my Dockerfile is doing a COPY ./from /to command and Docker doesn't seem to be able to find the file. I have set up a minimal test to recreate the problem.
I have the project set up with this directory structure:
docker/
nginx/
Dockerfile
nginx.conf
docker-compose.yml
The nginx Dockerfile contains:
FROM nginx:1.17.9-alpine
# Add nginx configs
COPY ./docker/nginx/nginx.conf /etc/nginx/nginx.conf
# Copy source code for things like static assets
COPY . /application
# Expose HTTP/HTTPS ports
EXPOSE 80 443
And the docker-compose.yml file contains:
version: "3.1"
services:
nginx:
build: docker/nginx
working_dir: /application
volumes:
- .:/application
ports:
- "80:80"
This is pretty basic - it's just copying the nginx.conf configuration file to /etc/nginx/nginx.conf inside the container.
When I run docker-compose up for this project, from the project root, inside WSL, I receive the following error:
Building nginx
Step 1/4 : FROM nginx:1.17.9-alpine
---> 377c0837328f
Step 2/4 : COPY ./docker/nginx/nginx.conf /etc/nginx/nginx.conf
ERROR: Service 'nginx' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder502655363/docker/nginx/nginx.conf: no such file or directory
This is not what I expect (and not what happens on linux/mac systems) - but I assume it's messing up because of the relative path specified in the Dockerfile? Is this a Docker Desktop bug specifically with WSL, and does anybody know a workaround for the mean time? Thank you!
The path in the Dockerfile should be relative to the build context path. In this example just nginx.conf because the context path is docker/nginx.

Does docker compose create port mappings automatically?

I created a simple asp.net core app in Visual Studio 2019 and added docker support.
Dockerfile, .dockerignore, and docker-compose file are all created.
In a command prompt I navigate to the folder docker-compose.yml file is present and then run the command
docker-compose up
I see that the containers are created and port mappings happen so that I can browse the web app in the browser.
So when I run the following inspect command on the container
docker inspect --format="{{ .NetworkSettings.Ports}}" ContainerId
I get something like this
map[80/tcp:[{0.0.0.0 32782}]]
So now I can browse the app with http://localhost:32782/index.html
Next if I tear down the containers with
docker-compose down
the containers are stopped and deleted. Created image remains.
Now when I do a docker run against that image to start a container
docker run -it --rm ae39
a new container is created but I am not able to browse the app because there is no port mapping from container to host. I have to explicitly specify this when I use the run command. Only then I am able to browse the app running inside of the container form the host.
But when I use docker compose I dont have to specify the port mapping. Something magical happens and the ports mappings are created for me. Note that the docker-compose.yml file is plane vanilla and does not contain any port mappings. So also the Dockerfile. They are included below for reference.
My question is does docker compose automatically create port mappings? If so how? Of is that is to do something with Visual Studio 2019
version: '3.4'
services:
generator31:
image: ${DOCKER_REGISTRY-}generator31
build:
context: .
dockerfile: generator31/Dockerfile
And the dockerfile is here.
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["generator31/generator31.csproj", "generator31/"]
RUN dotnet restore "generator31/generator31.csproj"
COPY . .
WORKDIR "/src/generator31"
RUN dotnet build "generator31.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "generator31.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "generator31.dll"]
I think I found the answer.
The key is the config command
docker-compose config
After I run that command, I see the following.
services:
generator31:
build:
context: D:\Trials\Docker\aspnetcore311\generator31
dockerfile: generator31/Dockerfile
environment:
ASPNETCORE_ENVIRONMENT: Development
image: generator31
ports:
- target: 80
version: '3.4'
I see ports, and also environment. In the docker compose file I pasted in the question, they are not present. So where did they come from?
I open the folder in file explorer, and I see docker-compose.override.yml file sitting quietly next to docker-compose.yml file. Open that file and I get the answer.
ports is defined there. Docker compose command picks up the configuration from multiple files, docker-compose.override.yml is one such file. And now this so ans should help as Fabian-Desnoes suggsted.
By having the EXPOSE tag in your dockerfile, it tells the platform that you are using that you need that port to be mapped. When using docker-compose, it will see this and automatically map this to a random open port. The EXPOSE keyword doesn't however expose any ports. It is just used to say to the platform you are using "Could you expose this port for me" and relies on the platform to do it for you.
If you want the ports to be mapped to a specific port every time on your host machine then you can add 'ports' to the service in you docker-compose.yml (host_machine_port:container_port)

Resources