Assign labels to Docker daemon - docker

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

Related

how to reopen a docker container window again

I was able to run a docker container but if I do sudo docker-compose up -d but how to reopen/watch the screen again if I need and close again. I am using ubuntu.
Thanks
In order to follow the logs of all of the containers that are included in the docker-compose.yml file, run the command docker-compose logs -f (probably with sudo in your case) in the same directory in which you already ran sudo docker-compose up -d. You can find more information on the command here.
You are probably looking for docker attach (documentation). Usage is:
docker attach [OPTIONS] CONTAINER

Failed to connect. Is Docker running? (Vs Code)

I get this error in Ubuntu in vscode and I can't see my images in vscode.
I run sudo docker ps -a and everything is OK on terminal!
What should I do to solve this problem?
I think it can be because your user is not in the docker group.
Easily check the list of your user's groups using:
groups <user>
And check in the output if you can see "docker".
If not, simply add the user to the docker group by typing:
sudo usermod -aG docker ${USER}
Don't forget to restart the VS Code and the system if necessary.
I had to create docker group for themozel's solution to work.
Here is what worked for me:
Creates docker group
sudo groupadd docker
Add your user to the docker group
sudo usermod -aG docker $USER
The problem is docker is running as root but vs code trying to connect in user.
I am also having this problem.
I solved this problem with install the Docker Engine
Delete the docker completely
sudo apt-get remove docker docker-engine docker.io containerd runc
Then install the Docker Engine
https://docs.docker.com/engine/install/
With the docker extension installed, workaround for me (on Mac) was:
(cmd-shift-p)
Go to "Preferences: Open Workspace Settings"
at the top of the settings, search for "docker path"
enter Absolute path to docker client executable (in my case "/usr/local/bin/docker")
Hope this helps someone.
In you installed VS Code with the flatpak package manager (For example on PopOS) it will not detect docker

Docker System has not been booted with systemd as init system

I have an Ubuntu 18.04 image runing on my docker container. I login into it and installed Openresty. also installed systemd. When I use command systemctl I get this error:
System has not been booted with systemd as init system (PID 1). Can't operate.
How can I fix it?
If I understand the OP, he is trying to run systemctl within the container. This does not work because systemd is not running within the container to begin with. It cannot be done in unprivileged containers. There is another question here in SO about why he should not run systemd within a container.
I quickly googled and found this 2014 page about using systemd within a container in docker, where there is a short explanation. The fix is to use a privileged container (running docker run --privileged ...), which is arguably a bad idea but might suit the OP. There is a 2019 update of that last article, and the bottomline is they developed their own container engine (so no docker).
The obvious solution would be to have a single service, so no need for systemd, although that might not be possible in the OP's case.
In summary, possible solutions:
not to use systemd
use a privileged container
not to use docker
In your terminal, you can type:
$ sudo dockerd
and the magic is happen
So, Open other terminal and try it
$ docker ps -a
If you still have a problem with permission, run:
$ sudo usermod -aG docker your-user
Did you try to use: sudo /etc/init.d/docker start instead of systemd ?
I have a similar problem and it solves it.
You need to start your container by this command to enable systemd.
docker run -itd --privileged docker pull ubuntu:18.04 /usr/sbin/init
After toying with Systemd myself and bumping into this I found a good solution to work around this in Docker.
You can setup a cronjob to run on container reboot.
Dockerfile.yml:
COPY startup.sh /home/$USERNAME
WORKDIR /home/$USERNAME
RUN chmod +x startup.sh
RUN runuser -u $USERNAME -- echo "#reboot /home/$USERNAME/startup.sh" >> cronjobs
RUN runuser -u $USERNAME -- crontab cronjobs
RUN runuser -u $USERNAME -- rm cronjobs
https://askubuntu.com/questions/814/how-to-run-scripts-on-start-up#816
To complement #javier-gonzalez answer, if you're following running systemd within container AND getting the error bash: /usr/sbin/init: No such file or directory when trying to run the container, you can use /lib/systemd/systemd as ENTRYPOINT in your Dockerfile instead since /usr/sbin/init since it is just a symlink to the same thing.
FROM ubuntu:<anyversion>
ENTRYPOINT ["/lib/systemd/systemd"]
You may have forgotten to start docker before using it
sudo service docker start

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"

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