How do I run a docker container based on its name from `docker ps -a`? - docker

By default docker leaves a bunch of dead volumes around.
$ docker ps -a
61e99f563834 jolly_swanson user/name:version "command" 52 seconds ago Exited (130) 51 seconds ago
Why doesn't docker run jolly_swanson restart that container with its old data? I feel like I must be missing something from the documentation.

You seem to be confusing images and containers. Docker leaves dead containers around, not images (and not volumes either).
docker run creates a new container from an existing image. So docker run jolly_swanson does not work because jolly_swanson is the name of a container, not an image.
To start an existing container, use start, e.g. docker start jolly_swanson.

Related

docker container not removing from docker

I am trying to remove container but when i run docker-compose rm ,runs fine but when i run docker ps then again it shows container:
root#datafinance:/tmp# docker-compose rm
Going to remove tmp_zookeeper_1_31dd890a1cbf
Are you sure? [yN] y
Removing tmp_zookeeper_1_31dd890a1cbf ... done
root#datafinance:/tmp# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03b08e4ef0b3 confluentinc/cp-zookeeper:latest "/etc/confluent/dock…" 14 hours ago Up 14 hours docker_c_zookeeper_1_7c953dce7d69
Use docker-compose ps, it will show the container which only launched by docker-compose up. If it shows there is no container, then this means this container was not launched by this docker-compose.yaml.
And Error starting userland proxy: listen tcp 0.0.0.0:32181: bind: address already in use' means the port 32181 is occupied either by other docker container or other process. You could use docker rm -f $(docker ps -qa) to delete all containers or more you can use netstat -oanltp | grep 32181 to find which process really occupy 32181.
Finally, if for any reason you did not able to delete container as you said, you can just use service docker restart or systemctl restart docker to make all container down. Then repeat above docker rm xxx.
With above steps, you can use docker compose up -d to use your service now.
try this :
docker rm -f 03b08e4ef0b3
DANGER
you may also try this, but be aware that will delete everything (Containers, Images, Networks, ....)
docker system prune -a -f
when all not helped your last resort is to restart Docker daemon
service docker restart
and then repeat the steps...
I think what you are looking for is :
docker-compose down
which removes the containers after stopping them according to this.
According to this, docker-compose rm removes the "stopped" containers. If your container(s) are running, I think it won't remove to prevent accidents.

Starting docker container that is more than 90 days old

I had created a docker container image for Business Central 2 months ago. Now when I try to start the container it starts with an unhealthy status and Business Central client doesn't work.
docker start <container-id>
I checked the logs that told me that I am trying to run a container which is more than 90 days old.
Initializing...
Restarting Container
PublicDnsName unchanged
Hostname is MyBCDev
PublicDnsName is MyBCDev
You are trying to run a container which is more than 90 days old.
Microsoft recommends that you always run the latest version of our containers.
Set the environment variable ACCEPT_OUTDATED to 'Y' if you want to run this container anyway.
at , C:\Run\navstart.ps1: line 54
at , C:\Run\start.ps1: line 121
at , : line 1
I googled the issue and all I can find is to use the docker run command with accept outdated parameter, but that creates a new container. Whereas I want to start the existing container.
docker run --env accept_eula=Y --memory 4G microsoft/dynamics-nav
How can I start an existing docker container that is more than 90 days old?
Update
I did the docker commit using the existing container and repository:tag. But when I ran the container (docker run) using the new image it got stuck somewhere in the middle
Try to set ACCEPT_OUTDATED=Y and start the container. If it doesn't worked then try this hack.
Make use of docker commit command.
docker commit container-id myimage:v1
This will create new docker image out of that stopped container with all the data and config in it.
Run a new docker container out of that image.
This new docker container will be almost same as that of stopped docker container that was 90 days old.
Hope this helps.
You should set ACCEPT_OUTDATED=Y
docker run -e ACCEPT_EULA=Y -e ACCEPT_OUTDATED=Y --memory 4G microsoft/dynamics-nav

How to kill container generated by docker-compose?

(sorry using the term "kill" with quotes is not about docker-compose kill, is about "UNIX ps kill" after what the process really go out of the "UNIX ps list")
Usual docker run can be "killed" by usual docker stop, because after stop I not see the container at docker ps -a... If it is correct, there are a semantic bug with docker-compose because I can't "kill" the containers, they stay at docker ps.
After my simple docker-compose up (without &) I do ^C and the containers stay there at docker ps -a... Impossible to kill by docker compose stop.
NOTE: when I use ordinary docker run and after it docker stop there are nothing at docker ps -a, so I can say "I killed it".
Usual docker run can be "killed" by usual docker stop, because after stop I not see the container at docker ps.
No. docker stop just stops a running container, it doesn' t remove the container. This happens only in case you've used docker run --rm .... This --rm option means that when the container is stopped, it will be removed/deleted.
Docker
docker run ... creates and runs a container
docker stop ... stops a running container
docker start ... starts a stopped container
docker rm ... removes a stopped container
Docker Compose
docker-compose up creates and runs a collection of containers
docker-compose stop stops the containers
docker-compose start starts the containers
docker-compose down stops and removes the containers
Be careful...
As it discussed in the comments section, by using docker-compose down other things can also take place regarding volumes, networks. Keep in mind that you might lose data (if your container is a database for example) and make sure you have saved them or you are somehow able to create them again.
Check out running containers:
docker ps
Example output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e86521d81a96 app_php "docker-php-entrypoi…" 2 hours ago Up About an hour 0.0.0.0:8080->80/tcp app_php_1
7a30681b6255 mysql:5.6 "docker-entrypoint.s…" 3 hours ago Up About an hour 0.0.0.0:3306->3306/tcp app_db_1
21aa3eef5f42 phpmyadmin/phpmyadmin "/run.sh supervisord…" 4 hours ago Up About an hour 9000/tcp, 0.0.0.0:8081->80/tcp app_phpmyadmin_1
9afc52b3f82f mailhog/mailhog "MailHog" 4 hours ago Up About an hour 1025/tcp, 0.0.0.0:8082->8025/tcp app_mailhog_1
then stop one by the container id:
docker kill part_of_the_id/name
For instance:
docker kill e86 or docker kill app_php_1
Docker-compose is just a script to help you manage one or multiple containers running in a group and is absolutely not required to manage your containers.
To remove the container completely you have to remove the container docker rm container_id_or_name
To stop all running containers:
docker stop $(docker ps -q)
You can use docker rm <container-name> to do that. This command will stop and remove service container. Anonymous volumes attached to the container will not be removed.

moving docker-compose images between hosts

based on Moving docker-compose containersets
I have loaded the images :
$ docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
br/irc latest 3203cf074c6b 23 hours ago 377MB
openjdk 8u131-jdk-alpine a2a00e606b82 5 days ago 101MB
nginx 1.13.3-alpine ba60b24dbad5 4 months ago 15.5MB
but now i want to run them, as they would run with docker-compose, but i cannot find any example.
here is the docker-compose.yml
version: '3'
services:
irc:
build: irc
hostname: irc
image: br/irc:latest
command: |
-Djava.net.preferIPv4Stack=true
-Djava.net.preferIPv4Addresses
run-app
volumes:
- ./br/assets/br.properties:/opt/br/src/java/br.properties
nginx:
hostname: nginx
image: nginx:1.13.3-alpine
ports:
- "80:80"
links:
- irc:irc
volumes:
- ./nginx/assets/default.conf:/etc/nginx/conf.d/default.conf
so how can i run the container, and attach to it, to see if its running, and in what order do i run these three images. Just started with docker, so not sure of the typical workflow ( build, run, attach etc )
so even though i do have docker-compose yml file, but since i have the build images from another host, can i possibly run docker commands to run and execute the images ? making sure that the local images are being referenced, and not the ones from docker registry.
Thanks #tgogos, this does give me a general overview, but specifically i was looking for:
$ docker run -dit openjdk:8u131-jdk-alpine
then:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cc6ceb8a82f8 openjdk:8u131-jdk-alpine "/bin/sh" 52 seconds ago Up 51 seconds vibrant_hodgkin
shows its running
2nd:
$ docker run -dit nginx:1.13.3-alpine
3437cf295f1c7f1c27bc27e46fd46f5649eda460fc839d2d6a2a1367f190cedc
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3437cf295f1c nginx:1.13.3-alpine "nginx -g 'daemon ..." 20 seconds ago Up 19 seconds 80/tcp vigilant_kare
cc6ceb8a82f8 openjdk:8u131-jdk-alpine "/bin/sh" 2 minutes ago Up 2 minutes vibrant_hodgkin
then: finally:
[ec2-user#ip-10-193-206-13 DOCKERLOCAL]$ docker run -dit br/irc
9f72d331beb8dc8ccccee3ff56156202eb548d0fb70c5b5b28629ccee6332bb0
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f72d331beb8 br/irc "/opt/irc/grailsw" 8 seconds ago Up 7 seconds 8080/tcp cocky_fermi
3437cf295f1c nginx:1.13.3-alpine "nginx -g 'daemon ..." 56 seconds ago Up 55 seconds 80/tcp vigilant_kare
cc6ceb8a82f8 openjdk:8u131-jdk-alpine "/bin/sh" 2 minutes ago Up 2 minutes vibrant_hodgkin
All three UP !!!!
Your question is about docker-compose but you also ask things about run, build, attach which makes me think I should try to help you with some basic information (which wasn't so easy for me to cope with a couple of months ago :-)
images
Images are somehow the base from which containers are created. Docker pulls images from http://hub.docker.com and stores them in your host to be used every time you create a new container. Changes in the container do not affect the base image.
To pull images from docker hub, use docker pull .... To build your own images start reading about Dockerfiles. A simple Dockerfile (in an abstract way) would look like this:
FROM ubuntu # base image
ADD my_super_web_app_files # adds files for your app
CMD run_my_app.sh # starts serving requests
To create the above image to your host, you use docker build ... and this is a very good way to build your images, because you know the steps taken to be created.
If this procedure takes long, you might consider later to store the image in a docker registry like http://hub.docker.com, so that you can pull it from any other machine easily. I had to do this, when dealing with ffmpeg on a Raspberry Pi (the compilation took hours, I needed to pull the already created image, not build it from scratch again in every Raspberry).
containers
Containers are based on images, you can have many different containers from the same image on the same host. docker run [image] creates a new container based on that image and starts it. Many people here start thinking containers are like mini-VMs. They are not!
Consider a container as a process. Every container has a CMD and when started, executes it. If this command finishes, or fails, the container stops, exits. A good example for this is nginx: go check the official Dockerfile, the command is:
CMD ["nginx"]
If you want to see the logs from the CMD, you can docker attach ... to your container. You can also docker stop ... a running container or docker start ... an already stopped one. You can "get inside" to type commands by:
docker exec -it [container_name] /bin/bash
This opens a new tty for you to type commands, while the CMD continues to run.
To read more about the above topics (I've only scratched the surface) I suggest you also read:
Is it possible to start a shell session in a running container (without ssh)
Docker - Enter Running Container with new TTY
How do you attach and detach from Docker's process?
Why docker container exits immediately
~jpetazzo: If you run SSHD in your Docker containers, you're doing it wrong!
docker-compose
After you feel comfortable with these, docker-compose will be your handy tool which will help you manipulate many containers with single line commands. For example:
docker compose up
Builds, (re)creates, starts, and attaches to containers for a service.
Unless they are already running, this command also starts any linked services.
The docker-compose up command aggregates the output of each container (essentially running docker-compose logs -f). When the command exits, all containers are stopped. Running docker-compose up -d starts the containers in the background and leaves them running
To run your docker-compose file you would have to execute:
docker-compose up -d
Then to see if your containers are running you would have to run:
docker ps
This command will display all the running containers
Then you could run the exec command which will allow you to enter inside a running container:
docker-compose exec irc
More about docker-compose up here: https://docs.docker.com/compose/reference/up/

Is there a difference between "docker ps" and "docker container ls"?

The documentation of docker ps and docker container ls both says "List containers", but does not mention the other command. Is there a difference between those two commands?
The output looks exactly the same:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bbe3d7158eaa flaskmysqldockerized_web "python app.py" 5 hours ago Up 18 seconds 0.0.0.0:8082->5000/tcp flaskmysqldockerized_web_1
4f7d3f0763ad mysql "docker-entrypoint..." 6 hours ago Up 18 seconds 0.0.0.0:3307->3306/tcp flaskmysqldockerized_db_1
There is no difference between docker ps and docker container ls. The new command structure (docker container <subcommand>) was added in Docker 1.13 to provider a more structured user experience when using the command line.
To my knowledge, there has not yet been any official announcement to drop support for the old-style commands (like docker ps and others), although it might be reasonable to assume this might happen at some point in the future.
This is described in a blog post accompanying the release of Docker 1.13:
Docker has grown many features over the past couple years and the Docker CLI now has a lot of commands (40 at the time of writing). Some, like build or run are used a lot, some are more obscure, like pause or history. The many top-level commands clutters help pages and makes tab-completion harder.
In Docker 1.13, we regrouped every command to sit under the logical object it’s interacting with. For example list and startof containers are now subcommands of docker container and history is a subcommand of docker image.
docker container list
docker container start
docker image history
These changes let us clean up the Docker CLI syntax, improve help text and make Docker simpler to use. The old command syntax is still supported, but we encourage everybody to adopt the new syntax.
docker ps is shorthand that stands for "docker process status", whilst docker container ls is shorthand for the more verbose docker container list.
As the accepted answer explains, there is no difference in how they work, and docker container ls is the 'newer' command, so you should probably prefer it.
Both commands actually only show running containers by default, which makes the first one (docker ps) a little more confusing as that command on its own isn't really showing 'process status'. To see the status of all containers, add the -a option for 'all' (or use --all), e.g.
docker container ls -a
older
docker ps -a or docker container ps -a

Resources