I have got below Dockerfile.
FROM node:boron
# Create app directory
RUN mkdir -p /usr/src/akamai
WORKDIR /usr/src/akamai
# Install app dependencies
COPY package.json /usr/src/akamai/
RUN npm install
# Bundle app source
COPY . /usr/src/akamai
#EXPOSE 8080
CMD ["node", "src/akamai-client.js", "purge", "https://www.example.com/main.css"]
Below is the command which I run from CMD after the docker image build
docker run -it "akamaiapi" //It executes the CMD command as given in above Dockerfile.
CMD ["node", "src/akamai-client.js", "purge", "https://www.example.com/main.css"] //I want these two arguments directly passed from docker command instead hard-coded in Dockerfile, so my Docker run commands could be like these:
docker run -it "akamaiapi" queue
docker run -it "akamaiapi" purge "https://www.example.com/main.css"
docker run -it "akamaiapi" purge-status "b9f80d960602b9f80d960602b9f80d960602"
You can do that through a combination of ENTRYPOINT and CMD.
The ENTRYPOINT specifies a command that will always be executed when the container starts.
The CMD specifies arguments that will be fed to the ENTRYPOINT.
So, with Dockerfile:
FROM node:boron
...
ENTRYPOINT ["node", "src/akamai-client.js"]
CMD ["purge", "https://www.example.com/main.css"]
The default behavior of a running container:
docker run -it akamaiapi
would be like command :
node src/akamai-client.js purge "https://www.example.com/main.css"
And if you do :
docker run -it akamaiapi queue
The underlying execution in the container would be like:
node src/akamai-client.js queue
Related
I have a docker file which when built and run stops. I am trying to run both client and server in one docker container. If there is any solution to use docker-compose, then that is already in place and working fine. Please advise how to keep the container up and running using docker run. Thanks!
Here is my docker file, package.json and screenshot of folder structure.
DockerFile contents:
FROM node:14.14.0-alpine
RUN apk update && apk add bash
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
WORKDIR /app
EXPOSE 3000
EXPOSE 4565
CMD ["npm","run","prebuild"]
docker build: command:
docker build -t sample .
docker run command:
docker run -d -it --name sm -v `pwd`:/app sample
Package.json:
As far as I'm concerned you can run a command line when building an image with RUN or when running a container with CMD. Is there anyway to do so when starting a docker container?
My goal is to run the gcloud datastore automatically just after typing docker start my_container_name.
If this is possible, which changes should I apply to my Dockerfile?
(I have already installed all the packages required and I can run that command after docker run --name my_container_name -i -t my_image_name but I want it to be run also when starting the container)
Docker execute RUN command when you build the image.
Docker execute ENTRYPOINT command when you start the container. CMD goes as arguments to ENTRYPOINT. Both of these can be overridden when you create a container from an image. Their purpose in Dockerfile is to provide defaults for future when you or someone else will be creating containers from this image.
Consider the example:
FROM debian:buster
RUN apt update && apt install procps
ENTRYPOINT ["/usr/bin/ps"]
CMD ["aux"]
The RUN command adds ps command to the image, ENTRYPOINT and CMD are not executed but they will be when you run the container:
# create a container named 'ps' using default CMD and ENTRYPOINT
docker run --name ps my_image
# equivalent to /usr/bin/ps aux
# start the existing container 'ps'
docker start ps
# equivalent to /usr/bin/ps aux
# override CMD
docker run my_image au
# equivalent to /usr/bin/ps au
# override both CMD and ENTRYPOINT
docker run --entrypoint=/bin/bash my_image -c 'echo "Hello, world!"'
# will print Hello, world! instead of using ps aux
# no ENTRYPOINT, only CMD
docker run --entrypoint="" my_image /bin/bash -c 'echo "Hello, world!"'
# the output is the same as above
Each time you use docker run you create a container. The used ENTRYPOINT and CMD are saved as container properties and executed each time you start the container.
Below is my Dockerfile:
FROM python:3
COPY . /Demo
WORKDIR /Demo
RUN pip install -r requirements.txt
EXPOSE 9005
CMD python ./app.py
I am using following command to run the resulting image:
docker run -it -d host:port imagename:v1
The container is automatically exiting. When I run docker ps, no running container is shown.
Instead of this line:
CMD python ./app.py
give absolute path like below:
CMD python /<path-to-script>/app.py
I need to get access to a directory from docker container to another docker container.
In the first container I am running a nodeJS application and in the tests/e2e folder there are my e2e tests and the configuration for webdriverIO.
Also it I don't need a persistend volume - like I've done it so far. I just need the test files as long as both container are running.
$ docker run
--name app_stage
--volume tests:/app/tests
--detach
app:stage
This is the Dockerfile to that application
RUN mkdir -p /app
WORKDIR /app
COPY . /app
RUN npm install
RUN npm run build
EXPOSE 3000
ENV NODE_ENV production
CMD next start
In the second container I'm running webdriverIO, which needs to get the tests and the configuration of the first container stored there in app/tests
$ docker run
--rm
--volumes-from app_stage
webdriverio wdio
But this is not working as I do not see the needed directory in the second container.
First, specify VOLUMEvariable in you dockerfile:
RUN mkdir -p /app
WORKDIR /app
COPY . /app
RUN npm install
RUN npm run build
EXPOSE 3000
ENV NODE_ENV production
VOLUME /app/tests
CMD next start
Use your first command to start app_stage container then start webdriverio container with the second command.
The documentation for the run command follows the following syntax:
docker run [OPTIONS] IMAGE[:TAG|#DIGEST] [COMMAND] [ARG...]
however I've found at times that I want to pass a flag to [COMMAND].
For example, I've been working with this image, where the [COMMAND] as specified in the Dockerfile is:
CMD ["/bin/bash", "-c", "/opt/solr/bin/solr -f"]
Is there any way to tack on flags to "/opt/solr/bin/solr -f" so that it's in the form "/opt/solr/bin/solr -f [-MY FLAGS]"?
Do I need to edit the DockerFile or is there some built in functionality for this?
There is a special directive ENTRYPOINT which fits your needs. Unlike CMD it will add additional flags at the end of your command.
For example, you can write
ENTRYPOINT ["python"]
and run it with
docker run <image_name> -c "print(1)"
Note, that this only will work if you write command in exec form (via ["...", "..."]), otherwise ENTRYPOINT will invoke shell and pass your args there, not to your script.
More generally, you can combine ENTRYPOINT and CMD
ENTRYPOINT ["ping"]
CMD ["www.google.com"]
Where CMD means default args for your ENTRYPOINT. Now you can run both of
docker run <image_name>
docker run <image_name> yandex.ru
and only CMD will be replaced.
Full reference about how ENTRYPOINT and CMD interact can be found here
The CMD directive of a Dockerfile is the command that would be run when the container starts if no command was specified in the docker run command.
The main purpose of a CMD is to provide defaults for an executing container.
In your case, just use the docker run command as follow to override the default command specified in the Dockerfile:
docker run makuk66/docker-solr /bin/bash -c "/opt/solr/bin/solr -f [your flags]"