Skip to main content

Docker

Run systemd in container​

Dockerfile
FROM centos:7
MAINTAINER "Gimo" <self@gimo.me>
ENV container docker
RUN yum -y update; yum clean all
RUN yum -y install systemd; yum clean all; \
(cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]
docker build -t systemd_centos .
docker run --privileged -d -v /sys/fs/cgroup:/sys/fs/cgroup:ro systemd_centos

Ref: Running systemd within a docker container

Save and load docker images​

docker save ubuntu -o ubuntu.tar
docker load -i ubuntu.tar

Update image using docker-compose​

docker-compose pull && docker-compose up -d

Ref: build process - how to get docker-compose to use the latest image from repository - Stack Overflow

Remove unused images​

docker image prune -a

Ref: How to remove old and unused Docker images - Stack Overflow

Access host port from container​

On Linux, add --add-host=host.docker.internal:host-gateway to your Docker command to enable this feature. (See below for Docker Compose configuration.)

Use your internal IP address or connect to the special DNS name host.docker.internal which will resolve to the internal IP address used by the host.

To enable this in Docker Compose on Linux, add the following lines to the container definition:

extra_hosts:
- "host.docker.internal:host-gateway"

Ref: How to access host port from docker container - Stack Overflow

Pull docker image via proxy​

sudo systemctl edit docker
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80/"
Environment="HTTPS_PROXY=http://proxy.example.com:80/"

sudo systemctl restart docker

Ref: Cannot download Docker images behind a proxy - Stack Overflow