I'm trying to run this docker example. Yet it appears that I can't start my docker container.
sudo docker stop aff28c5dab3f
sudo docker start aff28c5dab3f
sudo docker ps
ID IMAGE COMMAND CREATED STATUS PORTS
sudo docker ps -a
ID IMAGE COMMAND CREATED STATUS PORTS
aff28c5dab3f shykes/pybuilder:latest /usr/local/bin/build 26 minutes ago Exit 0
52200b5c58a6 shykes/pybuilder:latest /usr/local/bin/build 10 hours ago Exit 0
b59e84340a7c ubuntu:12.04 echo hello 11 hours ago Exit 0
5c1bd5bc53d6 ubuntu:12.04 echo hello 12 hours ago Exit 0
and when I try to run
sudo docker attach aff28c5dab36, I see Impossible to attach to a stopped container, start it first
I forgot to attach to the container, and hence was getting the error. I ran sudo docker attach aff28c5dab36 and now everything works fine.
It's possible that when you run docker attch command, the buildapp command inside the container is already finished and the container is stopped automatically. If you run docker attach immediately after run the buildapp command, you'll see its output.
Related
I am new to docker.
My first tried eaxmple is hello-world, CLI I used is "docker run hello-world". When I tried to remove the image, it says "one container is using its reference." So I found/delete the all containers that created by hello-world image.
When I execute "docker run hello-world", it will create a container and print texts. But when I do "docker start [container name/id]", it doesn't print the expected texts.
docker:~$ sudo docker container ls --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c792389afa8 hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago gifted_khorana
docker:~$ sudo docker start 4c792389afa8
4c792389afa8
Should I do "docker run image" to create/start container every time? Or Just do "run images" once, and use "start container" to start it?
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.
docker ps or docker container ls returns an overview of all running containers. The meaning of all columns is clear to me, except one. What does the column 'COMMAND' mean?
This is the command which is passed to the container.
$ docker run -d busybox top
$docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3eca7c034b21 busybox "top" 6 seconds ago Up 5 seconds recursing_dirac
If you check above, top is the command which has been passed to the busybox container and that's what it's showing in the docker ps -a.
It's the command passed to docker run <image> [command].
$ docker run -d ubuntu sleep 60
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
f0c9cd92a938 ubuntu "sleep 60" 3 seconds ago Up 1 second
If no command was specified there then it's the CMD from the Dockerfile. In ubuntu's case that would be CMD ["/bin/bash"]:
$ docker run -di ubuntu
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
9cd752ee86f4 ubuntu "/bin/bash" 4 seconds ago Up 2 seconds
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
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"