How to copy files inside container with docker-compose - docker

I have a simple image that runs a jar file. That jar file inside the image needs a special configuration file in order to run.
In the location with the docker-compose.yml I have a folder named "carrier" and under this folder I have that file.
The docker-compose.yml:
version: "3.3"
services:
web:
image: "myimage:1.80.0.0"
ports:
- "61003:61003"
volumes:
- ./carrier:/var/local/Config/
When I hit docker-compose up it complains that the file is not there, so it doesn't copy it.
If I do another option like I did in the .sh command, something like this:
volumes:
- ./carrier:/var/local/Config/:shared
It complains about another error:
C:\Tasks\2246>docker-compose up
Removing 2246_web_1
Recreating 1fbf5d2bcea4_2246_web_1 ... error
ERROR: for 1fbf5d2bcea4_2246_web_1 Cannot start service web: path /host_mnt/c/Tasks/2246/carrier is mounted on / but it is not a shared mount
Can someone please help me?

Copy the files using Dockerfile, use below;
FROM myimage:1.80.0.0
RUN mkdir -p /var/local/Config/
COPY carrier /var/local/Config/
EXPOSE 61003
docker-compose.yml
version: "3.3"
services:
web:
build:
dockerfile: Dockerfile
context: '.'
ports:
- "61003:61003"
In the end, run below command to build new image and start container
docker-compose up -d --build

You can use Dockerfile if it does not copy.
Dockerfile;
FROM image
COPY files /var/local/Config/
EXPOSE 61003
Docker-compose;
version: "3.3"
services:
web:
build: . (path contains Dockerfile)
ports:
- "61003:61003"
volumes:
- ./carrier:/var/local/Config/

Remove the last /
volumes:
- ./carrier:/var/local/Config

I'm not sure but you can try to set full access permissions for all user to /carrier:
chmod -R 777 /carrier

Thanks all for all your answers.
Seems like finally docker warned me with some comparisons over the windows files vs Linux files when building the image. (Not with docker compose but with Dockerfile).
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
Tried it on linux and works.

Related

How can I add a file to my volume without writing a new file to the host?

I'm trying to run a Next.js project inside docker-compose. To take advantage of hot-reloading, I'm mounting in the entire project to the Docker image as a volume.
So far, so good!
This is where things are starting to get tricky: For this particular project, it turns out Apple Silicon users need a .babelrc file included in their dockerized app, but NOT in the files on their computer.
All other users do not need a .babelrc file at all.
To sum up, this is what I'd like to be able to do:
hot reload project (hence ./:/usr/src/app/)
have an environment variable write content to /usr/src/app/.babelrc.
not have a .babelrc in the host's project root.
My attempt at solving was including the .babelrc under ci-cd/.babelrc in the host file system.
Then I tried mounting the file as a volume like - ./ci-cd/.babelrc:/usr/src/app/.babelrc. But then a .babelrc file gets written back to the root of the project in the host filesystem.
I also tried include COPY ./ci-cd/.babelrc /usr/src/app/.babelrc within the Dockerfile, but it seems to be overwritten with docker-composes's volume property.
Here's my Dockerfile:
FROM node:14
WORKDIR /usr/src/app/
COPY package.json .
RUN npm install
And the docker-compose.yml:
version: "3.8"
services:
# Database image
psql:
image: postgres:13
restart: unless-stopped
ports:
- 5432:5432
# image for next.js project
webapp:
build: .
command: >
bash -c "npm run dev"
ports:
- 3002:3002
expose:
- 3002
depends_on:
- testing-psql
volumes:
- ./:/usr/src/app/

In Docker, how do I copy files from a local directory so that I can then copy those files into my Docker container?

I'm using Docker
Docker version 19.03.8, build afacb8b
I have the following docker-compose.yml file ...
version: "3.2"
services:
sql-server-db:
build: ./
container_name: sql-server-db
image: microsoft/mssql-server-linux:2017-latest
ports:
- "1433:1433"
environment:
SA_PASSWORD: "Password1!"
ACCEPT_EULA: "Y"
and here is the Docker file it uses to build ...
FROM microsoft/mssql-server-linux:latest
# Create work directory
RUN mkdir -p /usr/work
WORKDIR /usr/work
# Copy all scripts into working directory
COPY . /usr/work/
# Grant permissions for the import-data script to be executable
RUN chmod +x /usr/work/import-data.sh
EXPOSE 1433
CMD /bin/bash ./entrypoint.sh
On my local machine, I have some files in a "../../scripts/myproject/*.sql" directory (the ".." are relative to the directory where my docker-compose.yml file is stored). Is there a way I can run "docker-compose up" and have those files copied into a directory from which I can then copy them into the container's "/usr/work" directory?
There are 2 ways to solve this, with one being easier than the other, but both have use cases.
The easy way
You could mount the directory directly to the container through the docker-compose like this:
version: "3.2"
services:
sql-server-db:
build: ./
container_name: sql-server-db
image: microsoft/mssql-server-linux:2017-latest
ports:
- "1433:1433"
environment:
SA_PASSWORD: "Password1!"
ACCEPT_EULA: "Y"
volumes:
- ../../scripts/myproject:/path/to/dir
Note the added volumes compared to the yaml in your question. This will mount the myproject directory to /path/to/dir within the container. What this will also mean is that if the sql-server-db container writes to any of the files in /path/to/dir, then the file in myproject on the host machine will also change, since the files are mounted.
The less easy way
You could copy the files directly during the build of the image. This is a little bit harder, since the build stage of docker doesn't allow the copying of parent directories unless you add some extra arguments. What needs to happen is that you set the context of the build stage to a different directory than the current directory. The context determines which files are sent to the build stage. This is the same directory as the directory the Dockerfile resides in by default.
To take this approach, you need the following in your docker-compose.yml:
version: "3.2"
services:
sql-server-db:
build:
context: ../..
dockerfile: path/to/Dockerfile # Here you should specify the path to your Dockerfile, this is a relative path from your context
container_name: sql-server-db
image: microsoft/mssql-server-linux:2017-latest
ports:
- "1433:1433"
environment:
SA_PASSWORD: "Password1!"
ACCEPT_EULA: "Y"
So above the context is now ../.. so that you are able to copy files two directories above. You can then copy the myproject directory in your Dockerfile like this:
FROM microsoft/mssql-server-linux:latest
COPY ./scripts/myproject /myfiles
The advantage of this approach is that the files are copied instead of being mounted, so the docker container can write whatever it wants to these files, without affecting the host machine.

How do I create this using Docker

Download the repository to your local machine and unzip the directory. Enter the directory (you may rename the directory first) on command line environment, then use the following command to download the rails docker image and to build.
The repository file is been downloaded and unzipped it. What should I do
docker-compose run web rails new . --force --no-deps --database=postgresql
docker-compose build
ERROR:
Can't find a suitable configuration file in this directory or any
parent. Are you in the right directory?
Supported filenames: docker-compose.yml, docker-compose.yaml
You need to define docker-compose.yml file or docker-compose.yaml. Accordingly to documentation:
Using Compose is basically a three-step process:
1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
3. Run docker-compose up and Compose starts and runs your entire app.
A docker-compose.yml looks like this:
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
More on official website

Why docker container can't see volume on different container?

I have 2 containers that I fire up using docker-compose up.
The first I just pull from the docker hub nginx:stable
The second one I build on top of the php from the hub
dockerfile
FROM composer:1.9.3
RUN mkdir /fatfree
RUN ["composer","require","bcosca/fatfree-core","--working-dir","/fatfree"]
FROM php:7.4-fpm
COPY --from=0 /fatfree /fatfree
I also tried VOLUME /fatfree in the above file to no avail.
docker-compose.yml
version: "3.7"
services:
webserver:
image: nginx:stable
ports:
- "80:8080"
volumes:
- ./www:/www
- fatfree:/fatfree
links:
- php
php:
build:
context: .
dockerfile: dockerfile
volumes:
- ./www:/www
- "fatfree:/fatfree"
volumes:
fatfree:
If I interpreted correctly the docker documentation, my www/index.php should be able to see whatever is in /fatfree, but it doesn't. The folder itself shows up, but it appears empty.
If I run the dockerfile interactively docker container run -i -t test bash , the /fatfree folder exists and it has all the files I expect it to have.
There are plenty of stackoverflow questions asking how to achieve this, and they all seem to suggest that what I'm doing is actually ok, but it doesn't work, and I have no clue why.
Any suggestion is appreciated.
Your mapping is incorrect.
You want:
volumes:
- /fatfree:/www
The first entry /fatfree refers to the path on your host machine.
The second entry /www refers to the path in the container.
In my example, your host's /fatfree directory (and content) will be mapped to the container's /www directory.
Change as desired.

Docker / Docker-compose volume fill & share issue

I have a few questions about Docker volumes. I have installed Docker and docker-compose on a fresh host running debian stretch. I managed to get a docker-compose file running for a simple nginx/php-fpm project, both containers mounted on the directory containing the source code. I wanted to try to create a single volume that would be shared across my containers but I have a few issue, and my understanding of the official documentation is not helping.
So this is an idea of what I'm trying to achieve :
Question 1 : Trying to create a volume from a dockerfile on a directory mounted from host
docker-compose.yml :
version: '3'
services:
php:
build:
context: .
dockerfile: php.dockerfile
volumes:
- ./host-project-directory:/project
php.dockerfile :
FROM php:7-fpm
VOLUME project
from my understanding, when running docker-compose we should have a volume created on host containing all files from /project from container. And /project from container should contain all files from ./host-project-directory from host.
If I ls the content of /project on container I can see the files from host, but using docker volume list, there are no volumes created on host, why ?
Question 2 : How to populate and use this volume from another container ?
version: '3'
services:
php:
build:
context: .
dockerfile: php.dockerfile
volumes:
- named-volume:/project
web:
image: nginx
links:
- php
volumes:
- named-volume:/project
volumes:
named-volume:
This should create a volume called 'named-volume' and bind it to /project directories on both containers php and web.
Now, how to populate this volume with content from ./host-project-directory ?
I've tried adding a dockerfile like
ADD ./host-project-directory /project
But nothing changed and the volume remained empty.
I'm sorry if this is due to my lack of experience using Docker but I can't figure out how to make this simple thing work.
Thank you for your time !
For the first question, I try a simple docker file like this:
FROM php:7-fpm
COPY ./project /project
And a docker-compose like this:
version: '3'
services:
php:
build: .
volumes:
- named-volume:/project
web:
image: nginx
links:
- php
volumes:
- named-volume:/project
volumes:
named-volume:
Since you create the volume on docker-compose you don't need to create that in the Dockerfile.
Running docker volume list, I'm able to see the volume created with a local driver. Making ls inside the folder I'm also able to see the file. It's important to note, that the file present in you local directory it's not the same that the file inside the container. So if you edit the files in the host this will not change the files in container. That's because you have your volume created in another path, probably at: /var/lib/docker/volumes/...
This happens because you map the volume to the path, but you not specifies where you want the volume. To do that just make your docker-compose like this:
version: '3'
services:
php:
build: .
volumes:
- ./project:/project
web:
image: nginx
links:
- php
volumes:
- ./project:/project
Making this I'm still able to see the volume with the volume list command but without a name.
So I don't know why you are not able to see the volume in the list.
For question 2:
Doing the example above I have the files inside the container that exists in my local "project" folder.
Please check that the path to the local folder is correct.
A bind mount is not the same thing as a volume. You're defining a named volume here, but wanting the functionality of a bind mount.
Try this
version: '3'
services:
php:
build:
context: .
dockerfile: php.dockerfile
volumes:
- ./host-project-directory:/project
web:
image: nginx
links:
- php
volumes:
- ./host-project-directory:/project

Resources