It may not be a major way to use docker, but I would like to create a docker container for ubuntu and enter it for various developments.
It was my first time to touch docker, and I saw various materials, but there were few similar cases, so as a memorandum.
--Install docker --Creating a Dockerfile --Create docker image --Start docker container --Connect to docker container
Follow Official Site
$ sudo apt-get remove docker docker-engine docker.io containerd runc
$ sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo apt-key fingerprint 0EBFCD88
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
$ sudo docker run hello-world
With this, Hello world. At this rate, I couldn't use the docker command without adding sudo, so
$ sudo gpasswd -a <user> docker
This eliminates the need for sudo.
The Dockerfile created this time is as follows.
#Specify the version of ubuntu to use
FROM ubuntu:18.04
#Install the command to use
RUN \
apt update && \
apt -y upgrade && \
apt install -y build-essential && \
apt install -y software-properties-common && \
apt install -y curl git man unzip vim wget sudo
#Creating a user because it is inconvenient to be root
RUN useradd -m hoge
#Grant root authority
RUN gpasswd -a hoge sudo
#Password set to pass
RUN echo 'hoge:pass' | chpasswd
#Set the shell for ssh login to bash
RUN sudo sed -i 's/hoge:x:1000:1000::\/home\/hoge:\/bin\/sh/hoge:x:1000:1000::\/home\/hoge:\/bin\/bash/g' /etc/passwd
#Settings for ssh (see official website)
RUN apt install -y openssh-server
RUN mkdir /var/run/sshd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /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
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
$ docker build . -t myubuntu:18.04
Execute in the directory where the Dockerfile is placed. myubuntu: 18.04 is an arbitrary name. You can check it with the following command.
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu 18.04 iiiiiidddddd 6 minutes ago 496MB
$ docker run -d -P --name test myubuntu:18.04
Create a container from the docker image and start it.
Background execution with -d
,
Connect the ECPOSE port with -P
to the local,
Specify the container name with --name
.
$ docker port test 22
0.0.0.0:32768
Ssh for the port displayed in.
$ ssh hoge@localhost -p 32768
hoge@localhost's password:<pass>
Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-47-generic x86_64)
...
hoge@nyonyonyo:~$
I was able to ssh in the container with user hoge and start bash! The rest will be developed in this (should).
https://docs.docker.com/engine/examples/running_ssh_service/ https://qiita.com/techno-tanoC/items/58e9c5c74d90392d9de4
Recommended Posts