docker ubuntu container exec bash issue - docker

I pull & run an image like
docker run -d --name=lemp \
-v /Users/bappa/Desktop/server/www:/var/www/ \
-p 8080:80 \
stenote/docker-lemp:16.04
& then go to bash like
docker exec -it lemp bash
which is absolutely fine. But When I do with ubuntu:16.04 image same thing. I found response like below
Where is the problem? why the container exit? Thanks.

The reason that caused the different behavior is because of their Dockerfile CMD or ENTRYPOINT.
Once the main process (CMD or ENTRYPOINT) finishes, a docker container stops.
If you look at docker-lemp Dockerfile:
ENTRYPOINT ["/entrypoint.sh"]
Comparing to Ubuntu Dockerfile:
CMD ["bash"]
docker-lemp runs entrypoint.sh which runs further processes that remain in the foreground while Ubuntu runs bash that quits itself after completion.
If you want to keep Ubuntu in the background, a simple trick would be:
docker container run -d ubuntu:16.04 tail -f /dev/null
This replaces the default CMD bash with tail -f /dev/null so the container does not exits.

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.

What does the "." in "docker run <image> . " do?

I have 2 commands:
docker run -d -p 5000:8080 ${image_name} .
and
docker run -d -p 5000:8080 ${image_name}
The only difference between these two commands is the period at the end. What is the purpose of the period? I understand that it signifies the current directory, but what is its use in a command like this?
Arguments after the image name are passed to the image's entrypoint, so it depends on the default ENTRYPOINT of the image. Often, the entrypoint is bash, so running
docker run -d -p 5000:8080 ${image_name} .
Is like running bash ..
The fact that you are publishing ports in your docker run command makes me think that the image runs a server. Let's say the entrypoint of your image is python server.py. Then the command
docker run -d -p 5000:8080 ${image_name} .
is akin to running python server.py .
and the command
docker run -d -p 5000:8080 ${image_name}
is akin to running python server.py (note the absence of the dot).

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

docker, mariadb doesn't start at "init", based in debian:stable

i am trying write a Dockerfile like that
FROM debian:stable
RUN apt-get update
RUN apt-get install -y mariadb-server
EXPOSE 3306
CMD ["mysqld"]
I create the image with
docker build -t debian1 .
And i create the container with
docker run -d --name my_container_debian -i -t debian1
20 seconds after, docker ps -a tells that container is exited. Why? I want the container is up and mariadb running. Thanks. Sorry for the question.
mysqld alone would exit too soon.
If you look at a MySQL server Dockerfile, you will note its ENTRYPOINT is a script docker-entrypoint.sh which will exec mysqld in foreground.
exec "$#"

Resources