When should I use Docker's container name? - docker

When I ran docker ps -a, I got
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3be2faeb751 centos:latest touch /var/log/test 2 minutes ago Exited (1) 2 minutes ago insane_kirch6
What is the name, insane_kirch6, for?

You can name your own containers with --name when you use docker run. If you do not provide a name, Docker will generate a random one like the one you have.
Check their documentation for naming at Legacy container links, The importance of naming

And even more importantly, you can run named containers again later with start:
docker start --interactive named-containter

Not only for visibility, but it also can be used as container_id,
in docker commands like start, stop, exec, rm, ...
When you want to run a command in an existing container (running or exited), you will identify the container either by name or container_id.
Examples:
Create a container named qqqq and start a process "sleep" 1 minute, and then exit.
$ docker run --name qqqq ubuntu sleep 60
Run another command in the container qqqq:
$ docker exec qqqq ps -aef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 04:21 ? 00:00:00 sleep 60
root 11 0 3 04:21 ? 00:00:00 ps -aef
Delete the container qqqq:
$ docker rm qqqq
qqqq

Related

docker, container is not running

docker ps shows instances, but when I try to log in to the instance, it says it's not running?
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eaa62ff2df11 monitor_kibana "/usr/local/bin/dumb…" 4 months ago Up 9 days kibana
613dc901f2e1 monitor_elasticsearch-search "/usr/local/bin/dock…" 4 months ago Up 9 days elasticsearch-search
$ docker exec -it eaa62 bash
Error response from daemon: Container eaa62ff2df11547744c5f7cf82cad16bf576820d2a209c4f19f173cca68f5511 is not running
$
Could it be that the container only runs for a very short time? If you use the -a flag in your statement to get only active containers, like so:
docker ps -a
Does it still show up? It could be that it runs and just uses something like ECHO. In that case, because the program run succesfully the container is immediately terminated.
Is this an official image? If so, try to run the container without the -d (for deamon) flag. This should output the run information to terminal and give you some information on what is going on.
sudo docker exec -it eaa62 bash

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.

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.

Docker container not starting (docker start)

I created the container with the following command:
docker run -d -p 52022:22 basickarl/docker-git-test
Here are the commands:
root#basickarl:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root#basickarl:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4ac54468455 basickarl/docker-git-test:latest "/bin/bash" 7 minutes ago Exited (0) 26 seconds ago adoring_lumiere
22d7c5d83871 basickarl/docker-git-test:latest "/bin/bash" 2 hours ago Exited (127) About an hour ago thirsty_wright
root#basickarl:~# docker attach --sig-proxy=false e4
FATA[0000] You cannot attach to a stopped container, start it first
root#basickarl:~# docker start e4
e4
root#basickarl:~# docker attach --sig-proxy=false e4
FATA[0000] You cannot attach to a stopped container, start it first
root#basickarl:~#
Not much to say really, I'm expecting the container to start and stay upp. Here are logs:
root#basickarl:~# docker logs e4
root#basickarl:~#
You are trying to run bash, an interactive shell that requires a tty in order to operate. It doesn't really make sense to run this in "detached" mode with -d, but you can do this by adding -it to the command line, which ensures that the container has a valid tty associated with it and that stdin remains connected:
docker run -it -d -p 52022:22 basickarl/docker-git-test
You would more commonly run some sort of long-lived non-interactive process (like sshd, or a web server, or a database server, or a process manager like systemd or supervisor) when starting detached containers.
If you are trying to run a service like sshd, you cannot simply run service ssh start. This will -- depending on the distribution you're running inside your container -- do one of two things:
It will try to contact a process manager like systemd or upstart to start the service. Because there is no service manager running, this will fail.
It will actually start sshd, but it will be started in the background. This means that (a) the service sshd start command exits, which means that (b) Docker considers your container to have failed, so it cleans everything up.
If you want to run just ssh in a container, consider an example like this.
If you want to run sshd and other processes inside the container, you will need to investigate some sort of process supervisor.
What I need is to use Docker with MariaDb on different port /3301/ on my Ubuntu machine because I already had MySql installed and running on 3306.
To do this after half day searching did it using:
docker run -it -d -p 3301:3306 -v ~/mdbdata/mariaDb:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --name mariaDb mariadb
This pulls the image with latest MariaDb, creates container called mariaDb, and run mysql on port 3301. All data of which is located in home directory in /mdbdata/mariaDb.
To login in mysql after that can use:
mysql -u root -proot -h 127.0.0.1 -P3301
Used sources are:
The answer of Iarks in this article /using -it -d was the key :) /
how-to-install-and-use-docker-on-ubuntu-16-04
installing-and-using-mariadb-via-docker
mariadb-and-docker-use-cases-part-1
Good luck all!

How to bring a running docker container again foreground

I have a docker container running
sudo docker ps -a
result:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0cd8d5166717 32bit/ubuntu:14.04 /bin/bash 15 hours ago Up About an hour hadoop-test
How can I bring this running container again to -it mode and grab the terminal of ubuntu system ?
You can attach it:
docker attach 0cd8d5166717
Type Ctrl-PCtrl-Q to detach it again.

Resources