Creating your own docker container images

Container technology is in a all high lately. Although not a new concept, Linux LCX was originally developed in 2008, Docker is a much more common technology that you can know more at: What is a Container?.

containers Photo by CHUTTERSNAP

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. docker.com (2020)

The main unit of a docker image is know as a Dockerfile. This is a text file with the specification of how the image should be build and behave at run time.

LABEL maintainer="doug at andreanidr.com"
FROM ubuntu:latest

RUN apt-get update && apt-get install -y python-software-properties
RUN add-apt-repository ppa:nginx/stable
RUN apt-get update && apt-get install -y nginx
RUN apt-get update && apt-get install -y openssl
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
RUN mkdir /etc/nginx/ssl
ENV SSL_DIR /etc/letsencrypt/live/

RUN mkdir -p /etc/letsencrypt/live/blog.andreanidr.com
RUN mkdir -p /etc/ssl/certs
COPY cert.pem /etc/letsencrypt/live/blog.andreanidr.com
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log

RUN mkdir -p /config/nginx
RUN cp /etc/nginx/nginx.conf /config/nginx

EXPOSE 80
EXPOSE 443

USER nginx

ENTRYPOINT ["nginx", "-c", "/config/nginx/nginx.conf"]

check other options at: docs.docker.com

Each step of the Dockerfile is executed in order to build the image of the container:

To build the container image, issue the command: docker build -t [user]/image_name <dockerfile_path> where:

eg (assume . as current directory): docker build -t andreanidr/nginx .

If finished successfully, the image will be ready for deployment, just execute:

docker run [options] image_tag

eg: docker run -d -v /opt/nginx/:/config/nginx -p 80:80 -p 443:443 --env SSL_PATH=/config/nginx/letsencrypt/live --name nginx-ssl-01 andreanidr/nginx

To check running containers, run: docker ps or docker ps -a to see all containers created (even if stopped/crashed).

Check container logs with docker logs container_name.

Build your own docker is a good way to understand how the overall application works. You will be able to start troubleshoot and fix issues that will occur even if you use other person image (there are tons on dockerhub.com) and expand on them or create your own images if a pre-existing didn’t exist yet.

What do you think about container technology? Does it help you streamline your workflow? Would you consider changing to an all container based desktop like Jess Franzelle?