2

My goal is to setup a very minimalistic Ubuntu container for hosting a server within it. It should be accessible via ssh and have a persistent configuration.

I am currently having this server running within a virtual machine. This should move to Docker now since I would like to reduce complexity and increase maintainability (I have couple of other containers running perfectly).

What I've tried so far:

  • Created a docker-compose.yml & Dockerfile and connected a local folder
  • Got access to the container with "docker container exec -ti bash"
  • Installed things within this machine, configured ssh

My issues are:

  • The build seems to work, even with configuring ssh
  • SSH via remote does not work - logging in via "docker container exec ... bash" works fine
  • Configuring things within the machine, settings up ssh etc works (same commands like in Dockerfile) - SSH via remote works
  • Shutting down (docker-compose down) and starting up (docker-compose up -d) resets everything I did within the machine

Do you have any ideas, or even a working docker-compose file? Searching for this results in thousands of articles how to install Docker on Ubuntu...

THANK YOU so much!

My Dockerfile looks like this

FROM ubuntu:latest

RUN apt-get update
RUN apt-get install openssh-server -y

RUN mkdir /var/run/sshd

RUN echo 'root:pwd' | chpasswd

# SSH allow root login via remote
RUN sed -i 's/#PermitRootLogin/PermitRootLogin/g' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

# RESTART SSH
RUN /etc/init.d/ssh restart

My docker-compose.yml looks like this

version: "3"
services:

  # ubuntu server
  ubuntu_test:
    build: .
    container_name: AAubuntuServerDockerfile
    restart: always
    command: ["sleep","infinity"]
    volumes:
      - './Docker/Data/Ubuntu_Test:/exchange:rw'
    ports:
      - "5555:22"
    network_mode: bridge
1
  • 1
    If it needs an ssh daemon, interactive logins, and "persistent configuration", a virtual machine is technically a better match; I would not try to change anything from the working VM-based setup you have now. A Docker container is more appropriate for something that has a single process and where it makes sense to launch multiple copies of the container from a single immutable base image.
    – David Maze
    Feb 2, 2022 at 22:11

1 Answer 1

0

It would be best if you use persistence. That can be achieved by using volumes - some examples of this can be found:

(this assumes you are staying on the server/computer in question) https://docs.docker.com/engine/reference/builder/#volume

If you are not interested in the official documentation, I feel like this gist is very concise: https://gist.github.com/onlyphantom/0bffc5dcc25a756e247cb526c01072c0

I would start by checking that the volume information you posted above is written in the way you expect.

I took your configuration above and ran it on my Mac OS Montery and Docker Desktop 4.16 and made the following changes, resulting in persistence.

I made a single change here (note that you need to think through each service, "thing" that you want to keep around when it comes back online.) This doesn't fix and persist all changes only those that are connected and in the SSH folder. Hopefully that makes sense.

volumes:
  - sshd:/etc/ssh/
ports:
  - "22:22"
network_mode: bridge 
volumes:   
sshd:

If you are not wanting to stay on the same system and want to move the container, say to another computer/server/friend whatever you will probably want to actually commit the changes to the container and push them which can be done with the following commands:

You need to commit your changes to the container and then run it. (the answer below is not mine, but does a great job of explaining: I lose my data when the container exits)

Try this:

sudo docker pull ubuntu

sudo docker run ubuntu apt-get install -y ping

Then get the container id using this command:

sudo docker ps -l

Commit changes to the container:

sudo docker commit <container_id> iman/ping 

Then run the container:

sudo docker run iman/ping ping www.google.com

This should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.