Debian 10 Buster
Sponsored Link

Docker : Use Dockerfile2019/07/24

 
Use Dockerfile and create Docker images automatically.
It is also useful for configuration management.
[1] For example, Create a Dockerfile to install apache2 and sshd and also install Supervisor to control multiple services on a Container.
root@dlp:~#
vi Dockerfile
# create new

FROM debian
MAINTAINER ServerWorld <admin@srv.world>

RUN apt-get update
RUN apt-get -y install openssh-server apache2 supervisor
RUN echo "Hello DockerFile World" > /var/www/html/index.html
RUN mkdir /var/run/sshd; chmod 755 /var/run/sshd
RUN mkdir /root/.ssh; chown root. /root/.ssh; chmod 700 /root/.ssh
RUN ssh-keygen -A

ADD supervisord.conf /etc/supervisor/supervisord.conf
ADD .ssh/id_rsa.pub /root/.ssh/authorized_keys

EXPOSE 22 80
CMD ["/usr/bin/supervisord"]

# create a Supervisor template

root@dlp:~#
vi supervisord.conf
[supervisord]
nodaemon=true

[program:sshd]
command=/usr/sbin/sshd -D
autostart=true
autorestart=true

[program:apache2]
command=/usr/sbin/apachectl -D FOREGROUND
autostart=true
autorestart=true

# generate SSH key

root@dlp:~#
ssh-keygen -q -N "" -f /root/.ssh/id_rsa
# build image ⇒ docker build -t [image name]:[tag] ./

root@dlp:~#
docker build -t web_server:latest ./

Sending build context to Docker daemon  25.09kB
Step 1/12 : FROM debian
 ---> 00bf7fdd8baf
Step 2/12 : MAINTAINER ServerWorld <admin@srv.world>
 ---> Running in 464a037bb269
Removing intermediate container 464a037bb269
 ---> da7062920d80
Step 3/12 : RUN apt-get update
 ---> Running in a5f6c9129047
.....
.....
Successfully built 42e95a62ed8e
Successfully tagged web_server:latest

root@dlp:~#
docker images

REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
web_server                 latest              42e95a62ed8e        43 seconds ago      300MB
srv.world/debian_apache2   latest              4295df3e5c82        6 minutes ago       243MB
debian                     latest              00bf7fdd8baf        2 weeks ago         114MB

# run Container on background

root@dlp:~#
docker run -d -p 2022:22 -p 8081:80 web_server

ad9085a4054fa94f0a8b0ecb44eb01311e17af2bb289fc2b7d5b1ff08de57648
root@dlp:~#
docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                        NAMES
ad9085a4054f        web_server          "/usr/bin/supervisord"   27 seconds ago      Up 26 seconds       0.0.0.0:2022->22/tcp, 0.0.0.0:8081->80/tcp   distracted_visvesvaraya

# verify accesses

root@dlp:~#
curl localhost:8081

Hello DockerFile World
root@dlp:~#
ssh -p 2022 localhost /bin/hostname

The authenticity of host '[localhost]:2022 ([::1]:2022)' can't be established.
ECDSA key fingerprint is SHA256:LZCF9x1nEZn442WB4WUMz/Lz0WQ90b4wVeXb2fqQ/os.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:2022' (ECDSA) to the list of known hosts.
ad9085a4054f
The format of Dockerfile is [INSTRUCTION arguments] .
Refer to the following description for INSTRUCTION.
INSTRUCTION Description
FROM iIt sets the Base Image for subsequent instructions.
MAINTAINER It sets the Author field of the generated images.
RUN It will execute any commands when Docker image will be created.
CMD It will execute any commands when Docker container will be executed.
ENTRYPOINT It will execute any commands when Docker container will be executed.
LABEL It adds metadata to an image.
EXPOSE It informs Docker that the container will listen on the specified network ports at runtime.
ENV It sets the environment variable.
ADD It copies new files, directories or remote file URLs.
COPY It copies new files or directories.
The differences of [ADD] are that it's impossible to specify remore URL and also it will not extract archive files automatically.
VOLUME It creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers
USER It sets the user name or UID.
WORKDIR It sets the working directory.

Matched Content