Fedora 30
Sponsored Link

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

FROM fedora
MAINTAINER ServerWorld <admin@srv.world>

RUN dnf -y install openssh-server httpd supervisor
RUN echo "Hello DockerFile World" > /var/www/html/index.html
RUN mkdir /root/.ssh; chown root. /root/.ssh; chmod 700 /root/.ssh
RUN ssh-keygen -A

ADD supervisord.conf /etc/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:httpd]
command=/usr/sbin/httpd -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  16.9 kB
Step 1/10 : FROM fedora
 ---> d09302f77cfc
Step 2/10 : MAINTAINER ServerWorld <admin@srv.world>
 ---> Running in 8cfcdeae2629
 ---> f59ff4ea81ea
Removing intermediate container 8cfcdeae2629
Step 3/10 : RUN dnf -y install openssh-server httpd supervisor
 ---> Running in 05f68672bad9
.....
.....
Removing intermediate container bce86fcefc05
Successfully built 405d738492e1

[root@dlp ~]#
docker images

REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
web_server               latest              405d738492e1        13 seconds ago      566 MB
srv.world/fedora_httpd   latest              3e633f065d3b        6 minutes ago       595 MB
docker.io/fedora         latest              d09302f77cfc        8 weeks ago         275 MB

# run Container on background

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

[root@dlp ~]#
docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                        NAMES
79be1913163b        web_server          "/usr/bin/supervisord"   7 seconds ago       Up 5 seconds        0.0.0.0:2022->22/tcp, 0.0.0.0:8081->80/tcp   stoic_shockley

# verify accesses

[root@dlp ~]#
curl localhost:8081

Hello DockerFile World
[root@dlp ~]#
ssh -p 2022 localhost /usr/bin/uname -a

Linux 79be1913163b 5.0.10-300.fc30.x86_64 #1 SMP Tue Apr 30 16:22:12 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
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