How to SSH into running docker container from jenkins execute shell - linux

I am running a docker container (dind) from jenkins execute shell
CONTAINER_ID="$(sudo docker run --privileged -i -d jpetazzo/dind)"
To execute docker commands inside container I get into container shell
sudo docker exec -it --privileged ${CONTAINER_ID} bash
and than I am trying to execute these commands inside dind container.
sudo docker pull hubuser/hello-world
sudo docker run hubuser/hello-world
sudo docker tag imageId hubuser/hello-world:123
sudo docker login --username=hubuser --password=password
sudo docker push hubuser/hello-world
All of these 7 commands I have written in jenkins execute shell. Below given 5 commands are executing outside dind container, not inside. If I am trying from terminal than it is attaching to the container shell and executing properly. I want to execute them inside container but from jenkins. I also tried adding exec before every command like this:
sudo docker exec -it --privileged ${CONTAINER_ID} sudo docker pull hubuser/hello-world
sudo docker exec -it --privileged ${CONTAINER_ID} sudo docker run hubuser/hello-world
and so on. This executes commands inside dind container but all commands executes in parallel, so before pulling and running the hello-world image, it tries to tag it and push it. There it is not finding any hello-world image to tag and it does't do anything.
I want all my below 5 commands to execute serially inside dind container, that too from jenkins execute shell.

The title of your post is "how to ssh into running docker". I just want to point out this article, wrote by a Docker engineer: If you run SSHD in your Docker containers, you're doing it wrong!
After I read your post, that is not treating issue with ssh, I just thought about: why not execute a bash-script that does sequentially what you wanted to?
I'm not sure if I understood well btw, is Jenkins inside a docker? Are you running a docker in a docker?

Related

OCI runtime exec failed: exec failed: container_linux.go:344: starting container process

When i run the below command
$ docker container exec -it nginx1 ping nginx2
This is the error which i faced :
OCI runtime exec failed: exec failed: container_linux.go:344: starting container process caused "exec: \"ping\": executable file not found in $PATH": unknown
How to resolve this issue ?
Before reading this answer just let you know, it's my 2nd day of learning docker, It may not be the perfect help for you.
This error may also occur when the ping package is not installed in the container, I resolved the problem as follow, bash into the container like this
docker container exec -it my_nginx /bin/bash
then install ping package
apt-get update
apt-get install inetutils-ping
This solved my problem.
Please use alpine image of nginx:
docker container run -d --name my_nginx_name nginx:alpine
docker container run -d --name my_nginx_name2 nginx:alpine
Then try to ping using below command:
docker container exec -it my_nginx_name ping my_nginx_name2
I had the same problem and managed to solve it by accessing:
docker exec -ti <CONTAINER ID> /bin/sh
This something I came across recently. When ran a docker container with a custom name and if we put an command/option(s)/etc after the name, that would be passed to the container as commands. So in here container tried to find the ping command inside it but couldn't, So as the above answer you must install the inetutils-ping inside the container and run the command
Try this it worked for me
# $ docker container exec -it new_nginx bash
# apt-get update
# apt-get install inetutils-ping
Do it for both the container than run your command
# $ docker container exec -it nginx1 ping nginx2
Install ping utilities in the container .
docker container exec -it webhost /bin/bash
apt-get update
apt-get install inetutils-ping
docker container exec -it webhost ping new_nginx
Try to install ping in both conatiners,
apt-get update,
apt-get install inetutils-ping
After that Try the ping command.
This error is reported when you try to run the command not found in docker image. Please check if ping is installed in the docker image.

'service docker status' fails: invalid arguments

I'm having trouble installing docker inside docker.
Outer docker works just fine but inner docker behavior is doubtful. I've followed all steps in official tutorial but after installation when I call status it still shouts error:
$ > sudo service docker status
/etc/init.d/docker: invalid arguments
* could not access PID file for Docker
BTW starting service works fine:
$ > sudo service docker start
* Starting Docker: docker [ OK ]
But docker itself seems to be working properly. hello-world is working and all other experiment I'm doing are also working so far. But I'm not sure I won't stumble upon some other issue later on.
I tried using several OS combinations. For outer OS i used Centos 7 and LinuxMint 17.3 and inner OS ubuntu:14.04 and ubuntu:16.04 but result is always the same (so I'm guessing it has to do something with ubuntu).
Outer docker is running docker with --privileged flag.
So it looks like this:
[outer OS Centos 7] docker run -ti --privileged ubuntu:14.04 bash
[inner OS Ubuntu] sudo service docker status
Thanks for the help,
x3mSpeedy

docker-machine breaks docker native client on linux

I am on Ubuntu and decided to use docker-machine to run some docker swarm tests. Here you execute
eval $(docker-machine env xxxxx)
and with that your native docker client points to that machine/vm. However, after the tests I wan't the docker command to point to my local docker client/daemon/whatever and executed
eval $(docker-machine env -u)
which is supposed to unset the environment variables. But now I get this error
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.
I've had to create a docker machine on VirtualBox called default, point to that machine and run my commands there. But its pretty lame since I feel like I am back on Windows and one of the reason I came to Ubuntu was better docker integration.
Is there any fix for this?
unset all docker variables
unset ${!DOCKER_*}
regarding the 'can't connect to daemon', ensure you're prepending each docker command with sudo, or to allow your current user to interact with docker use:
sudo groupadd docker
sudo usermod -aG docker $(whoami)
restart docker and
re-login to the terminal

Starting a shell in the Docker Alpine container

To start an interactive shell for the Ubuntu image we can run:
ole#T:~$ docker run -it --rm ubuntu
root#1a6721e1fb64:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
But when this is run for the Alpine Docker image, the following results:
ole#T:~$ docker run -it --rm alpine
Error response from daemon: No command specified
What is the command for starting an interactive shell in an Alpine base container?
ole#T:~$ docker run -it --rm alpine /bin/ash
(inside container) / #
Options used above:
/bin/ash is Ash (Almquist Shell) provided by BusyBox
--rm Automatically remove the container when it exits (docker run --help)
-i Interactive mode (Keep STDIN open even if not attached)
-t Allocate a pseudo-TTY
Usually, an Alpine Linux image doesn't contain bash, Instead you can use /bin/ash, /bin/sh, ash or only sh.
/bin/ash
docker run -it --rm alpine /bin/ash
/bin/sh
docker run -it --rm alpine /bin/sh
ash
docker run -it --rm alpine ash
sh
docker run -it --rm alpine sh
I hope this information helps you.
Nowadays, Alpine images will boot directly into /bin/sh by default, without having to specify a shell to execute:
$ sudo docker run -it --rm alpine
/ # echo $0
/bin/sh
This is since the alpine image Dockerfiles now contain a CMD command, that specifies the shell to execute when the container starts: CMD ["/bin/sh"].
In older Alpine image versions (pre-2017), the CMD command was not used, since Docker used to create an additional layer for CMD which caused the image size to increase. This is something that the Alpine image developers wanted to avoid. In recent Docker versions (1.10+), CMD no longer occupies a layer, and so it was added to alpine images. Therefore, as long as CMD is not overridden, recent Alpine images will boot into /bin/sh.
For reference, see the following commit to the official Alpine Dockerfiles by Glider Labs:
https://github.com/gliderlabs/docker-alpine/commit/ddc19dd95ceb3584ced58be0b8d7e9169d04c7a3#diff-db3dfdee92c17cf53a96578d4900cb5b
In case the container is already running:
docker exec -it container_id_or_name ash

Docker can't connect to docker daemon

After I update my Docker version to 0.8.0, I get an error message while entering sudo docker version:
Client version: 0.8.0
Go version (client): go1.2
Git commit (client): cc3a8c8
2014/02/19 12:54:16 Can't connect to docker daemon. Is 'docker -d' running on this host?
And I've followed the instructions and entered command sudo docker -d, and I got this:
[/var/lib/docker|2462000b] +job initserver()
[/var/lib/docker|2462000b.initserver()] Creating server
open /var/lib/docker/aufs/layers/cf2414da53f9bcfaa48bc3d58360d7f1cfd3784e4fe51fbef95197709dfc285d: no such file or directory[/var/lib/docker|2462000b] -job initserver() = ERR (1)
2014/02/19 12:55:57 initserver: open /var/lib/docker/aufs/layers/cf2414da53f9bcfaa48bc3d58360d7f1cfd3784e4fe51fbef95197709dfc285d: no such file or directory
How do I solve the problem?
Linux
The Post-installation steps for Linux documentation reveals the following steps:
Create the docker group.
sudo groupadd docker
Add the user to the docker group.
sudo usermod -aG docker $(whoami)
Log out and log back in to ensure docker runs with correct permissions.
Start docker.
sudo service docker start
Mac OS X
As Dayel Ostraco says is necessary to add environments variables:
docker-machine start # Start virtual machine for docker
docker-machine env # It's helps to get environment variables
eval "$(docker-machine env default)" # Set environment variables
The docker-machine start command outputs the comments to guide the process.
Linux
To run docker daemon on Linux (from CLI), run:
$ sudo service docker start # Ubuntu/Debian
Note: Skip the $ character when copy and pasting.
On RedHat/CentOS, run: sudo systemctl start docker.
To initialize the "base" filesystem, run:
$ sudo service docker stop
$ sudo rm -rf /var/lib/docker
$ sudo service docker start
or manually like:
$ sudo docker -d --storage-opt dm.basesize=20G
Install docker-machine on Linux
To install machine binaries on Linux:
locally:
install -vm755 <(curl -L https://github.com/docker/machine/releases/download/v0.5.3/docker-machine_linux-amd64) $HOME/bin/docker-machine
global:
sudo bash -c 'install -vm755 <(curl -L https://github.com/docker/machine/releases/download/v0.5.3/docker-machine_linux-amd64) /usr/local/bin/docker-machine'
macOS
On macOS the docker binary is only a client and you cannot use it to run the docker daemon, because Docker daemon uses Linux-specific kernel features, therefore you can’t run Docker natively in OS X. So you have to install docker-machine in order to create VM and attach to it.
Install docker-machine on macOS
If you don't have docker-machine command yet, install it by using one of the following methods:
Using Brew command: brew install docker-machine docker.
manually from GitHub:
install -v <(curl https://github.com/docker/machine/releases/download/v0.5.3/docker-machine_linux-amd64) /usr/local/bin/docker-machine
See: Get started with Docker for Mac.
Configure docker-machine on macOS
To start Docker Machine via Homebrew, run:
brew services start docker-machine
To create a default machine (if you don't have one, see: docker-machine ls):
docker-machine create --driver virtualbox default
Then set-up the environment for the Docker client:
eval "$(docker-machine env default)"
Then double-check by listing containers:
docker ps
See: Get started with Docker Machine and a local VM.
Install Docker.app on macOS
Alternatively to above solution, you can install a Docker app by:
brew cask install docker
Check this post for more details. See also: Cannot connect to the Docker daemon on macOS
If you are running Docker on OS X, running the following eval has worked for me.
eval "$(docker-machine env default)"
If you'd prefer not to have to run this eval statement on every terminal session, you can add this to your bash_profile:
#Docker
eval "$(docker-machine env default)"
Be sure to restart the terminal session or run source on bash_profile for the changes to take effect.
After a detailed investigation, this issue seems to happen every time after Mac OS X is rebooted (or the Docker virtual machine is restarted) which prevents the Docker client from connecting to the Docker daemon.
To solve the issue, you can either:
A) Reinstall Docker Toolbox using the official installer (https://www.docker.com/products/docker-toolbox), or simply
B) Run the following commands in order:
# First make sure that the virtual machine is running
docker-machine start default
# Regenerate TLS connection certs, requires confirmation
docker-machine regenerate-certs default
# Finally, set env
eval "$(docker-machine env default)"
C) Same as (B), you can also copy and paste the following line to run all of the three commands:
docker-machine start default; docker-machine regenerate-certs default; eval "$(docker-machine env default)"
In case you get the following error:
Error getting SSH command: Something went wrong running an SSH command!
command : cat /etc/os-release
err : exit status 255
output :
just re-run the three commands another time, and it should work the second time.
This usually happens when you are not in the docker group. You can add yourself to the docker group with:
sudo usermod -aG docker yourusername
or
sudo usermod -aG docker $(whoami)
After this, you need to logout and log back into the server.
Alternatively, you can sudo every Docker command.
If all the other solutions above don't work you can try checking the ownership of /var/run/docker.sock:
ls -l /var/run/docker.sock
If you're not the owner then change ownership with the command:
sudo chown *your-username* /var/run/docker.sock
Then you can go ahead and try executing the Docker commands hassle-free :D
You can use the command
sudo service docker stop && sudo service docker start
OR
sudo service docker restart
to simply restart it.
The best way to find out why Docker isn't working will be to run the daemon manually.
$ sudo service docker stop
$ ps aux | grep docker # do this until you don't see /usr/bin/docker -d
$ /usr/bin/docker -d
The Docker daemon logs to STDOUT, so it will start spitting out whatever it's doing.
Here was what my problem was:
[8bf47e42.initserver()] Creating pidfile
2015/01/11 15:20:33 pid file found, ensure docker is not running or delete /var/run/docker.pid
This was because the instance had been cloned from another virtual machine. I just had to remove the pidfile, and everything worked afterwards.
Of course, instead of blindly assuming this will work, I'd suggest running the daemon manually one more time and reviewing the log output for any other errors before starting the service back up.
Do a ps aux | grep docker to see if the daemon is running. If not run /etc/init.d/docker start
If you get the message Can't connect to docker daemon. Is 'docker -d' running on this host?, you can check it by docker version.
If you see the information like Docker Client is running. but Docker Server is not, it's obviously you need to start the Docker server.
In CentOS, you can use service to start or stop the Docker server.
$ sudo service docker stop
$ sudo service docker start
Then, after you type docker version, you will get the information of Docker Client and Docker Server, and the Docker daemon has been started.
Use Docker CE app
macOS
Use the new Docker Community Edition app for macOS. For example:
Uninstall all Docker Homebrew packages which you've installed so far:
brew uninstall docker-compose
brew uninstall docker-machine
brew uninstall docker
Install an app manually or via Homebrew-Cask:
brew install --cask docker
Note: This app will create necessary links to docker, docker-compose, docker-machine, etc.
After running the app, checkout the a Docker whale icon in the status menu.
Now you should be able to use docker, docker-compose, docker-machine commands as usual in the Terminal.
Related:
Brew install docker does not include docker engine?
Cannot connect to the Docker daemon on macOS
Linux/Windows
Download the Docker CE from the download page and follow the instructions.
I have similar problem. I had to logout and login again to shell because I have just installed Docker and following command didn't show in my environment.
export DOCKER_HOST=127.0.0.1:4243 >> ~/.bashrc
I restart Docker after installing it:
$ sudo service docker stop
$ sudo service docker start
And it works.
I have faced this problem, and I restarted Docker using these commands:
$ sudo service docker stop
$ sudo service docker start
But I did not solve my problem, because I forgot to execute my Docker commands without sudo. For those who faces this problem, try to check that.
Try
$ sudo docker info
instead of this:
$ docker info
I have the same error and trying docker-machine regenerate-certs or eval.. did not work for me.
This on OS X 10.11.3 (El Capitan) and Docker v1.10.1. I was able to fix it only by deleting and recreating docker-machine again. Source
If running docker-machine ls, it shows you a similar output to the one below;
DOCKER
Unknown
ERRORS
Unable to query docker version: Cannot
connect to the docker engine endpoint
Try removing your Docker machine with;
docker-machine rm -f default
Where default is your Docker machine name. Then;
docker-machine create -d virtualbox default
Creates a new Docker machine.
Double check that everything looks normal now (no errors or unknown Docker) with:
docker-machine ls
Finally don't forget to run "$(docker-machine env default)" before you continue or run the Docker Quickstart Terminal which does it for you...
I knew that there are plenty of answers already in this post. Just I would like to add one simple answer that is solved the above mentioned problem .
sudo systemctl start docker
Run the above command and it will start all the docker related threads/services.
Try adding the current user to docker group:
sudo usermod -aG docker $USER
Then log out and login.
At April 2020 on MacOS Catalina, you just need to open the desktop application:
I had the same problem - "Can't connect to docker daemon." (except I didn't get any 'file not found' errors on trying to start the server.)
'ps' showed that "/usr/bin/docker -d" was still running
I realised that I'd never actually succeeded in running the server myself though. Every attempt had produced
...
2014/03/24 21:57:29 pid file found, ensure docker is not running or delete /var/run/docker.pid
So I belatedly realised that installing docker had maybe registered the daemon with upstart, which had started it for me. Hence, trying to kill the daemon to manually restart it fails (operation not permitted). So I did a
sudo kill -9 <PID>
on the daemon process. Another daemon immediately took its place, and this new one DOES now let my CLI client connect:
$ sudo docker info
Containers: 0
Images: 0
Driver: aufs
Root Dir: /var/lib/docker/aufs
Dirs: 0
WARNING: No memory limit support
WARNING: No swap limit support
Following Docker's DOC site: Manage Docker as a non-root user
1) Create Docker Group
sudo groupadd docker
2) Make user belong to docker group to get the group's privileges.
sudo usermod -aG docker $USER
Check whether the DOCKER_HOST environment variable is set for your shell.
env | grep DOCKER_HOST
If it exists,
unset DOCKER_HOST
Then this should work:
docker run hello-world
I just had the same issue, running on Amazon AWS.
Here's what I attempted:
Set up docker-machine locally with already existing AWS instance
Used generic setup
It kind of connected, but since the remote port was closed, it failed
After that, the Docker daemon refused to start up, but running dockerd did work...
It was tested following on the remote machine:
service docker start # Also restart, no success
systemctl start docker # Also restart, no success
dockerd # Success
I removed /var/lib/docker and uninstalled everything, but there was no success after reinstallation. Unfortunately I have no logs stored from failures, but docker.service just refused to start.
However, what finally solved my issue was basically:
sudo usermod -aG docker $(whoami)
I got the same problem. In CentOS 6.5:
ps aux |grep `cat /var/run/docker.pid`
If it shows no Docker daemon process exists, then I type:
docker -d
Then Ctrl + D to stop Docker. Because we use the -d option, Docker will run as daemon. Now we can do:
service docker start
Then I can do a docker pull centos. That's all.
NOTE: If these do not work, you can try yum update, and then repeat these again, because I yum install before these.
If you are running on OS X using Docker tool, follow this.
Restart the daemon and configure your environment:
docker-machine restart
And then
docker-machine env
Finally,
eval $(docker-machine env)
To test the daemon is running:
docker ps -a or docker-machine ls. This will list all containers.
The Docker Service may not be running.
If you are on a RedHat/Fedora/CentOS, please try this:
sudo systemctl start docker
If you are on Ubuntu/Debian:
sudo service start docker
Docker will start running on your host and respective port.
Run the following command:
docker context use default
To fix this issue, I had to enable the docker service:
sudo systemctl enable /usr/lib/systemd/system/docker.service
Check if you are using Docker Machine :)
Run docker-machine env default should do the trick.
Because according to documentation:
Docker Machine is a tool that lets you install Docker Engine on
virtual hosts, and manage the hosts with docker-machine commands. You
can use Machine to create Docker hosts on your local Mac or Windows
box, on your company network, in your data center, or on cloud
providers like AWS or Digital Ocean.
Using docker-machine commands, you can start, inspect, stop, and
restart a managed host, upgrade the Docker client and daemon, and
configure a Docker client to talk to your host.
Point the Machine CLI at a running, managed host, and you can run
docker commands directly on that host. For example, run
docker-machine env default to point to a host called default, follow on-screen
instructions to complete env setup, and run docker ps,
docker run hello-world, and so forth.
https://docs.docker.com/machine/overview/
I also had the same issue. The problem was in sockets allocated to docker-daemon and docker-client.
First, permission was not set for the docker-client on docker.sock You can set it using "sudo usermod -aG docker $USER"
Then check your bash file where the docker-client is running, For me it was on 0.0.0.0:2375, while docker-daemon was running on unix socket.(It was set in the configuration file of dockerd).
Just comment the bash-line and it'll work fine.
But if you want to make it work on TCP port instead of unix socket, change the configuration file of dockerd and set it on 0.0.0.0.2375 and keep the line in bash as it is if present or set it to 0.0.0.0:2375.
To fix, you need to issue the following commands in the terminal. I'll explain each step:
# Uninstall Docker from apt packages
$ sudo apt-get remove docker docker.io
# Remove it from the libraries just to be
# sure it's gone forever
$ sudo rm -rf /var/lib/docker/*
Now, if you want to simplify things and get more time, you can run my init script with the parameter installDocker:
# Pull the init script from GitHub
$ wget https://github.com/dminca/dotfiles/blob/master/init
# Add rights to run the script
$ chmod 755 init
# Just run the script with the installDocker parameter
$ ./init installDocker
A reboot is optional, but I suggest you do it to be sure all runs smoothly.
I had the same problem running Docker 1.10 on Ubuntu 14.04 and none of the given answers worked. For me, the fix was to specify the storage driver when running the Docker daemon.
sudo docker daemon --storage-driver=devicemapper

Resources