I’m sharing some notes taken from the book ‘Docker for Web Developers.’
Without further ado, here are my notes:
Images
- A Docker image is a snapshot of a file and operating system with libraries and application executables. In essence, an image is a recipe or template for creating a container. (In a similar way that some computer languages let you define a reusable class template for instantiatingobjects of the same type.)
- Containers are launched from a single image template configured by a Dockerfile.
Dockerfile
- An image is configured using a Dockerfile. It typically defines:
- a starting base image – usually an operating system
- all necessary installation steps, such as defining environment variables, copying files from the host, running install processes, etc.
- whether the container should attach one or more volumes for data storage
- whether the container should join a network to communicate with others
- which ports (if any) are exposed to localhost on the host
- the application launch command.
- A Dockerfile defines the build steps required to install and execute an application in order to create a ready-to-run image.
- Dockerfile commands run as a root (super) user when the image is created. This is generally safe;
- you could restart a container if something catastrophic occurred
- docker image build -t nodehello .
- docker run -it –rm –name nodehello -p 3000:3000 nodehello
- Docker Compose will use the nodehello image built earlier or initiate a build when that does not exist. You can also instruct Docker Compose to rebuild the image – perhaps after makingchanges to your Dockerfile :
- docker-compose up –build
- ss
Volumes
- Containers do not retain state between restarts. This is generally a good thing; any number of containers can be started from the same base image and each can handle incoming requests regardless of how or when they were launched
- However, some containers – such as databases – absolutely must retain data so Docker provides two storage mechanism types:
- Volumes
- Bind mounts
- Either can map to a directory on the container, Volumes are the recommended way to persist data
- docker volume create data
- docker volume inspect data
Networks
- Any TCP/IP port can be exposed on a container, such as 3306 for MySQL
- This allows the applications on the host to communicate with the database system at localhost:3306 .
- An application running in another container could not communicate with MySQL because localhost resolves to itself
- For this reason, Docker creates a virtual network and assigns each running container a unique IP address. It’s then becomes possible for one container to communicate with another using its address.
Docker Compose
- A single container is launched with a single docker command
- Docker Compose is a tool for managing multiple containers with associated volumes and networks. A single configuration file, normally named docker-compose.yml.
Orchestration
- Containers are portable and reproducible. A single application can be scaled by launching identical containers on the same server, another server, or even a different data center on the other side of the world. (Docker Swarm / Kubernetes)
- Cloud hosts offer their own orchestration solutions, such as AWS Fargate, Microsoft Azure, and Google Cloud. These are often based on Kubernetes but may have custom options or tools.
Launch a MySQL database with Docker
- docker run -it –rm –name mysql -p 3306:3306 –mount « src=mysqldata,target=/var/lib/mysql » -e MYSQL_ROOT_PASSWORD=mysecret mysql
- it creates a container from a specified image (mysql on the last line)
- -it =====> keep a container running after the application ends
- –rm =====> remove the container after is stops
- –name =====> name a container
- -e =====> define an environment variable
- –net =====> connect to specific Docker network
- it creates a container from a specified image (mysql on the last line)
- docker inspect -f ‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ mysql
- Port
- This would make the port available to other containers within the Docker network but not expose it to the host
Connect to a container shell
- docker exec -it mysql bash
- Every Docker container runs an isolated Linux environment. You can connect to its shell and run commands, examine logs, or perform any other activities.
- Remember containers are stateless! Any changes you make will be lost whenever the container is restarted.
- docker container restart adminer mysql
- docker container pause mysql
- docker container unpause mysql
- docker container rm mysql
- docker container prune (remove all stopped containers)
- docker container stop mysql mongodb adminer
- docker system prune ===> All stopped containers, unused networks, and dangling images can be removed
- docker exec -it mysql sh -c « ls / »
Define a Docker network
- When a container is started, it’s assigned an IP address on the default Docker network
- However, this can change on subsequent restarts so it is difficult to communicate between containers using an IP address.
- docker run has –ip and –ip6 options so you can define a fixed IP, but it is generally easier to refer to another container using it’s –name
- docker network create –driver bridgemysqlnet
- docker run -d –rm –name mysql -p 33066:33066 –mount « src=mysqldata,target=/var/lib/mysql » -e MYSQL_ROOT_PASSWORD=mysecret –net mysqlnet mysql
Deleting disk volumes
- docker exec mysql /usr/bin/mysqldump -u root -pmysecret maroc > backup.sql
Full clean start
- docker system prune -a –volumes
WordPress Example
- docker run … –mount « src=wpfiles,target=/var/www/html » …
- docker run …. -v $PWD/wp-content:/var/www/html/wp-content …
Node Application
- In previous chapters, you referenced a specific image: from Docker Hub. In this file, you’re using
- a build: option to create an image from a Dockerfile :
- context: is the relative path to the location of your Dockerfile , and
- dockerfile: is the name of your Dockerfile .
Docker Search Hub
- docker search –limit 5 php
Pull a Docker Hub image
- docker pull node:lts-alpine
Dockerfile Reference
- A Dockerfile is a plain-text file describing the steps to build an image, typically for your own application. To build a Docker image from a Dockerfile in the current directory, enter:
CLEAN UP
- docker-compose down ===> stop the application
- The application’s Docker containers, images, volumes, and networks can be removed with:
- docker-compose rm
- docker volume prune -f
- docker image rm quiz_nodejs quiz_nginx
- Alternatively, you can wipe all Docker data including base images:
- docker system prune -af –volumes