I'm using docker-compose for a project that has multiple services.
To run it, I simply type docker-compose up and docker ps would show a list of running services as follows:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94cc0fca9ff5 backend_app "sh entrypoint.sh /b…" 4 hours ago Up 4 hours 0.0.0.0:3000->3000/tcp backend_app_1
e770466bc28b backend_worker "sh entrypoint.sh /b…" 4 hours ago Up 4 hours 3000/tcp backend_worker_1
Till this point, I have no problem, all as expected. Now, I'm trying to run bash for the image backend_app. I tried:
docker-compose exec app bash
docker exec -it backend_app /bin/bash
docker exec -it 94cc0fca9ff5 /bin/bash
among a hundred other things, but none seem to work. What should I do to run bash when starting the image using docker-compose?
Depending on the image you're using, bash might not be installed, should work 100% using sh instead.
Related
Here is an example on my CLI:
$ docker pull hello-world
$ docker run hello-world
It shows empty when ls/ps
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
It shows up only when I use -a but it'd suggest the containers are actually not actively running.
$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96c3e42ae83a hello-world "/hello" 11 seconds ago Exited (0) 8 seconds ago jovial_rosalind
dcaed0ba308f registry "/entrypoint.sh /etc…" 42 minutes ago Created 0.0.0.0:5000->5000/tcp registry
Have I missed something?
Looks like your container exited right away. Is it meant to be interactive? (like running bash, or needing any user interaction?) if it is, you should run it like this to attach a terminal to it:
docker run -ti hello-world
If not, what does your hello program do? If it is not something that will keep running, then the container will stop whenever it exits.
Also keep in mind that, unless you pass docker run the -d/--detach flag, it will only return after the container has stopped - so if it returns right away, that means your container has already stopped.
You may want to use one of these to get a bash shell in the container to debug your problem:
docker run -ti hello-world bash
docker run --entrypoint bash -ti hello-world
To understand the difference between them, you can read the documentation on ENTRYPOINT and COMMAND.
I have installed Docker and have running some Ubuntu image with command:
sudo docker run ubuntu
I would like to create some text file on it and find it next time the same image will run. How to achieve that?
UPD.
Got problems with attaching to docker.
I have running docker
docker ps -a
aef01293fdc9 ubuntu "/bin/bash" 6 hours ago Up 6 hours priceless_ramanujan
Since it is Up mode, I suppose I don't need to execute command:
docker start priceless_ramanujan
So, I run command attach
docker attach priceless_ramanujan
And got nothing in output while command not returns.
Why I can't get to container's bash?
Simple example:
$ docker run -it ubuntu
root#4d5643e8c1a8:/# echo "test" > test.txt
root#4d5643e8c1a8:/# cat test.txt
test
root#4d5643e8c1a8:/# exit
exit
$ docker run -it ubuntu
root#cdb44750bffc:/# cat test.txt
cat: test.txt: No such file or directory
root#cdb44750bffc:/#
docker run image_name
This command creates and starts a new container based on the provided image_name. If a name is not set for the container, a random one is generated and assigned by docker. In the above example 2 containers were created based on ubuntu.
with docker ps -a we can see that modest_jennings and optimistic_leakey are the random names created:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cdb44750bffc ubuntu "/bin/bash" About a minute ago Exited (1) 4 seconds ago optimistic_leakey
4d5643e8c1a8 ubuntu "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago modest_jennings
cat test.txt failed the 2nd time because the file didn't exist. The container started from a "clean" ubuntu image.
Actually, we created test.txt inside modest_jennings only.
docker start container_name
This command starts a stopped container. So, in our case, the file is still there:
$ docker start modest_jennings
modest_jennings
$ docker attach modest_jennings
root#4d5643e8c1a8:/# cat test.txt
test
root#4d5643e8c1a8:/#
docker commit container_name image_name
This command is to create a new image, so that you can use it later and run containers based on that image. Continuing our example...
$ docker commit modest_jennings my_ubuntu
sha256:a4357f37153ac0b94e37315595f1a3b540538283adc3721df4d4e3b39bf8334f
$ docker run -it my_ubuntu
root#2e38616d532a:/# cat test.txt
test
root#2e38616d532a:/#
If you want a custom image, you can create a Dockerfile
`FROM ubuntu:16.04
ADD ./test.txt /tmp/`
after you can build it docker build -t ubuntu:custom .
and finally run your custom image docker run --name myubuntu ubuntu:custom sleep 3000
You can check your file with docker exec -it myubuntu /bin/bash and more /tmp/test.txt
I'm trying to run my docker image and overwrite the default command node ./bin/name-of-program with:
docker run name/hub:4.5.0 /bin/bash
But isn't working. The default command is being executed and finishing the container:
2e9d10532e4c name/hub:4.5.0 "node ./bin/name-of-program /bin/ba" About a minute ago Exited (1) About a minute ago insane_brown
Is it possible to access a container? I have a code inside that i losted.
If the container defines an ENTRYPOINT command, that runs before the CMD. Try:
docker run -it --entrypoint /bin/bash name/hub:4.5.0
The -it (aka -i -t) is necessary to get an interactive terminal; without that bash won't run.
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.
How to stat a docker container. I had created it using
docker run -d -P -v /Users/bsr:/usr/local/users --name test ubuntu
I do have virtual box guest addition installed, and mounting works. But, I am not sure why I can't I keep the shell running.
bsr[~/tmp/web] $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf620ff6c36a ubuntu:latest "/bin/bash" 2 hours ago Exited (0) 2 minutes ago test
8213c8d49842 nginx:latest "nginx" 3 hours ago Up About an hour 0.0.0.0:49154->80/tcp web
bsr[~/tmp/web] $ docker start test
test
bsr[~/tmp/web] $ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf620ff6c36a ubuntu:latest "/bin/bash" 2 hours ago Exited (0) 2 seconds ago test
8213c8d49842 nginx:latest "nginx" 3 hours ago Up About an hour 0.0.0.0:49154->80/tcp web
bsr[~/tmp/web] $
Edit:
it may be because the command (/bin/bash ??) finishes immediately. When I tried,
docker run -d -P -v /Users/bsr:/usr/local/users --name test5 ubuntu /bin/bash -c "while true; do echo Hello world; sleep 1; done"
I could get the terminal. But isn't there any way to just start a container and get to the terminal ??
If you want to run an interactive process, you should use the -i (keep stdin open in case you detach) and -t (allocate a pseudo-tty) flags:
docker run -it ubuntu
You can look at the docs for more information on those flags and their usage.
You can start by using simple command.
docker run "CONTAINER_ID"