Examine the evolution of virtualization technologies from bare metal, virtual machines, and containers and the tradeoffs between them.
Explores the three core Linux features that enable containers to function (cgroups, namespaces, and union filesystems), as well as the architecture of the Docker components.
Install and configure Docker Desktop
Use publicly available container images in your developer workflows and learn how about container data persistence.
Building out a realistic microservice application to containerize.
Write and optimize Dockerfiles and build container images for the components of the example web app.
Use container registries such as Dockerhub to share and distribute container images.
Use Docker and Docker Compose to run the containerized application from Module 5.
Learn best practices for container image and container runtime security.
Explore how to use Docker to interact with containers, container images, volumes, and networks.
Add tooling and configuration to enable improved developer experience when working with containers.
•Developer Experience Wishlist
Deploy containerized applications to production using a variety of approaches.
In this lesson we craft the docker run
commands to run our containerized application.
First, we need to build our container images.
Because of the repo structure, we need to pass the -f
flag to tell docker where our Dockerfiles live, and pass a context pointing to the correct subdirectory:
DOCKERCONTEXT_DIR:=../05-example-web-application/
DOCKERFILE_DIR:=../06-building-container-images/
docker build -t client-react-vite -f ${DOCKERFILE_DIR}/client-react/Dockerfile.3 ${DOCKERCONTEXT_DIR}/client-react/
docker build -t client-react-ngnix -f ${DOCKERFILE_DIR}/client-react/Dockerfile.5 ${DOCKERCONTEXT_DIR}/client-react/
docker build -t api-node -f ${DOCKERFILE_DIR}/api-node/Dockerfile.7 ${DOCKERCONTEXT_DIR}/api-node/
docker build -t api-golang -f ${DOCKERFILE_DIR}/api-golang/Dockerfile.6 ${DOCKERCONTEXT_DIR}/api-golang/
Create a new Docker network called my-network:
docker network create my-network
Check that the network was created:
docker network ls
We will work our way forward, starting with the database and ending with our react client.
docker run -d \
--name db \
--network my-network \
-e POSTGRES_PASSWORD=foobarbaz \
-v pgdata:/var/lib/postgresql/data \
-p 5432:5432 \
--restart unless-stopped \
postgres:15.1-alpine
We have seen commands like this to start a PostgeSQL container, but now we are adding:
-d
: to run it in the background--network my-network
: to attach it to the network we created--restart unless-stopped
: to restart the DB if it crashesDATABASE_URL:=postgres://postgres:foobarbaz@db:5432/postgres
docker run -d \
--name api-node \
--network my-network \
-e DATABASE_URL=${DATABASE_URL} \
-p 3000:3000 \
--restart unless-stopped \
api-node
One option worth calling out is:
-e DATABASE_URL=${DATABASE_URL}
: which we use to pass the database credentials to the applicationDATABASE_URL:=postgres://postgres:foobarbaz@db:5432/postgres
docker run -d \
--name api-golang \
--network my-network \
-e DATABASE_URL=${DATABASE_URL} \
-p 8080:8080 \
--restart unless-stopped \
api-golang
You will notice this is nearly identical to the api-node
command except it uses a different container image and publishes a different port.
We will run both the development server with vite
as well as the more production ready nginx
based client:
docker run -d \
--name client-react-vite \
--network my-network \
-v ${PWD}/client-react/vite.config.js:/usr/src/app/vite.config.js \
-p 5173:5173 \
--restart unless-stopped \
client-react-vite
docker run -d \
--name client-react-nginx \
--network my-network \
-p 80:8080 \
--restart unless-stopped \
client-react-ngnix
The options worth noting are:
-v ${PWD}/client-react/vite.config.js:/usr/src/app/vite.config.js
: which mounts a new vite config file in at runtime-p 80:8080
: to publish port 8080 in the container to port 80 on our localhostTo stop and remove all of these containers we can use:
docker stop db api-node api-golang client-react-vite client-react-nginx
docker rm db api-node api-golang client-react-vite client-react-nginx
docker network rm my-network