How to find what ports are exposed from docker image? - docker

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[]]

Related

Can seem to bring down docker containers

I run docker ps and it shows that 5 containers that have been running for three weeks.
I then run docker-compose down but when I run docker ps again, they are all still running.
I have tried the following command but none seems to work
kill
stop
down --rmi local
rm
down
How can I stop these? I tried just bringing up my new docker-compose.yml and ignoring the olde one but I get:
ERROR: for apache Cannot create container for service apache: Conflict. The container name "/apache" is already in use by container "70c570d60e1248292f279a37634fd8b4ce7e2535d2bfa14b2c6e4089652c0152". You have to remove (or rename) that container to be able to reuse that name.
What to try to stop the old container?
You can list containers:
(base) docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c788727f0f7b postgres:14 "docker-entrypoint.s…" 7 days ago Up 7 days 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp dev-db
88e8ddcb7d4e redis "docker-entrypoint.s…" 7 days ago Up 7 days 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp beautiful_neumann
Delete container:
(base) docker rm -f c788727f0f7b # container_id
c788727f0f7b
List containers:
(base) docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
88e8ddcb7d4e redis "docker-entrypoint.s…" 7 days ago Up 7 days 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp beautiful_neumann
As you can see the container got stopped(c788727f0f7b).
You can list stopped containers using:
docker container ls -f 'status=exited'

Running docker container from debian:stretch image does not work

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.)

How can I assign a name to the intermediate container that is created when building an Docker image?

How can I assign a name to the intermediate container that is created when building an Docker image (e.g., using docker image build)?
E.g. when I run docker build -t kaldi -f kaldi2.dockerfile ., I see that the intermediate container is assigned a random name such as "musing_mahavira".
username#server:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAME
831b257dd17c c8aa0c73486e "/bin/sh …" 2 seconds ago Up 23 seconds musing_mahavira
With docker run, one can assign a name to the container with the --name option:
username#server:~$ docker run -d --name hello kaldi
username#server:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
806df7f58a87 kaldi "bash" 3 minutes ago Up 3 minutes hello
Unfortunately, naming during the Docker container building is impossible.
To specify a name for a container use --name argument when you launch the container, or rename a running container to something more descriptive.
docker rename container_name specify_name

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 create and download image from Docker container running in Docker Swarm

Imaging following scenario everyone can stack in production:
we are running Elastic search as docker containers, indexing some
data we would like to backup data every 3 months
means we need to
create docker image from running container and upload it to registry.
Haven't found any clues how to do that in documentation.
With the swarm orchestration, your individual containers/tasks inside of the service may be restarted (e.g. if you have a node failure or your application crashes). For persistent data, I'd use an external volume and backup that volume directly. If you want to do this in swarm, you can commit the containers it creates by locating the specific container and committing it with the standard commands:
$ docker service create --name test-commit busybox /bin/sh -c 'while true; do ls / >/tmp/ls.`date +%T`.log; sleep 30; done'
2vbnf5s39vs0jfc53at3ko1cg
$ docker service ls
ID NAME REPLICAS IMAGE COMMAND
2vbnf5s39vs0 test-commit 1/1 busybox /bin/sh -c while true; do ls / >/tmp/ls.`date +%T`.log; sleep 30; done
$ docker service ps test-commit
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
eu28da042s9tdwlddzk6adkan test-commit.1 busybox docker-demo Running Running 9 seconds ago
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
545e7fe6f5bd busybox:latest "/bin/sh -c 'while tr" 28 seconds ago Up 26 seconds test-commit.1.eu28da042s9tdwlddzk6adkan
$ docker diff test-commit.1.eu28da042s9tdwlddzk6adkan
C /tmp
A /tmp/ls.12:02:13.log
A /tmp/ls.12:02:43.log
$ docker commit test-commit.1.eu28da042s9tdwlddzk6adkan
test-commit:1
sha256:2255b476b307b69cf20afbc7c46fae43f05c92a70f1525aa5d745c26a406dc90
$ docker images | grep test-commit
test-commit 1 2255b476b307 9 seconds ago 1.093 MB
You can use docker commit to turn a container into an image.
But I would advise against doing that in this case. It's better to use some kind of volume for your data and back that up separately.

Resources