I have my project architecture like this:
.
├── app/
├── context/
│ ├── Dockerfile
│ ├── .dockerignore
│ └── php.ini
├── database/
├── http/
├── composer.json
└── docker-compose.yml
and in docker-compose.yml I have the following configuration:
version: '3.8'
services:
app:
container_name: "ERP"
restart: always
build:
context: ./context
dockerfile: Dockerfile
stdin_open: true
tty: true
ports:
- '8000:80'
links:
- db_server
volumes:
- .:/usr/src/app
working_dir: /usr/src/app
db_server:
container_name: "db_server"
image: 'mysql:8.0'
ports:
- '3306:3306'
But when I set the Dockerfile content to set up the application with docker-compose up, having Dockerfile content as this:
FROM ubuntu:20.04
WORKDIR /usr/src/app
RUN cat composer.json
It says "No such file or directory composer.json". Why?
UPDATE
I managed to solve the problem based on ENTRYPOINT configuration ..
as far I understand - I'm new to docker - , the ENTRYPOINT defines an entry script running at the beginning of a container , thus , starting that script will be definitely in the run time of the container and after the initializations specified by docker-compost.yml file .. so the contents of the actual context will be available to the script to see it in the time it runs ..
Thank you all for your answers .
That's because you define the context to be "./context", so you are stuck into this folder, where composer.json isn't.
Use "." for the context. and context/Dockerfile for the dockerfile.
Then mounting '.' will mount the whole directory, and not only the ./context one.
The build process to create an image occurs before the runtime process that takes that image and runs a container. The compose file includes a build section to allow you to easily build the image before running it, but all of the other parts of the compose file define runtime configurations like volume mounts and networking.
At build time, you do not get to specify the volume sources, at most you can define a required volume target in the image with a VOLUME step. (Note if you do that, future RUN steps within the Dockerfile may have issues modifying that directory since many build tools mount an anonymous volume as you've requested, but only capture the changes to the container filesystem, not the volume filesystem).
If you need the contents of a file or directory in your image, you must perform a COPY or ADD step in the Dockerfile to copy from the build context (typically imported as . or the current directory) into the image.
Key build.context define a path to a directory containing a Dockerfile. This is a context of building image, and during build process docker doesn't have access to composer.json file (it is out of the context).
Command RUN runs command during build. If you want to run it when container is starting, you should use CMD.
FROM ubuntu:20.04
WORKDIR /usr/src/app
CMD cat composer.json
Related
I have succesfully created a volume (/code in the container) which holds django app (/web locally). When I spin up the dev environment, I want to have a folder (holding dummy data) available within the container, but I want to COPY the data, not have it as a VOLUME (so that if it's deleted in the container, it's not deleted from my local file system).
Folder structure:
...
docker-compose.yml
docker-compose.dev.yml
webms/
data/
folder/
subdir1/
file1
subdir2/
web/
Dockerfile
...
docker-compose.yml
...
services:
web:
volumes:
- ./webms/web:/code
build:
context: ./webms
...
docker-compose.dev.yml
...
services:
web:
volumes:
- ./webms/data/folder:/code/folder
...
Dockerfile
...
WORKDIR /code
ADD ./web/ .
COPY ./data/folder/ ./folder/
...
I can add data/folder as a volume succesfully, but then changes in the container reflect on my file system.
If remove the volumes directive from docker-compose.dev.yml, volumes in docker-compose.yml overwrites /code in the container and /folder is not longer available.
If I remove all the volumes directives, all the files are there, but changes to code in /web isn't live anymore (have to restart the container each time).
How do I copy data/folder into the container and have it available every time I restart the container / have a copy of the local file system copied into the volume? It may not be possible.
I have looked at similar answers on SO and they don't address this case.
If I attach myself to the container and check the files inside /app I can see my host content inside valve_controller, modify it, etc.
I can't see the files during the build process (RUN ls /app/ trows nothing). I need to verify the code and then compile it.
Are volumes mounted after the build generation?
Which option do I have that doesn't involve COPY?
version: '3.7'
services:
valve_controller:
container_name: "valve_controller"
build:
context: .
dockerfile: ./valve_controller/Dockerfile
working_dir: /app
tty: true
volumes:
- ./valve_controller:/app
Dockerfile
VOLUME /app
RUN ls /app/
Volumes are mounted only when the container is run, not during the build process. This is intentional, since the image generation should not depend on anything outside your build context (the directory where your Dockerfile is). If you need any files during image build, you should COPY them in.
Volumes are mounted the moment you run the container. Therefore you can't refer to the files during the build process.
Adding a command tag in the docker-compose with a list of commands that need to run separated with && or ; would do the trick.
It's also possible to create and initial image with the volume and import that one.
I have the following projects structure on my machine filesystem:
../
├── angular_front_end/
│ └── docker-compose.yml
│ └── Dockerfile
├── node_back_end_service/
│ └── docker-compose.yml
│ └── Dockerfile
└── php_back_end_service/
└── docker-compose.yml
└── Dockerfile
The thing is, I don't want to go through each one and do docker-compose up, it's horrible to maintain.
Is there a way to unite them all under one command somehow?
Also, can I run all of them under one container, like the back-end container in the screenshot below?
Thanks a lot!
You can create a single docker-compose.yml file at the root of this directory hierarchy that launches everything.
version: '3.8'
services:
frontend:
# Builds Dockerfile within that directory,
# can only reference files within this directory
build: angular_front_end
ports: ['3000:3000']
node:
build: node_back_end
php:
build: php_back_end
To the extent that these services require databases or other Docker resources, they all need to be duplicated in this top-level docker-compose.yml file.
In principle it's possible to reuse your existing Compose files, but there are two big issues you'll run into. You need to consistently use multiple docker-compose -f options, every time you run a docker-compose command; with your setup this will quickly become unwieldy (even with just three services). Also, all filesystem paths are interpreted relative to the first -f option's path so a declaration like build: . won't point at the right place.
I have the following project structure:
.
..
project/
docker/cli/Dockerfile
docker-compose.yml
In docker-compose.yml I have the following configuration:
cli:
build: docker/cli
And somewhere in my Dockerfile:
COPY . /app
Now the problem is that when I do docker-compose build cli docker copies the contents of docker/cli/ in /app in my image. Which makes sense because that is the relative path to my docker/cli/Dockerfile. Is there a way however to tell in my docker-compose.yml config that the path should be different (namely the root dir of my project where the actual project files are)?
You can use the context attribute of a build instruction inside the docker-compose file.
version: '2'
services:
cli:
build:
context: .
dockerfile: docker/cli/Dockerfile
This should work as desired. The context is what is sent to the docker daemon for build. You will also want a .dockerignore file in your project directory though, to ensure only what you need is sent.
I need to add some paths to my PATH in docker-compose.yml
in docker-compose.yml I have tried
app:
...
environment:
- PATH /code/project
however that just overwrites the existing PATH - whereas I want to add to the existing PATH
A docker-compose.yml does not offer you any mean to extend an environment variable which would already be set in a Docker image.
The only way I see to do such things is to have a Docker image which expects some environment variable (let's say ADDITONAL_PATH) and extends at run time its own PATH environment variable with it.
Let's take the following Dockerfile:
FROM busybox
ENV PATH /foo:/bar
CMD export PATH=$PATH:$ADDITIONAL_PATH; /bin/echo -e "ADDITIONAL_PATH is $ADDITIONAL_PATH\nPATH is $PATH"
and the following docker-compose.yml file (in the same directory as the Dockerfile):
app:
build: .
Build the image: docker-compose build
And start a container: docker-compose up, you will get the following output:
app_1 | ADDITIONAL_PATH is
app_1 | PATH is /foo:/bar:
Now change the docker-compose.yml file to:
app:
build: .
environment:
- ADDITIONAL_PATH=/code/project
And start a container: docker-compose up, you will now get the following output:
app_1 | ADDITIONAL_PATH is /code/project
app_1 | PATH is /foo:/bar:/code/project
Also note a syntax error in your docker-compose.yml file: there must be an equal sign (=) character between the name of the environment variable and its value.
environment:
- PATH=/code/project
instead of
environment:
- PATH /code/project
I know this is an old thread, but I think there are a couple of things that can be clarified.
Through docker-compose file one can only address variables from the host machine, therefore it is NOT possible to extend image's PATH from docker-compose.yml:
app:
...
environment:
- PATH=/code/project:$PATH
On the other hand, using RUN or CMD EXPORT directive will not suffice due to EXPORTED variables not persisting through images. Since every Dockerfile directive generates an intermediate image, these values will be reflected in them and not in the main image where you actually need them.
The best option would be to use build option in docker-compose.yml:
app:
build: .
and adding ENV option to a Dockerfile:
ENV PATH /path/to/bin/folder:$PATH
This is suggested in issue #684 and I would also suggest looking at an answer: docker ENV vs RUN export.
You can add your value.
To do so you need to know name or ID of the container, run it to know:
docker ps
This will print details of all running containers. Look for your container and copy its ID or name. Then run this:
docker inspect <container ID>
It will print all values of specified container. Look for ENV section and find PATH environment variable. Then copy its value, add your changes and extend it with your new values then set it again in your docker-compose.yml "environment" section.
app
environment:
- PATH=value-you-copied:new-value:new-value:etc
Note that you shouldn't remove anything from initial value of PATH, just extend it and add new value.
#Thomasleveil's answer works only for containers built directly from the docker-compose file (via the build). And you have no control over the command executed.
I needed this functionality for containers downloaded from (our) repository where this does not quite work.
I have found solution using the entrypoint and command.
Lets have some base container base and another one, java7, that is based upon it. And finaly some docker-compose using the java7 container to run some stuff.
Probably the most important file here, entrypoint.sh
$ cat base/script/entrypoint.sh
#!/bin/bash
export PATH="$PATH_ADD:$PATH"
echo "Path modified to $PATH"
exec $#
Dockerfile for base container
$ cat base/Dockerfile
FROM xxx
# copy entrypoint script that extends current PATH variable by PATH_ADD
COPY script/entrypoint.sh /usr/sbin
ENTRYPOINT ["/usr/sbin/entrypoint.sh"]
Dockerfile for java7 container
$ cat java7/Dockerfile
FROM base
# download java7
curl ... /opt/java/jdk7
ENV JAVA_HOME /opt/java/jdk7
Commands run by docker-compose
$ cat sbin/run-app1.sh
exec $JAVA_HOME/bin/java -version
$ cat sbin/run-app2.sh
exec $JAVA_HOME/bin/java -version
Docker-compose using these:
$ cat docker-compose.yml
version: '3'
services:
app1:
image: java7
command: run-app1.sh
environment:
PATH_ADD: /app/sbin
volumes:
- "./sbin:/app/sbin:cached"
app2:
image: java7
command: run-app2.sh
environment:
PATH_ADD: /app/sbin
volumes:
- "./sbin:/app/sbin:cached"
File structure
$ tree
.
├── base
│ ├── script
│ │ └── entrypoint.sh
│ └── Dockerfile
├── java7
│ └── Dockerfile
├── sbin
│ ├── run-app1.sh
│ └── run-app2.sh
└── docker-compose.yml
to add a single location to PATH in your docker-compose.yml file:
app
environment:
- PATH=/code/project:$PATH
to add multiple locations to your PATH in your docker-compose.yml file
app
environment:
- PATH=/code/project:/code/lib:/foo/bar:$PATH
to add to your PYTHONPATH
app
environment:
- PYTHONPATH=/code/project:/code/lib:/foo/bar