03 Docker Images - dchantzis/sfh-docker-in-development-part-1 GitHub Wiki
Source: https://serversforhackers.com/c/div-docker-images
Goals
- Run a dev environment in Docker, with the following containers:
- Nginx/PHP
- MySQL
- Redis Every Container from the above 3, will be in its own separate directory upder ./docker/app/
- Build a
Dockerfile
to build an nginx/php image - Since we're not customising the installation for MySQL and Redis, we can use the official images for MySQL/Redis.
Create a new project directory, named "php-app":
mkdir php-api
cd php-api
mkdir docker
mkdir docker/app
Create the Dockerfile
:
cd docker/app
vim Dockerfile
Add the following (to find the latest official ubuntu releases, go to https://hub.docker.com/_/ubuntu)
FROM ubuntu:18.04
Exit vim and run:
docker run --rm -it ubuntu:18.04 bash
Options:
- --rm - remove it once stop running it
- -it - make is interactive
- ubuntu:18:04 - the instance we are running
- bash - to make it look like we are inside the container
Then run:
apt-get update
apt-get install -y nginx
To check if nginx was installed:
which nginx
Then we can inspect the Container to see the changes. Open a new terminal window and check the running processes (from outside the Container):
docker ps
get the Container ID of the running process and then:
docker diff [CONTAINER_ID]
This shows all the changes to the container (files added, modified, deleted). We can then commit thos echanges to make a new Image from that Container and its changes:
docker commit -a "Dimitris Hantzis" -m "Installed Nginx" [CONTAINER_ID] [REPOSITORY_NAME:TAG]
This way we commit changes made to the running container:
- -a Author string
- -m The commit message
- [CONTAINER_ID] We give it the container from which to make a new image
- [REPOSITORY_NAME:TAG] We give the image a name of mynginx and tag is latest, example: mynginx:latest
The commit has created a new image with the changes from the running Container Check the current Images:
docker ls
Now instead of creating a new image we can run the newly-created one, like so:
docker run --rm -it mynginx:latest bash
And that is the process of what happens inside of a Dockerfile
.
For every change in the Dockerfile
-- every added line -- it will create a
new Container, make a change, commit that change and make a new Image off of it.
At the end we get a final Image that we can use.
Every intermediary container or image that gets created, will get destroyed automatically, it gets cleaned out. We just get the last image of the completed work as a result.