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?.
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:
- FROM: download and uses a base image as reference for the image creation. You can find more images at hub.docker.com.
- RUN: executes a instruction on the base image, it is image specific so you must be aware of the base image configuration in order to run specific commands (eg: you cannot run Linux utilities on Windows based images or apt on non-debian based ones).
- ENV: define environment variables inside the container.
- EXPOSE: allow the final image to expose certain ports to the host. Make sure to only exposed necessary ports.
- COPY: copy files from the host to the image.
- USER: sets the user that will execute command specified by entrypoint.
- ENTRYPOINT: command to be executed by the container on start.
To build the container image, issue the command: docker build -t
[user]/image_name <dockerfile_path>
where:
- -t [user]/image_name: tag which the image will be referred as. User is optional.
- <dockerfile_path>: path where the dockerfile is stored, can use . if on the same folder as the file.
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
- -d: run the docker in daemon mode, detaching it from the current shell session;
- -v: map volumes between host:container;
- -p: map ports between host:container;
- –env: set environment variable inside container (key=value);
- –name: set the executing container name;
- andreanidr/nginx: image tag name as defined on the build step;
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?