Skip to content

Instantly share code, notes, and snippets.

@ederparaiso
Last active February 24, 2024 09:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ederparaiso/4847aa4e117f2d1e35d14259e6ef8b2d to your computer and use it in GitHub Desktop.
Save ederparaiso/4847aa4e117f2d1e35d14259e6ef8b2d to your computer and use it in GitHub Desktop.
docker-centos7-systemd-sshd

Docker centOS 7 based with systemd enabled and sshd installed.

Build image

docker build --rm -t c7-systemd-sshd .

Run

docker run --rm --privileged=true -p 22:22 -v /sys/fs/cgroup:/sys/fs/cgroup:ro c7-systemd-sshd

Alternatively you can use docker compose

Build

docker-compose build

Run

docker-compose run --service-ports --rm c7-systemd-sshd

In order to connect in the running container using ssh, obtain container_id running docker ps and run

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_id

version: '3'
services:
c7-systemd-sshd:
build: .
ports:
- "22:22"
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
privileged: true
networks:
- c7_sshd_network
networks:
c7_sshd_network:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.10.0.0/16
FROM centos:7
ENV container docker
# remove some files to enable systemd
RUN (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" ]
RUN yum clean all && \
yum -y install openssh-server openssh-clients initscripts
RUN ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
RUN ssh-keygen -f /etc/ssh/ssh_host_ed25519_key -N '' -t ed25519
EXPOSE 22
# setup new root password
RUN echo root:pass | chpasswd
CMD ["/usr/sbin/init"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment