04 Using Dockerfiles - dchantzis/sfh-docker-in-development-part-1 GitHub Wiki
Source: https://serversforhackers.com/c/div-docker-images
We use a Dockerfile to build a new image. Dockerfiles make creating an image from a container easier (it's code in a file you can commit SCM).
vim Dockerfile
For every change, every instruction that we write, will create a new Image, do the changes and then commits that change. For this reason it is a good practice to have the least amount of instructions, as it makes the process quicker.
Here's the Dockerfile we end up with:
FROM ubuntu:18.04
LABEL maintainer="Chris Fidao"
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& apt-get install -y gnupg tzdata \
&& echo "UTC" > /etc/timezone \
&& dpkg-reconfigure -f noninteractive tzdata
RUN apt-get update \
&& apt-get install -y curl zip unzip git supervisor sqlite3 \
nginx php7.2-fpm php7.2-cli \
php7.2-pgsql php7.2-sqlite3 php7.2-gd \
php7.2-curl php7.2-memcached \
php7.2-imap php7.2-mysql php7.2-mbstring \
php7.2-xml php7.2-zip php7.2-bcmath php7.2-soap \
php7.2-intl php7.2-readline php7.2-xdebug \
php-msgpack php-igbinary \
&& php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
&& mkdir /run/php \
&& apt-get -y autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
We can now remove the unused image:
docker image ls
docker image rm mynginx:latest
We can build the new image like so:
cd php-app/docker/app
docker build -t shippingdocker/app:latest -f ./Dockerfile .
Some notes regarding the above command:
- -t shippingdocker/app:latest The image name and tag
- -f .Dockerfile
.
The context of where everything is, where you want to run stuff. That is not necessarily the directory where the Dockerfile is in, but the directory anything that is referenced in the Dockerfile is relative to. So if the Dockerfile references any files on your local filesystem to pull in the docker image that it makes, that is going to be relative to whatever directory you reference as the context, and that context is the current directory.
For example (first go to the root directory of the project):
cd ./../../
docker build -t shippingdocker/app:latest -f docker/app/Dockerfile docker/app
docker image ls
We can run a Container based on the Image we have just created:
docker run --rm -it shippingdocker/app:latest bash
To show the currently running Containers (processes)
docker ps
To show all containers:
docker ps -a
To stop nginx from running in the background of docker, we need to add the following in the Dockerfile
:
&& echo "daemon off;" >> /etc/nginx/nginx.conf
Then re-build the docker image:
docker bulld -t shippingdocker/app:latest -f docker/app/Dockerfile docker/app
Check the images
docker image ls
To run the Image and push it to the backround we remove the flag -it and add -d, like so:
docker run -d --rm shippingdocker/app:latest nginx
nginx is now runn on the foreground. To check, run the following:
docker ps