This file contains notes and code examples for using Docker.
I am a big fan of Academind on uDemy and YouTube. Their classes on eDemy are content rich and take you from noob to expert. Maximillian works from hands-on examples that you will actually use in IT. I highly recommend purchasing his course on Docker https://www.udemy.com/course/docker-kubernetes-the-practical-guide/.
Images are the template (instructions) that define the parameters of the container. The image is used to build containers and a single image can build many containers.
### Containers
Containers are packages that contain the application and the environment in which the application runs.
This command will create and run a container instance from the image. In this example it is using the office nodejs image to create a running instance in a container. Note: this command as it is doesn't do much.
`docker run -it node`
Adding the -it flag creates an interactive terminal instance where you can interact with the instance of node.
### Running a container from your own image
After the image is built you will get an ID for the image. Use the ID to start the container. If you are a good dev, then you assigned a name:tag and you use that instead.
Volumes store persistent data that survives after a container shuts down. These are folders on the host machine that are mounted in the container.
Volumes are managed by Docker. This means that we do not know where the volume is located and have no ability to manage them. Bind mounts solve that problem.
### Add a volume to the container
This command creates an anonymous volume. Anonymous volumes are not persisted when the container is removed.
`VOLUME ["{path}"]`
`VOLUME ["app/feedback"]`
Named volumes persist data when the container is removed. Named volumes are NOT defined in the Dockerfile, rather it is done at the command line when running the container.
Like named volumes but we have management control over the file system. Bind mounts are not created in the Dockerfile. Bind mounts are created from terminal.
A bind mount is created by adding a second -v command. Should use quotes to protect the absolute path.
`-v "{absolute path on host file system}:{mount location in container}"`
If you mount a volume to the app folder in the way that this command above does, then the mount will overwrite everything that the COPY and RUN commands do in the Dockerfile. This command is overwriting the container folder with the contents of the local host folder. Since the local host does not have the same npm dependencies as the container then we get an issue.
The solution is to use an anonymous volume. In the following command the folder that is defined in the anon volume is protected.
🛸 Why do this? Now we can make changes to the HTML files and we do NOT have to rebuild the container.
### Read Only Volume
When using a Bind Mount, the local drive that is mapped to the container is writable by the container. Since we probably don't want the container writing back to our source-code marking the volume as read-only is a good idea. Do this by adding :ro to the end of the volume declaration.
You can add volume declarations after the bind-mount to make subfolders writable if they need it. Use anonymous folders to do this. In the above example, all of app is read only but node_modules can be written by the container.
## References
[Docker & Kubernetes: The Practical Guide](https://www.udemy.com/course/docker-kubernetes-the-practical-guide/)