Exclude a command in docker - docker

I have the following ENTRYPOINT in my Dockerfile:
ENTRYPOINT["/setup.sh"]
I need to run a container without running setup.sh and run install.sh.
How do I do it?

Just change your docker entrypoint when executing docker run :
docker run -d --rm --entrypoint WHATEVER_COMMAND_YOU_WANT image_name
It will replace your dockerfile' entrypoint.

Related

Run a command line when starting a docker container

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.

Ubuntu docker image is not running in the detached mode

I created one image using below Dockerfile
FROM ubuntu:latest
RUN mkdir -p /app
COPY . /app
CMD python /app/app.py
I am using this command to build the image:
DOCKER_BUILDKIT=1 docker build -t my-first-ubuntu-image .
And this one to run the container
Command for running the container : docker container run -d my-first-ubuntu-image
And when I run docker ps -a*, its status is showing Exited.
What is going on? I cannot understand because when I did the same for a nginx image, it is in running state.
There is a trick to prevent the container from dying:
docker run -d my_image -d tail /dev/null -f
The command tail /dev/null -f keeps the container busy forever.

Docker Container is not running

Please help. When I want to go into a container is says
Error response from daemon: Container 90599013c666d332ff6560ccde5053d9127e72042ecc3887550aef90fa1d1eac is not running
My DockerFile:
FROM ubuntu:16.04
MAINTAINER Anton Lapitski <a.lapitski#godeltech.com>
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ADD ./ /usr/src/app
EXPOSE 80
ENTRYPOINT ["/bin/sh", "-c", "/usr/src/app/entry.sh"]
Starting script - start.sh:
sudo docker build -t starter .
sudo docker run -t -v mounted-directory:/usr/src/app/mounted-directory -p 80:80 starter
entry.sh script:
echo "Hello World"
ls -l
pwd
if mountpoint -q /mounted-directory
then
echo "mounted"
else
echo "not mounted"
fi
sudo docker ps -a gives:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90599013c666 starter "/bin/sh -c /usr/src…" 18 minutes ago Exited (0) 18 minutes ago thirsty_wiles
And mosе important:
sudo docker exec -it 90599013c666 bash
Error response from daemon: Container 90599013c666d332ff6560ccde5053d9127e72042ecc3887550aef90fa1d1eac is not running
Please could you tell what I am doing wrong?
P.S adding -d flag when running not helped.
Once the ENTRYPOINT completes (in any form), the container exits.
Once the container exits, you can't docker exec into it.
If you want to get a shell on the image you just built to poke around in it, you can
sudo docker run --rm -it --entrypoint /bin/sh starter
To make this slightly easier to run, you might change ENTRYPOINT to CMD in your Dockerfile. (Docker will run the ENTRYPOINT passing the CMD as command-line arguments; or if there is no entrypoint just run the CMD.)
...
RUN chmod +x ./app.sh
CMD ["./app.sh"]
Having done that, you can more easily override the command
sudo docker run --rm -it starter /bin/sh
You can try
docker start container_id and then docker exec -ti container_id bash for a stopped container.
You cannot execute the container, because your ENTRYPOINT script has been finished, and the container stopped. Try this:
Remove the ENTRYPOINT from your Dockerfile
Rebuild the image
run it with sudo docker run -it -v mounted-directory:/usr/src/app/mounted-directory -p 80:80 starter sh
The key is the i flag and the sh at the end of the command.
I tried these two commands and it works:
sudo docker start <container_id>
docker exec -it <containerName> /bin/bash

can't list file in docker in docker (dind)

I face this strange issue and can't explain why.
$ docker run -d --name dind --privileged --net=host -v `pwd`:/app -w /app docker:stable-dind
fe66d6e7e5effcf15e439a332a2368fddab810e9bc8ac3445392c8e56b0aa38a
$ docker exec dind ls
Dockerfile
$ docker exec dind docker run -v `pwd`:/app -w /app alpine ls
$ docker exec dind docker build -t demo .
Sending build context to Docker daemon 521.7kB
Step 1/24 : FROM alpine
So why I can't see my files in docker container which running in docker?
Why it can read the file Dockerfile with command docker build, but not docker run?
This is because pwd in your code will be parsed in your host machine, not in the container, so the container which in the container get the current directory of host machine, not current directory of container machine, you can prove it by change following command from:
$ docker exec dind docker run -v `pwd`:/app -w /app alpine ls
to
$ docker exec dind docker run -v /app:/app -w /app alpine ls
Then, you will see your Dockerfile output. FYI.

How can I write the docker -it option in Dockerfile?

I want create a Dockerfile for CMD line:
docker run --rm -it -v $(pwd):/data -p 8080:80 klokantech/openmaptiles-server
How I do -it in Dockerfile?
You can't control the "interactive" (-i) and "tty" (-t) runtime options from the Dockerfile at build time. You'll need to include that in the documentation for how to use your image.
You can pass a string instead of an array in CMD Dockerfile instruction.
So you can type:
CMD "docker run --rm -it -v $(pwd):/data -p 8080:80 klokantech/openmaptiles-server"
Otherwise "-it" should be an element of CMD array.

Resources