How to add variable to docker daemon in CentOS? - docker

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

Related

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.

Assign labels to Docker daemon

How do I assign labels to an already running Docker daemon on Ubuntu?
Tried:
export DOCKER_OPTS="--label=com.example.storage=ssd"
sudo restart docker
but didn't help. docker info need to show Labels.
The docker configuration section is clear:
Log into your host as a user with sudo or root privileges.
If you don’t have one, create the /etc/default/docker file on your host. Depending on how you installed Docker, you may already have this file.
Open the file with your favorite editor.
$ sudo vi /etc/default/docker
Add a DOCKER_OPTS variable with the following options. These options are appended to the docker daemon’s run command.
DOCKER_OPTS="--label=com.example.storage=ssd"
Save and close the file.
Restart the docker daemon.
$ sudo restart docker
sudo sed -i `'/DOCKER_OPTS/c\DOCKER_OPTS="--label=com.example.storage=ssd"' /etc/default/docker`
did the trick for me.
I would guess "sudo" isn't copying your environment. You might try "sudo -E".

Docker logging to container

I'm fresh user of Docker. The fist problem with which I'm faced is logging into container.
I'm found solutions to execute container bash commands by
docker exec -it ID bash
But, this is solution only for install/ remove packages. What to use if I want to edit nginx config in docker container ?
One of solutions can be loggin to container via ssh connection, but maybe Docker have something own for this ?, I mean easilly access without install OpenSSH ?
as you said,
docker exec -it container_id bash
and then use your favorite editor to edit any nginx config file. vi or nano is usually installed, but you may need to install emacs or vim, if this is your favorite editor
if you have just a few characters to modify,
docker exec container_id sed ...
might do the job. If you want to SSH into your container, you will need to install SSH and deal with the SSH keys, I am not sure this is what you need.
You're going about it the wrong way. You should rarely need to log into a container to edit files.
Instead, mount the nginx.conf with -v from the host. That way you can edit the file with your normal editor. Once you've got the config working the way you want it, you can then build a new image with it baked in.
In general, you have to get into the mindset of containers being ephemeral. You don't patch them; you throw them away and replace them with a fixed version.
How: Docker logging to container
Yes, you can. You can login the running container.
Exist docker exec or docker attach is not good enough. Looking to start a shell inside a Docker container? The solution is: jpetazzo/nsenter with two commands: nsenter and docker-enter.
If you are in Linux environment, then run below command:
docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
docker ps
# replace <container_name_or_ID> with real container name or ID.
PID=$(docker inspect --format {{.State.Pid}} <container_name_or_ID>)
nsenter --target $PID --mount --uts --ipc --net --pid
Then you are in that running container, you can run any linux commands now.
I prefer the other command docker-enter. Without login the container, you can directly run linux commands in container with docker-enter command. Second, I can't memory multiple options of nsenter command and no need to find out the container's PID.
docker-enter 0e8c248982c5 ls /opt
If you are mac or windows user, run docket with toolbox:
docker-machine ssh default
docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
PID=$(docker inspect --format {{.State.Pid}} 0e8c248982c5)
sudo nsenter --target $PID --mount --uts --ipc --net --pid
If you are mac or windows user, run docket with boot2docker:
boot2docker ssh
docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
PID=$(docker inspect --format {{.State.Pid}} 0e8c248982c5)
sudo nsenter --target $PID --mount --uts --ipc --net --pid
Note: The command docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter only need run one time.
How: edit nginx config
For your second question, you can think about ONBUILD in Docker.
ONBUILD COPY nginx.conf /etc/nginx/nginx.conf
With this solution, you can:
edit nginx.conf in local, you can use any exist editor .
needn't build your image every time after you change nginx configuration.
every time, after you change nginx.conf file in local, you need stop, remove and re-run the containe, new nginx.conf file will be deployed into contrainer when docker run command.
You can refer the detail on how to use ONBUILD here: docker build

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.

What is the difference b/w "service docker start" and "docker -d"?

I'm new to docker, and want to restart docker daemon. I want to add the OPTS to start docker like:
docker --registry-mirror=http://<my-docker-mirror-host> -d
I want to know what is they difference? Does they start the same thing?
By the way, I just use above command in my boot2docker, it did't work at all.
if you use service docker start then it will start docker as service with docker's upstart configuration file, e.g. /etc/default/docker for ubuntu and /etc/sysconfig/docker for centos.
if you use docker -d it will run docker in daemon mode.
if you want define your own registry-mirror for docker, you can do this:
ubuntu
$ echo "DOCKER_OPTS=\"\$DOCKER_OPTS --registry-mirror=http://<my-docker-mirror-host>\"" | sudo tee -a /etc/default/docker
$ sudo service docker restart
centos
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
mac
boot2docker up
boot2docker ssh "echo $'EXTRA_ARGS=\"--registry-mirror=http://<my-docker-mirror-host>\"' | sudo tee -a /var/lib/boot2docker/profile && sudo /etc/init.d/docker restart”
then your docker service with run with your own registry mirror.
To answer your questions (which are valid for debian/ubuntu, I don't have tinylinux handy to test which is used by boot2docker):
service docker start will run a startup script in /etc/init.d/docker
docker -d is the manual version of the previous script, useful when you want to run docker in debug mode. I suspect the example you gave will not do the same thing, because there are more options specified in the service script.
if you want to add more docker command options, edit the /etc/default/docker file
Update after OP's comments:
To add your new switch, you need to specifically edit the variable (which maybe exported) DOCKER_OPTS and add your option to the end of the existing options.
My /etc/default/docker options are:
export DOCKER_OPTS="--tlsverify --tlscacert=/etc/docker/ca.pem
--tlskey=/etc/docker/server-key.pem --tlscert=/etc/docker/server.pem --label=provider=XXXX
--host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2376"
To add the registry-mirror I would edit the DOCKER_OPTS to look like this
export DOCKER_OPTS="--tlsverify --tlscacert=/etc/docker/ca.pem
--tlskey=/etc/docker/server-key.pem --tlscert=/etc/docker/server.pem --label=provider=XXXX
--host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2376
--registry- mirror=192.168.59.103:5555"

Resources