Running docker container from debian:stretch image does not work - docker

I am trying to run a container based on the debian:stretch image, but this does not work:
docker container run --detach debian:stretch
outputs:
7976eb7074289a741a2b183634345fc8519359cba4d543c03b0a6d4e5d7e0d53
And
docker ps -a
outputs:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7976eb707428 debian:stretch "bash" 3 seconds ago Exited (0) 2 seconds ago vigorous_lumiere
Whereas it works well with the latest nginx image:
docker run --detach nginx:latest
53ed18b5d1a7c72aa92bab0ca679269514db79f31a1d3759c2e25c7fdb1e82ff
docker ps -a
outputs:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
53ed18b5d1a7 nginx:latest "nginx -g 'daemon of…" 2 seconds ago Up 2 seconds 80/tcp admiring_hawking
7976eb707428 debian:stretch "bash" About a minute ago Exited (0) About a minute ago vigorous_lumiere
Why does the container based on the debian:stretch image that I am instantiating does not work?
Does this come from the debian image?
I am running Docker version 18.09.1, build 4c52b90 on Ubuntu 16.04 LTS

Your container literally doesn’t do anything: it starts a shell, but since it’s running as a background process and doesn’t have anything on its stdin, it immediately exits.
You should read the official Docker tutorial on building and running custom images. Generally you should work by building your application into a custom image, setting up that image’s default CMD to run your application, and using docker build and docker run (or a tool like Docker Compose) to run the assembly. There’s not much point in running a plain Linux distribution container.
(Also remember that it’s extremely routine to docker rm containers, and so anything you do in an interactive shell in a container is very likely to get lost.)

Related

docker container exist but does not run with container ID

I have created a container locally. Then, I run the following command:
docker ps -a
output is:
ONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abc6f4d50931 airflow "/bin/zsh" 17 hours ago Exited (137) 21 minutes ago xenodochial_mclaren
Then I try to run the container with the following command, it create a new container with same IMAGE but different container ID instead opening the container with this image name which exist.
docker run -p 8080:8080 -it airflow /bin/zsh/
The output of docker images command is:
REPOSITORY TAG IMAGE ID CREATED SIZE
airflow airflow 63e2e36735a6 46 hours ago 704MB
airflow latest 63e2e36735a6 46 hours ago 704MB
docker/getting-started latest 083d7564d904 6 weeks ago 28MB
Why is this creating new containers?
If you run docker run ... you spin out a new cointainer from the image.
The status of your container is Exited as you can check from the docker ps -a output.
If you want to start again the same container, you can try docker start abc6f4d50931.

On OS (CoreOS) restart Docker starts multiple containers

I have my customer Docker container on a CoreOS OS host. I start my container using the docker run command as. follows:
docker run -d --restart always --net=host -p 8080:8080 --log-opt max-size=2mb my_docker_hub_accountname/imagename
And when I reboot the host I always see multiple images being run when I run the following command:
user#coreos-1 ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4ac46db4a58c 3fa7e73d544d "java -jar /code/kec…" 7 days ago Up Less than a second lucid_aryabhata
da5392c136e9 7d996239c21c "java -jar /code/kec…" 7 days ago Up Less than a second awesome_jackson
15bcc4cfe26b 7d996239c21c "java -jar /code/kec…" 4 weeks ago Up Less than a second fervent_colden
f050f55bea3c 7d996239c21c "java -jar /code/kec…" 4 weeks ago Up Less than a second condescending_poincare
31e00707ddff 7d996239c21c "java -jar /code/kec…" 4 months ago Up Less than a second awesome_curran
user#coreos-1 ~
Is there a way to control so that I only have 1 image running instead of multiple images by the Docker container?
I was able to resolve this in one way.
I searched for all container using command:
docker ps -a
I had to remove all the other containers that were previously run using
docker rm $container_id
Now I only 1 docker image running in a container. I reboot and I only see 1 container running.
hope it helps another docker user.

Ensuring Docker container will start automatically when host starts

Is there a way to start a Docker container automatically when the host starts? Before, I use the ‘—restart always’ parameter with docker run but it only works if Docker Engine is not killed.
As your comment, I think you have misunderstood about --restart always.
Once docker run --restart always container is run, the container is restarted every time the host is restarted even though you stop the container explicitly .
For example.
$ docker run --restart always --detach --name auto-start-redis redis
d04dfbd73eb9d2ba5beac41363aa5c45c0e034e08173daa6146c3c704e0cd1da
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d04dfbd73eb9 redis "docker-entrypoint..." 4 seconds ago Up 4 seconds 6379/tcp auto-start-redis
$ reboot
# After reboot-------------------------------
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d04dfbd73eb9 redis "docker-entrypoint..." About a minute ago Up 21 seconds 6379/tcp auto-start-redis
$ docker stop auto-start-redis
auto-start-redis
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d04dfbd73eb9 redis "docker-entrypoint..." 2 minutes ago Exited (0) 30 seconds ago auto-start-redis
$ reboot
# After reboot-------------------------------
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d04dfbd73eb9 redis "docker-entrypoint..." 3 minutes ago Up 12 seconds 6379/tcp auto-start-redis
However, of course, it is based upon a premise that docker-host is auto-started. docker-host in here means docker daemon process itself. Usually docker-host will auto-start by default but if it is not, you need to set it yourself.
I am not sure which OS you are using but when it comes to Ubuntu16, you can make it with systemctl command.
$ sudo systemctl enable docker
# To tell systemd to start services automatically at boot, you must enable.
If you use docker swarm, you can make global service with --mode global flag that ensures run on every node in docker swarm.
docker service create --mode global ...
If you don't use docker swarm, the best solution I think is to use init system of your system like systemd as #I.R.R said. You can make your own service file for systemd and specify the condition when the service starts like below.
[Unit]
Description=Your App
After=docker
Refer to this article by digital ocean.

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/

How to find what ports are exposed from docker image?

I have some docker image. In my example it is called sample_nginx:
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
sample_nginx latest 4b34f8307839 34 hours ago 231.6 MB
I can run it docker run sample_nginx and in the colomn "PORTS" I can see what ports are exposed from that image:
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
650d7a9fe46e sample_nginx:latest "/bin/sh -c 'nginx - 3 minutes ago Up 3 minutes 80/tcp sleepy_mclean
Is it possible to find out what ports are exposed from the image without running it?
You can use docker inspect on an image to find this (and a variety of other interesting things):
$ docker inspect redis:latest
The above command will give you a significant amount of detail. You can alternately pass a format argument to return just what you're looking for, as below.
$ docker inspect --format='{{.Config.ExposedPorts}}' redis:latest
map[6379/tcp:map[]]

Resources