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.

FROM node:12.18.3-alpine

WORKDIR /app
COPY ./ /app
RUN npm install
EXPOSE 3000

CMD ["npm", "start"]
Dockerfile setup for React.

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 including alpine and slim which are minified variations of images intended to save space and resources. Other codenames are also avaliable which vary from build to build such as buster, stretch, or jessie.
  • 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 each RUN 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 .).

docker build --tag my-build:1.0 .
Build docker image

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 run my-build:1.0

# pull image from DockerHub
docker run hello-world
Run docker container

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

# Launch docker container in a running container
docker exec -it <container name> <command>
Shell into running docker container

Saving a docker container's state as an image.

# Save running docker container as an image
docker commit <container_id> <new_image>
Save running docker container state as an image