Docker Introduction
Docker
Setting up a Dockerfile
defines and sets up a container to run by Docker. The Dockerfile
below creates a container for building a React application.
The documentation to build a Dockerfile
is found here (getting-started here). Some of the important definitions to build a Dockerfile
are listed below:
FROM
: Pull a base container as the initial image. Images are pulled from DockerHub which traditionally has a variety of versions includingalpine
andslim
which are minified variations of images intended to save space and resources. Other codenames are also avaliable which vary from build to build such asbuster
,stretch
, orjessie
.WORKDIR
: Location to define as the working directory.COPY
: Copy files and directories from the host machine to the container.RUN
: Commands to execute within the container to setup the application, including install/dependencies and initial configuration. An intermediate image is created to store the result from eachRUN
command, in this case, it is best to chain commands together to avoid generating additional intermediate images.EXPOSE
: Expose a port number from the application to the host, ports are able to be remapped to different values to the host.CMD
: The command to start the application.
The docker container is an isolated system which is separate from the host. The host is able to access the container explictly through several methods of communication.
Building an Image and Running a Container
To build an image, the following command is used to construct an image from a Dockerfile
. The command will build an image with a defined tag with the Dockerfile
within the current directory (as pointed by .
).
To run an image in a docker container, the following command start a new container from a built docker image. If the image is not built locally, an attempt to pull the image from DockerHub will be performed. An example "hello world" is avaliable to test for a proper docker installation.
Docker Compose
Docker compose is an orchestration method to allow multiple docker images to interact with each other seamlessly. This can be particularly useful for applications interacting with databases and/or microservices. Docker compose ochestration is defined within a YAML file named docker-compose.yml
.
Docker Swarm
Docker swarm builds upon the docker-compose
construct by utilizing the YAML file upon a cluster of machines as a Docker Swarm setup. A useful resource to getting started using Docker Swarm can be found here: https://dockerswarm.rocks/ (assuming a basic understanding of Dockerfiles and Docker Compose)
Debugging Docker
Debugging a container or image can be done using the following
Saving a docker container's state as an image.