EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Docker Systemd

By Sarabjeet SinghSarabjeet Singh

Home » Software Development » Software Development Tutorials » Docker Tutorial » Docker Systemd

Docker Systemd

Introduction to Docker Systemd

The systemd is used to manage Docker daemon in many Linux distribution. We can start the Docker daemon, stop it, enable it so that it can be started automatically on boot, or check the status of it using systemd. We use the ‘systemctl’ command to manage services in most Linux distribution, in the same way, we can use this to manage Docker daemon as well, however, we can also use the ‘service’ command to do so if systemctl is not available. In short, we can control and manage Docker daemon with systemd.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

sudo systemctl <command> docker

Command:

  • start: It is used to start the docker service
  • stop: It is used to stop the docker service
  • status: It is used to check the status of the docker service
  • enable: It is used to configure the docker service to start at boot time
  • disable: It prevents the docker service to start at boot time.
  • restart: It is used to restart the docker service
  • show: It is used to show the environment variables define in systemd file.

sudo systemctl status docker

Docker Systemd 1

Explanation: In the above snapshot, we can see that the docker service is running and it is enabled.

How does systemd Command work in Docker?

We can configure the daemon flags and environment variables for our Docker daemon in different ways, however, the recommended way is to configure daemon flags and environment variables are to use the platform-independent file that is ‘daemon.json’. The default path of this file is ‘/etc/docker’ and it does not exist when we install Docker. We have to create this file when we configure daemon flags or environment variables the first time. We need to restart the Docker service after making changes to the ‘daemon.json’ file to enforce the changes.

Almost all daemon configuration options can be configured using ‘daemon.json’, however, we can not configure the ‘HTTP proxy’ option using ‘daemon.json’. We need to create a systemd drop-in directory for the docker service and create a configuration file inside it. It overrides the default docker.service file.

Examples

Let’s understand how we can configure Docker daemon flags or environment variables with the below examples: –

Scenario 1: Change the default logging driver to json-file and runtime directory to “/mnt/docker-data”

1. Let’s check the current configuration of the Docker daemon by running the ‘docker info’ command as below: –

Popular Course in this category
Docker Training (4 Courses, 3 Projects)4 Online Courses | 3 Hands-on Projects | 11+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (4,831 ratings)
Course Price

View Course

Related Courses
Python Training Program (36 Courses, 13+ Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

docker info

Docker Systemd 2

Docker Systemd 3

2. Now, edit the ‘daemon.json’ file or create it if does not exists already as shown below: –

sudo vi /etc/docker/daemon.json

1

3. We have to restart the Docker service using systemctl command to apply the new configuration as shown below: –

sudo systemctl restart docker

Note: You will get an error as shown below in the snapshot while restarting the docker daemon if the ‘daemon.json’ file is not proper or missing something, so re-check the ‘daemon.json’ file and restart the service.

Docker Systemd 5

Explanation: In the above snapshot, we can see that it encountered an error in the first attempt so recheck the ‘daemon.json’ for any error and fix it and then restarted the service once again without any error.

Tip: Wait for 1-2 mins after making changes to the file and then restart the service.

4. Let’s verify the changes by running the ‘docker info’ command once again:

docker info

Docker Systemd 6

Docker Systemd 7

Scenario 2: Configure HTTP/HTTPS Proxy by Overriding Docker.Service File

We have two ways to configure this, one is a regular install and another is the rootless mode. Below steps are used to configure HTTP/HTTPS proxy in regular install: –

1. First of all, we need to create a systemd drop-in directory for the docker service using the below command: –

sudo mkdir -p /etc/systemd/system/docker.service.d

Scenario 2

2. Now, create a file named http-proxy.conf inside the above directory and set the HTTP or HTTPS proxy, we can set both in the same file as shown below: –

sudo vi /etc/systemd/system/docker.service.d/http-proxy.conf

Scenario 2-1

Sudo

service

3. Save the file and exit.

4. We need to reload the daemon as we have created a new systemd file and restart the docker service to apply this new configuration as shown below: –

sudo systemctl daemon-reload
sudo systemctl restart docker

sudo 1

5. We can use the below command to verify the configuration has been loaded and it is the same as expected: –

sudo systemctl show --property=Environment docker

sudo 2

Advantages

  1. It is an easy way to control the Docker service.
  2. We can configure daemon flags and environment variables.
  3. We can change the default configuration of Docker daemon, like log-driver, storage driver, etc.

Rules and Regulation

  1. If we have already setup daemon flags on daemon startup then we cannot set it in the ‘daemon.json’ file. For example, if our Docker host uses systemd to start the Docker daemon then the ‘-H’ flag is already set, so we cannot use the hosts key in the ‘daemon.json’ file to add the listening address.
  2. If Docker is running in rootless mode, then we need to create the ‘docker.service.d’ folder in each users’ home directory to configure HTTP/HTTPS proxy as it uses files stored in each users’ directory. So, the folder path is ‘~/.config/systemd/user/docker.service.d’, other steps are same as mentioned in scenario-2.
  3. We can use the environment variable ‘NO_PROXY’ to exclude any hosts from proxying while configuring HTTP/HTTPS proxy. For example, we can exclude internal Docker registries.

Environment="NO_PROXY=localhost,127.0.0.1,docker-registry.example.com,.corp"

service 2

4. The ‘NO_PROXY’ option excludes, all domain name that matches in the option, for example, if we have mentioned ‘abc.com’ then it will exclude ‘abc.com’, ‘x.abc.com’, ‘y.abc.com’, and so on, however, if we have mentioned ‘.abc.com’ then it will exclude all ‘*.abc.com’ except ‘abc.com’.

5. We have optional filed ‘features’ in ‘daemon.json’ to enable or disable specific daemon features. For example, we can enable or disable ‘buildkit’ feature to set the default docker image builder.

Docker Systemd 8

Conclusion – Docker Systemd

We can control and manage almost all the configuration settings of Docker daemon using systemd. If we have to install the binary without package then we need to install two unit files ‘service and socket’ from Github repository to ‘/etc/systemd/system’ to integrate Docker with systemd.

Recommended Articles

This is a guide to Docker Systemd. Here we discuss the Introduction, How does systemd Command work in Docker with examples? respectively. You may also have a look at the following articles to learn more –

  1. Docker Hosts
  2. Docker Stop Container
  3. Docker Export
  4. Docker Volume

Docker Training (4 Courses, 3 Projects)

4 Online Courses

3 Hands-on Projects

11+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Docker Tutorial
  • Docker
    • What is Docker in Linux
    • Docker Architecture
    • Install Docker
    • Advantages of Docker
    • Docker Commands
    • Docker Toolbox
    • Docker Storage Drivers
    • Docker Push
    • Docker run Command
    • Dockerfile
    • Docker Containers
    • Docker Container Linking
    • Docker Stop Container
    • Docker List Containers
    • Docker Registry
    • Docker Volume
    • Docker Images
    • Docker Export
    • Docker Import
    • Docker Systemd
    • Docker Stack
    • Docker Privileged
    • Docker Pull
    • Docker ENTRYPOINT
    • Docker Start
    • Docker system prune
    • Docker Hosts
    • Docker Logging
    • Docker Save
    • Docker Commands Cheat Sheet
  • Docker Swarm
    • What is Docker Swarm
    • Docker Swarm Architecture
  • Interview Questions
    • Docker Interview Questions

Related Courses

Docker Training Course

Python Training Course

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Docker Training (4 Courses, 3 Projects) Learn More