'How to create a file using touch in Dockerfile or docker-compose?
I want to create an empty DB file using touch by Dockerfile or docker-compose.yml and volume it. Actually, I'm able to create it manually within the container as follows:
docker exec -it <container-name> bash
# touch /app/model/modbus.db
Whereas, when I use the following procedure it throws exited with code 0 and stops:
version: '3'
services:
collector:
build: .
image: collector:2.0.0
command: bash -c "touch /app/model/modbus.db" # Note
# command: bash /app/bashes/create_an_empty_db.sh
volumes:
- "./model/modbus.db:/app/model/modbus.db:rw"
tty: true
As well as this, I tried that via Dockerfile without any success either:
FROM python:3.6-slim
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /
ADD . /app
RUN touch /app/model/modbus.db # Note
CMD python app
[NOTE]:
Also without the command: bash -c "touch /app/model/modbus.db" in the docker-compose.yml which was the cause of exited with code 0; a directory will be created named modbus.db instead of a file due to the following section:
volumes:
- "./model/modbus.db:/app/model/modbus.db:rw"
TL;DR:
How to send a new file from the container to the host which does not exist in the host? (In other words, it is done inside of the container, not in the host)
Solution 1:[1]
I am not sure about the docker-compose.yml but the dockerfile that you have seems to be working for me.
The Dockerfile looks like this,
FROM python:3.6-slim
RUN mkdir /app
WORKDIR /
RUN touch /app/modbus.db
Build the dockerfile,
docker build -t test .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM python:3.6-slim
---> 903e8a0f0681
Step 2/4 : RUN mkdir /app
---> Using cache
---> c039967bf463
Step 3/4 : WORKDIR /
---> Using cache
---> c8c81ac01f50
Step 4/4 : RUN touch /app/modbus.db
---> Using cache
---> 785916fe4cea
Successfully built 785916fe4cea
Successfully tagged test:latest
Build the container,
docker run -dit test
52cde500cda015f170140ae9e7174a0367b29265a49a3742173946b686179fb3
I ssh'ed into the container and was able to find the file.
docker exec -it 52cde500cda015f170140ae9e7174a0367b29265a49a3742173946b686179fb3 /bin/bash
root@52cde500cda0:/# ls
app bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@52cde500cda0:/# cd app
root@52cde500cda0:/app# ls
modbus.db
Solution 2:[2]
Put the following in your docker-compose.yml
volumes:
- "./model:/app/model"
This will create a /app/model folder inside of your container. Its contents (which you will create inside the container) will be available on ./model on your host.
If you put the touch command in the CMD of your Dockerfile, that file will be created after starting the container when the volume is also initialized. So the following Dockerfile should work:
FROM python:3.6-slim
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /
ADD . /app
CMD touch /app/model/modbus.db && python app
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
