[LINUX] Introduction to docker Create ubuntu environment in ubuntu

Purpose

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.

flow

--Install docker --Creating a Dockerfile --Create docker image --Start docker container --Connect to docker container

install docker

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.

Creating a Dockerfile

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 '[email protected]\s*required\s*[email protected] optional [email protected]' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

From docker image creation to login

Create docker image

$ 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

Start docker container

$ 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.

Ssh connection to docker container

$ docker port test 22
0.0.0.0:32768

Ssh for the port displayed in.

$ ssh [email protected] -p 32768
[email protected]'s password:<pass>

Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-47-generic x86_64)
...
[email protected]:~$

I was able to ssh in the container with user hoge and start bash! The rest will be developed in this (should).

reference

https://docs.docker.com/engine/examples/running_ssh_service/ https://qiita.com/techno-tanoC/items/58e9c5c74d90392d9de4

Recommended Posts

Introduction to docker Create ubuntu environment in ubuntu
From Ubuntu 20.04 introduction to environment construction
How to create an NVIDIA Docker environment
[Docker] Create a jupyterLab (python) environment in 3 minutes!
Operate mongoDB from python in ubuntu environment ① Introduction of mongoDB
Linux beginners create Ubuntu 16.04 environment on Docker (for Mac)-first half-
Steps to install Ubuntu in VirtualBox
How to log in to Docker + NGINX
How to debug Dash (Flask) in Docker + VSCode + remote connection environment
I want to create a pipfile and reflect it in docker
Shell script to build pyenv environment on ubuntu in one shot
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
Method to build Python environment in Xcode 6
Using venv in Windows + Docker environment [Python]
Steps to install Python environment on Ubuntu
Create Python + uWSGI + Nginx environment with Docker
To reference environment variables in Python in Blender
How to use tensorflow under docker environment
Introduction to Vectors: Linear Algebra in Python <1>
Introduction to Effectiveness Verification Chapter 1 in Python
Use WebDAV in a Portable Docker environment
I wanted to use jupyter notebook with docker in pip environment (opticspy)
Environment maintenance made with Docker (I want to post-process GrADS in Python
Introduction to Scrapy (1)
Introduction to Scrapy (3)
Introduction to Supervisor
Hello World with gRPC / go in Docker environment
Introduction to Python Let's prepare the development environment
From nothing on Ubuntu 18.04 to setting up a Deep Learning environment in Tensor
Introduction to Tkinter 1: Introduction
How to create a Python virtual environment (venv)
Reintroduction to Docker
[Latest] How to build Java environment on Ubuntu
Install python package in personal environment on Ubuntu
tse --Introduction to Text Stream Editor in Python
Flutter in Docker-How to build and use a Flutter development environment inside a Docker container
I want to create a window in Python
How to create a JSON file in Python
Introduction to PyQt
I wrote "Introduction to Effect Verification" in Python
Introduction to Scrapy (2)
Create Nginx + uWSGI + Python (Django) environment with docker
Super easy! Python + Flask environment in Docker quickly
[Linux] Introduction to Linux
How to create data to put in CNN (Chainer)
[Venv] Create a python virtual environment on Ubuntu
Introduction to Scrapy (4)
[Note] How to create a Ruby development environment
Problems connecting to MySQL from Docker environment (Debian)
Introduction to Python "Re" 1 Building an execution environment
Introduction to discord.py (2)
How to create a Rest Api in Django
How to build Java environment on Ubuntu (Linux)
[TF] How to build Tensorflow in Proxy environment
Introduction to Effectiveness Verification Chapter 2 Written in Python
[Note] How to create a Mac development environment
[For beginners] Introduction to vectorization in machine learning
Introduction to discord.py
Use os.getenv to get environment variables in Python
tensorflow-gpu introduction memo
linux (ubuntu) memo
H2O.ai Introduction memo
Ubuntu18.04 Development environment creation memo
ConoHa VPS (ubuntu 18.04) Initial setting memo
Introduction
From Ubuntu 20.04 introduction to environment construction
Ubuntu Desktop 20.04 development environment construction memo
Alembic (DB schema management tool) introduction memo
Introduction to docker Create ubuntu environment in ubuntu