Cannot start simple docker container with "docker start" command from created image - docker

I have simple commands for making and starting container (create.sh):
docker build -t foo .
docker rm bar
docker create --name=bar foo && \
docker start bar && \
docker exec bar sh /bin/echo Test!!!
Dockerfile:
#/bin/bash
FROM centos:7
RUN yum update -y
But it cannot be started:
$ bash create.sh
Sending build context to Docker daemon 4.608 kB
Step 1 : FROM centos:7
---> 980e0e4c79ec
Step 2 : RUN yum update -y
---> Using cache
---> 80b94205920c
Successfully built 80b94205920c
bar
a1db507225ca7479bdcc3bb3d4e3a86339827f4bf0e9365f507978b11d99df19
bar
Error response from daemon: Container bar is not running
The container has been created but it hasn't started.

Your Dockerfile has no ENTRYPOINT or CMD, so it finishes immediately, that is normal.
Check the docs about ENTRYPOINT

The Dockerfile for CentOS specifies the shell as the CMD to run when you start a container:
CMD ["/bin/bash"]
Docker containers exit when the process they started with finishes, so what's happening is when you start your container, it runs bash and then ends, because there's no more input to the shell.
Using your Docker image, this does what you expect - the echo command overrides the bash command in the Dockerfile:
> docker run temp echo 'Test'
Test
If you want to keep a container running in the background, then you need the process inside the container to keep running. You can specify a command when you create the container:
>docker create temp sleep infinity
a34a4528b3cfbb7a36fb429d32510c5576831adeb899c07e4596a6b9731c945b
> docker start a34
a34
> docker exec a34 echo 'Test'
Test

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.

File created in interactive session within container dissapears after exiting container (container running in background)

Objective
Essentially what I am trying to accomplish is to install a bunch of software but store the commands run in the Dockerfile in the end. I was planning on recording the installation process by running the "script" function to record the commands run on the command line. I would like to know why it doesn't work but if there is a better way of doing it, I am all ears!
The issue
I'm sure there is a simple answer to this but I can't seem to figure it out. When I create a dummy file within my docker container it dissapears when I exit the container even though the container is running in the background.
Attempt
This is my Dockerfile
##Filename = Dockerfile
FROM centos:7
WORKDIR /dummy_folder
CMD ["echo", "hello world"]
I build the image and run it in the background.
docker build -t my_test_image:v1.0 .
docker run -d e9e949b5d85a tail -f /dev/null
Now I can see my container running in the background
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f4da7a1b74d e9e949b5d85a "tail -f /dev/null" 14 minutes ago Up 14 minutes trusting_poincare
If I create an interactive session and just dump a file in /dummy_folder and exit. When I create a new interactive session /dummy_folder is empty again.
docker run -it e9e949b5d85a /bin/bash
echo "dummy" > dummy
exit
docker run -it e9e949b5d85a /bin/bash
ls -alh /dummy_folder
P.S tail -f /dev/null is just a trick I use to keep the container running in the background as just running it with the flag -d doesn't work for centos containers apparently.
I am running Docker version 19.03.8, build afacb8b
Thanks
Sabri
You're creating a new container every time your run docker run ... . What I think you're trying to do is run a shell on the same container you started with docker run -d e9e949b5d85a tail -f /dev/null. If so, the Docker command your'e looking for is exec
Start an interactive session using the container ID (not the image ID) and do your stuff
docker exec -it 6f4da7a1b74d /bin/bash
$ echo "dummy" > dummy
$ exit
And then check the contents again with exec
docker exec 6f4da7a1b74d ls -alh /dummy_folder

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.

How to create and run a Docker container based on a minimal image?

I would like to build a container based on an image defined in a Dockerfile as follows:
FROM alpine:3.7
RUN touch test.txt
RUN echo "Hello Worl" > ./test.txt
After issuing docker image build -t test_alp ., I can find the image in the list of available images:
me#ub:test_dockerfile$ docker image build -t test_alp .
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM alpine:3.7
---> 6d1ef012b567
Step 2/3 : RUN touch test.txt
---> Running in 70a9056b0421
Removing intermediate container 70a9056b0421
---> 87b4ee2e9839
Step 3/3 : RUN echo "Hello Worl" > ./test.txt
---> Running in b48af2120d80
Removing intermediate container b48af2120d80
---> 7326560e8a47
Successfully built 7326560e8a47
Successfully tagged test_alp:latest
me#ub:test_dockerfile$ docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
test_alp latest 7326560e8a47 46 seconds ago 4.21MB
Then I create a container and start it.
me#ub:test_dockerfile$ docker create --name cont_alp 7326560e8a47
0f857d36e5b594afab7133513faf1dc3c62269a50af0e28e15564852fc379b10
(In between question: could I also create the container using docker create test_alp?)
me#ub:test_dockerfile$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f857d36e5b5 7326560e8a47 "/bin/sh" 11 seconds ago Created cont_alp
me#ub:test_dockerfile$ docker start cont_alp
cont_alp
me#ub:test_dockerfile$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f857d36e5b5 7326560e8a47 "/bin/sh" 24 seconds ago Exited (0) 4 seconds ago cont_alp
As you see, the container immediately returns to the Exited state. Why is that?
alpine:3.7 (as all other versions...) runs sh as a default command.
You are not changing the default container command in your build. When your run your container it will launch that command. Since there is no tty attached to ssh and you did not ask for an interactive run, sh has nothing to process and exists.
2 ways to get into your container for tests:
run the container from scratch interactively:
docker run -it --rm --name cont_alp test_alp:latest
run the container in the background with a long lasting command and execute sh to connect to it:
docker run -d --rm --name cont_alp test_alp:latest sh -c "while true; do sleep 2000; done"
docker exec -it cont_alp sh
If you want to launch your containers based on the above image by default with the long lasting command, you can add it to your Dockerfile. (Note that I also reduced the number of needed layers by running touch and echo in a single run. But do you need touch anyway...)
FROM alpine:3.7
RUN touch test.txt && echo "Hello World" > test.txt
CMD ["/bin/sh", "-c", "while true; do sleep 2000; done"]
Because your RUN command (echo) exited after execution! Generally docker containers run long execution daemon commands like server side applications...
You can do this by putting the commands you want to execute into a script with a loop, and setting the script to be the command Docker runs when it starts a container:
CMD /script.sh

Simple Dockerfile no work

This is my Dockerfile:
FROM debian:stable
MAINTAINER xxxx <xxxx#xxxx.com>
RUN apt-get update && apt-get upgrade -y
CMD ["/bin/bash"]
Then, I run in the directory of Dockerfile:
docker build -t testimage .
Finally:
docker run -d testimage
The container no start:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c4fe93e2e225 test "/bin/bash" 17 minutes ago Exited (0) 9 minutes ago gloomy_ritchie
You are trying to run a detached container (-d), but you are also attempting to launch an interactive shell (/bin/bash). Because bash requires an interactive terminal, it exits immediately, hence your container exits.
If you just want to run an interactive shell in your container, get rid of the -d:
docker run -it testimage
The -it flags set up the container for interactive use; see the man page for docker-run for more information.
A detached container is most often used to run a persistent service (like a database, or a web server), although you can run anything as long as it doesn't expect to be attached to an active terminal.

Resources