I am new to docker. I am trying to run a very simple script as a service along with other service using docker-compose. I have created an image using Dockerfile with following details :
FROM bash
CMD bash test_script.sh
I have redis and test_script images installed. My docker-compose.yml looks like this
version: '2'
services:
redis:
image: redis
test-script:
image: test-script
My test_script looks like this
#!/bin/bash
echo "***************** Sleeping *****************"
sleep 10
echo "***************** Woke Up ******************"
When I run "docker-compose up", Redis starts properly but I get "bash: test_script.sh: No such file or directory". Any help is appreciated.
Docker is telling you the truth. You need to COPY your test_script.sh into the container. Something like:
FROM bash
COPY test_script.sh /test_script.sh
CMD bash test_script.sh
This assumes that there is a file named test_script.sh in the same directory as your Dockerfile.
Related
I have a docker file
FROM tomcat:9.0.45-jdk8-adoptopenjdk-hotspot
RUN mkdir -p /opt/main
WORKDIR /opt/main
COPY run.sh test.sh runmain.sh /opt/main
RUN chmod +x /opt/main/run.sh && bash /opt/main/run.sh
ENTRYPOINT bash /usr/local/tomcat/bin/runmain.sh && /usr/local/tomcat/bin/catalina.sh run
An env file
ENV_MQ_DETAILS=tcp://10.222.12.12:61616
ENV_DB_HOST=10.222.12.12
runmain.sh file has the following code
#!/bin/bash
echo ${ENV_MQ_DETAILS}
echo ${ENV_DB_HOST}
when I run the docker run command
docker run --env-file .env bootstrap -d
The docker logs shows both env variable values printed.
when I use the docker-compose file
version: "3"
services:
bootstrap:
image: bootstrap
container_name: bootstrap
hostname: bootstrap
ports:
- 8080:8080
and run the command
docker-compose -f docker-compose-bootstrap.yaml --env-file .env bootstrap -d
I get two issues
While running the docker-compose-bootstrap.yaml the environment variables aren't shown in the logs hence can use them in the latest part of the code, why is it so and please help to resolve this (highest priority).
2 In both the cases (docker run and docker-compose run) , it keeps echoing the files in /opt/main/ folder
but nothing to bother though, but why ?
Please help in resolving the above issues.
I have script: docker run -it -p 4000:4000 bitgosdk/express:latest --disablessl -e test
how to put this command to dockerfile with arguments?
FROM bitgosdk/express:latest
EXPOSE 4000
???
Gone through your Dockerfile contents.
The command running inside container is:
/ # ps -ef | more
PID USER TIME COMMAND
1 root 0:00 /sbin/tini -- /usr/local/bin/node /var/bitgo-express/bin/bitgo-express --disablessl -e test
The command is so because the entrypoint set in the Dockerfile is ENTRYPOINT ["/sbin/tini", "--", "/usr/local/bin/node", "/var/bitgo-express/bin/bitgo-express"] and the arguments --disablessl -e test are the one provided while running docker run command.
The --disablessl -e test arguments can be set inside your Dockerfile using CMD:
CMD ["--disablessl", "-e","test"]
New Dockerfile:
FROM bitgosdk/express:latest
EXPOSE 4000
CMD ["--disablessl", "-e","test"]
Refer this to know the difference between entrypoint and cmd.
You don't.
This is what docker-compose is used for.
i.e. create a docker-compose.yml with contents like this:
version: "3.8"
services:
test:
image: bitgodsdk/express:latest
command: --disablessl -e test
ports:
- "4000:4000"
and then execute the following in a terminal to access the interactive terminal for the service named test.
docker-compose run test
Even if #mchawre's answer seems to directly answer OP's question "syntactically speaking" (as a Dockerfile was asked), a docker-compose.yml is definitely the way to go to make a docker run command, as custom as it might be, reproducible in a declarative way (YAML file).
Just to complement #ChrisBecke's answer, note that the writing of this YAML file can be automated. See e.g., the FOSS (under MIT license) https://github.com/magicmark/composerize
FTR, the snippet below was automatically generated from the following docker run command, using the accompanying webapp https://composerize.com/:
docker run -it -p 4000:4000 bitgosdk/express:latest
version: '3.3'
services:
express:
ports:
- '4000:4000'
image: 'bitgosdk/express:latest'
I omitted the CMD arguments --disablessl -e test on-purpose, as composerize does not seem to support these extra arguments. This may sound like a bug (and FTR a related issue is opened), but meanwhile it might just be viewed as a feature, in line of #DavidMaze's comment…
In a docker-compose file, is it possible to wait for a container to start and then print a status?
e.g. sleep 10 && echo started mysql on http://${HOST}:${PORT}
A Dockerfile has a run command, but there isn't such a thing in a compose file. How can I do this?
With docker-compose, just like with a regular docker run [...], you can specify entrypoint (cf. here) and command (cf. here).
In your case, however, what I would do is building an own Docker image based on your preferred MySQL image and COPY a simple entrypoint script into the image that does what you want, e.g.
#!/bin/sh
sleep 10
[command to run MySQL]
echo "Started MySQL on xyz"
Then specify this script as ENTRYPOINT in your Dockerfile.
The best way is just add this to the compose file:
print-status:
image: busybox
env_file: .env
command: "sh -c 'sleep 10 && echo \"http://localhost:${PORT}\"'"
depends_on:
- mysql
I'm trying to get the variable from the command line using:
sudo docker-compose -f docker-compose-fooname.yml run -e BLABLA=hello someservicename
My file looks like this:
version: '3'
services:
someservicename:
environment:
- BLABLA
image: docker.websitename.com/image-name:latest
volumes:
- /var/www/image-name
command: ["npm", "run", BLABLA]
All of this is so that I can run a script defined by what I use as BLABLA in the command line, I've tried going with official documentation.
Tried several options including:
sudo COMPOSE_OPTIONS="-e BLABLA=hello" docker-compose -f docker-compose-fooname.yml run someservicename
UPDATE:
I have to mention that as it is, I always get:
WARNING: The FAKE_SERVER_MODE variable is not set. Defaulting to a blank string.
Even when I just run the following command (be it remove, stop..):
sudo docker-compose -f docker-compose-fooname.yml stop someservicename
For the record: I'm pulling the image first, I never build it but my CI/CD tool does (gitlab), does this affect it?
I'm using docker-compose version 1.18, docker version 18.06.1-ce, Ubuntu 16.04
That docker-compose.yml syntax doesn't work the way you expect. If you write:
command: ["npm", "run", BLABLA]
A YAML parser will turn that into a list of three strings npm, run, and BLABLA, and when Docker Compose sees that list it will try to run literally that exact command, without running a shell to try to interpret anything.
If you set it to a string, Docker will run a shell over it, and that shell will expand the environment variable; try
command: "npm run $BLABLA"
That having been said, this is a little bit odd use of Docker Compose. As the services: key implies the more usual use case is to launch some set of long-running services with docker-compose up; you might npm run start or some such as a service but you wouldn't typically have a totally parametrizable block with no default.
I might make the docker-compose.yml just say
version: '3'
services:
someservicename:
image: docker.websitename.com/image-name:latest
command: ["npm", "run", "start"]
and if I did actually need to run something else, run
docker-compose run --rm someservicename npm run somethingelse
(or just use my local ./node_modules/.bin/somethingelse and not involve Docker at all)
I have the following entryfile
FROM <image-of-nodejs>
COPY docker/node/entry.sh /var/entries/entry.sh
RUN apt-get update
RUN apt-get install ant -y
CMD ["/var/entries/entry.sh"]
the image is used by a docker-compose file:
version: "3.3"
services:
my_node:
build:
context: ./
dockerfile: docker/node/Dockerfile-build-dev
volumes:
- type: bind
source: ./
target: /var/proj
and the entry.sh file is the following:
#!/bin/bash
export QNAMAKER_SUB_KEY=b13615t
If I then start the image and I enter the docker, I won't find my env variable set:
docker-compose up --force-recreate -d
docker-compose run my_node bash
root#9c081bedde65:/# echo ${QNAMAKER_SUB_KEY}
<empty>
I would prefer to set my variables throug my script in place of the ENV Dockerfile command. What's wrong?
There are a couple of things going on here.
First, docker-compose run doesn't run a command inside the container you started with docker-compose up. It starts a new container to run a one-off command. You probably want docker-compose exec.
The reason you don't see the variable when using docker-compose run is that you are overriding your CMD by providing a new command (bash) on the docker-compose run command line.
You could consider:
Using ENV statements in your Dockerfile.
Using the environment key in your docker-compose.yml
The former will embed the information into your image, while the latter would mean that the variable would be unset if you didn't explicitly set it in your docker-compose.yaml file (or using -e on the docker run command line).
You may be able to accomplish your goal using an ENTRYPOINT script and setting the value there, but that won't impact the environment visible to you when using docker exec (or docker-compose exec).