How do i expose the Docker Remote API on Centos 7? - docker

On ubuntu i can go into /etc/init/docker.conf and put in DOCKER_OPTS='-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock' to get the json data to display on my browser but how can i do it for Centos?
I have tried creating a file in /etc/sysconfig/docker and placing other_args="-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock" inside the file and restarting docker but it doesn't do anything.

The systemd unit installed by the Docker corp package hardcodes the command line used to start the docker daemon:
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
[...]
There is no support for reading a file from /etc/sysconfig or elsewhere to modify the command line. Fortunately, systemd gives us the tools we need to change this behavior.
The simplest solution is probably to create the file /etc/systemd/system/docker.service.d/docker-external.conf (the exact filename doesn't matter; it just needs to end with .conf) with the following contents:
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock
And then:
systemctl daemon-reload
systemctl restart docker
This is actually documented on the Docker website in this document, which includes instructions for a more flexible solution that will allow you to use files in /etc/sysconfig to control the daemon.

Yes, you can do the configuration thing. But how about a docker solution to a docker problem?
docker run -d \
--name sherpa \
-v /var/run/docker.sock:/tmp/docker.sock \
-p 2375:4550 \
djenriquez/sherpa --allow
Proxies access to the socket through port 2375 on localhost.

1、edit /usr/lib/systemd/system/docker.service to add two params in the service section:
# vim /usr/lib/systemd/system/docker.service
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
2、reload the configuration, and then restart docker。
# systemctl daemon-reload
# systemctl restart docker
3、to check for success, see if the return the following response。
# ps -ef|grep docker
root 26208 1 0 23:51 ? 00:00:00 /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
reference from Expose the Docker Remote API on Centos 7?

Related

Connect to remote docker host

I have following scenario.
Two Machine ( Physical Machine)
One is Windows 10 With Docker On Windows Installer and same way ubuntu 18.04 with docker-ce installed.
I can run command on individual and that is fine.
I want to connect Ubuntu Docker Host from Docker on Windows machine. So Docker CLI on Windows Point to deamon at Ubuntu Host.
You will need to enable docker remote API on Ubuntu Docker Host by adding below settings in daemon.json or your startup script
[root#localhost ~]# cat /etc/docker/daemon.json
{
"hosts": [ "unix:///var/run/docker.sock", "tcp://0.0.0.0:2376" ]
}
Once you restart docker you can connect to docker host locally by socket file and remotely by listening port (2376).
Verify the listening port of docker on Ubuntu
[root#localhost ~]# netstat -ntlp | grep 2376
tcp6 0 0 :::2376 :::* LISTEN 1169/dockerd
Now you can connect to this docker from Windows machine by setting the DOCKER_HOST env variable in Windows like this
PS C:\Users\YellowDog> set DOCKER_HOST=tcp://<Ubuntu-Docker_Host-IP>:2376
PS C:\Users\YellowDog> docker ps
It will list docker containers running on Ubuntu Docker Host
You can also do this through additional options to the service:
Find original ExecStart line docker.service:
systemctl status docker | grep load | grep -oP "\/.+service"
# --> /lib/systemd/system/docker.service
cat /lib/systemd/system/docker.service | grep ExecStart
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS
Create a new file to store the daemon options:
sudo mkdir /etc/systemd/system/docker.service.d/
Add next lines with -H unix:// -H tcp://0.0.0.0:2375 options to /etc/systemd/system/docker.service.d/options.conf:
cat <<EOF > /etc/systemd/system/docker.service.d/options.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// \$DOCKER_OPTS -H unix:// -H tcp://0.0.0.0:2375
EOF
Here you need to pay attention to the escaping $DOCKER_OPTS variable if it exists.
Or using your favorite editor, for example vim.
Now, reload the systemd daemon and restart the docker service:
# Reload the systemd daemon.
sudo systemctl daemon-reload
# Restart Docker.
sudo systemctl restart docker
Configuring your dev box to connect to the remote Docker daemon:
If you want to set DOCKER_HOST by default so it always connects remotely you can export it in your ~/.bashrc file.
Here’s an example of that as a 1 liner:
echo "export DOCKER_HOST=tcp://X.X.X.X:2375" >> ~/.bashrc && source ~/.bashrc
Or use it all at once:
DOCKER_HOST=tcp://X.X.X.X:2375 docker ps

how to set the DOCKER_HOST?

I was doing the django-shop tutorial from this link:https://django-shop.readthedocs.io/en/latest/tutorial/quickstart.html . I am very new in docker ,docker-compose and linux .
I get this error:
ERROR: Couldn't connect to Docker daemon at http://127.0.0.1:2375 - is
it running?
If it's at a non-standard location, specify the URL with the
DOCKER_HOST environment variable.
When I execute these commands...
$ git clone --depth 1 github.com/awesto/django-shop
$ cd django-shop
$ export DJANGO_SHOP_TUTORIAL=commodity
$ docker-compose up --build -d
I tried to do this Tutorial and this didn't work.
EDIT:
I use this Command to solve this problem:
$ sudo adduser razvan docker
As a general rule, never set DOCKER_HOST.
Given your error message, it looks like it might be set (incorrectly) and you might see if things get better if you
unset DOCKER_HOST
The two prominent exceptions are VM-based Docker environments (Docker Toolbox, Docker Machine, Kubernetes' minikube). In these cases there are helper scripts that can set it to the correct value:
eval $(docker-machine env) # Docker Machine, Docker Toolbox
eval $(minikube docker-env) # Minikube
By set DOCKER_HOST you tell for every run of docker in command line to use http api, instead of default - socket on localhost.
By default http api is turned off
$ sudo cat /lib/systemd/system/docker.service | grep ExecStart
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
you can add -H tcp://127.0.0.1:2375 for tern on http api on localhost
but usually you want to tern on api for remote servers by -H tcp://0.0.0.0:2375 (!!! do it only with proper firewall !!!)
so you need to change in /lib/systemd/system/docker.service to next line
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://127.0.0.1:2375 --containerd=/run/containerd/containerd.sock
I am using Ubuntu 16.04 so I went at the end of the /home/user/.profile file and placed the unset DOCKER_HOST command.
Then sourced the file like so: source /home/user/.profile then logged out and logged back in and docker now works normally.

How to configure docker to use /opt instead of /var

Docker uses /var/lib/docker for storing images, how to configure docker to use /opt in centos ?
I can able to change the docker directory using following set of command's
1.Stop docker
sudo systemctl stop docker
create docker.service.d
sudo mkdir /etc/systemd/system/docker.service.d
3.create docker.conf
sudo touch /etc/systemd/system/docker.service.d/docker.conf
4.Add the docker.conf with following lines
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --graph=/opt/docker --storage-driver=devicemapper
5.start Servicec
sudo systemctl daemon-reload
sudo systemctl start docker
Now the docker is using opt/docker instead of /var/lib/docker
You can configure docker daemon by option --graph(-g for short). In CentOS the service is managed by Systemd, you can find the link of service unit file at /etc/systemd/system/multi-user.target.wants/docker.service, or the original place /usr/lib/systemd/system/docker.service and change the ExecStart line, like:
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -g /opt
Then use systemctl daemon-reload to reload the changes, and systemctl restart docker to restart docker service.
you need to set the value of the graph parameter to your custom path, in /etc/docker/daemon.json
you can find it in:
https://docs.docker.com/v1.11/engine/reference/commandline/daemon/#daemon-configuration-file

Cannot change Docker image directory

I am using Fedora 22 and I must change my Docker image directory from
/var/lib/docker
to
/home/my_user/docker
Following this
How to change the docker image installation directory? I edited the /etc/sysconfig/docker adding:
other_args="-g /home/rseixas/Programs/Docker/images"
I restarted the service but no change. In fact I restarted my machine and I am not able to see it changing.
Someone can help me?
Do you have a /lib/systemd/system/docker.service file?
If so, edit it so that the Docker service uses the usual /etc/default/docker as an environment file: EnvironmentFile=-/etc/default/docker.
In the /etc/default/docker file then add DOCKER_OPTS="-g /home/rseixas/Programs/Docker/images".
At the end just do a systemctl daemon-reload && systemctl restart docker.
For further information please also have a look at the documentation.
In docker 1.8+ the service file settings changed a little:
[Service]
EnvironmentFile=-/etc/default/docker
# in docker 1.7 use ExecStart:
ExecStart=/usr/bin/docker -d $DOCKER_OPTS -H fd://
# in docker 1.8 use ExecStart:
ExecStart=/usr/bin/docker daemon $DOCKER_OPTS -H fd://
MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
& some other notes for Debian / Fedora with the latest docker & a custom directory.

How to add variable to docker daemon in CentOS?

I want to create registry mirror in docker. I read this tutorial. So,I want to add this variable "--registry-mirror=http://10.0.0.2:5000" to docker daemon when it start.
I have succeeded in mac. I add the line to /var/lib/boot2docker/profile:
EXTRA_ARGS="--registry-mirror=http://192.168.59.103:5555"
It can work after adding in mac. So I do the same thing in CentOS. I use the command in this question:I:
sudo sed -i 's|other_args=|other_args=--registry-mirror=http://<my-docker-mirror-host> |g' /etc/sysconfig/docker
sudo sed -i "s|OPTIONS='|OPTIONS='--registry-mirror=http://<my-docker-mirror-host> |g" /etc/sysconfig/docker
sudo service docker restart
and it makes my "/etc/sysconfig/docker" like below in CentOS, and this is my docker file:
# /etc/sysconfig/docker
#
# Other arguments to pass to the docker daemon process
# These will be parsed by the sysv initscript and appended
# to the arguments list passed to docker -d
OPTIONS=--selinux-enabled -H fd:// -g="/opt/apps/docker"
other_args="--registry-mirror=http://10.11.150.76:5555"
Then, I restart docker using this command:
service docker restart
But, the mirror didn't work in CentOS. I use command:
ps -ef
It did't add the variable to docker daemon. what is wrong?
In the /etc/sysconfig/docker file, change:
OPTIONS=--selinux-enabled -H fd:// -g="/opt/apps/docker"
into:
OPTIONS=--selinux-enabled -H fd:// -g="/opt/apps/docker" --registry-mirror=http://10.11.150.76:5555
I can't help you with other_args, I don't know this option.
If you using yum install docker, you may get a problem with the docker service config file.
Then you need to check your system service config file, see if it using other_args as a parameter to start docker. By default, the service config file should placed at /usr/lib/systemd/system/docker.service, edit it with any editor, check ExecStart part, add other_args to it.
For example, ExecStart=/usr/bin/docker -d --selinux-enabled $other_args

Resources