Table of Contents (🔎 Click to expand/collapse)
Docker is an open platform for developing, shipping, and running applications. With Docker, you can separate your applications from your infrastructure and treat your infrastructure like a managed application. Docker helps you ship code faster, test faster, deploy faster, and shorten the cycle between writing code and running code.
Docker does this by combining kernel containerization features with workflows and tooling that helps you manage and deploy your applications.
Docker containers can be directly used in Kubernetes, which allows them to be run in the Kubernetes Engine with ease.
# run a hello world container
$ docker run <image-name> # the container name would be generated randomly
$ docker run <image-name> --name [container-name]
# check the container image pulled from Docker Hub
$ docker images
# check the containers
$ docker ps # running containers
$ docker ps -a # all containers
- Command
docker run <image-name>
makes docker daemon searching for the given image. It will find the image locally or pulled the image from a public registry called Docker Hub. - Command
docker images
shows the docker images pulled from the Docker Hub public registry with the unique Image ID (in SHA256 format). - Command
docker ps
shows containers with the Container ID (a UUID generated by Docker to identify the container) and more metadata about the run.
We need the file named Dockerfile
which instructs the Docker daemon on how to build our Docker Image. For example:
FROM ubuntu:18.04 # creates a layer from the ubuntu:18.04 Docker image
COPY . /app # adds files from your Docker client’s current directory
RUN make /app # builds your application with make
CMD python /app/app.py # specifies what command to run within the container
Let's build the image with docker build
command and --tag
option. The tag is highly recommended when building Docker images for distinguishing.
# build image with tag
$ docker build --tag <image-name>:<image-tag> .
# run containers based on the built image
$ docker run --publish <container-port>:<host-port> --name <container-name> <image-name>:<image-tag>
# stop and remove containers
$ docker stop <container-name> && docker rm <container-name>
- Command
docker run
runs the containers based on the given image- The
--name
flag allows us to name the container if we like. - The
--publish
(-p
) flag with<host-port>:<container-port>
instructs Docker to map the host's port to the container's port. - The
--deatch
(-d
) flag runs containers in the background (not tied to the terminal's session)
- The
- Command
docker stop
anddocker rm
are used to stop and remove the containers. - Command
docker stop
anddocker rm
are used to stop and remove the containers.
# check the logs of containers
$ docker logs <container-id>
# start an interactive Bash session inside the running container
$ docker exec --interactive --tty <container-id> bash
# examine a container's metadata in Docker
$ docker inspect <container-id>
- Command
docker logs
shows the logs of containers.- The
--follow
(-f
) flag follow the log's output as the container is running.
- The
- Command
docker exec
runs a command in running containers.- The
--interactive
(-i
) flag keepsSTDIN
open even if not attached. - The
--tty
(-t
) flag allocates a pseudo-TTY. - Notice
bash
runs in theWORKDIR
directory specified in theDockerfile
.
- The
- Command
docker inspect
returns low-level information on Docker objects.- The
--format
(-f
) flag formats the output using the given template.
- The
We can publish the Docker Images we built to the registries such as Docker Hub, Google Container Registry (gcr) or even a self-hosted one.
# check config of project for finding the project-id
$ gcloud config list project
# tag the images with a registry name (needed for gcr)
$ docker tag node-app:0.2 gcr.io/<project-id>/<image-name>:<image-tag>
# push the image to Google Container Registry (gcr)
$ docker push gcr.io/<project-id>/<image-name>:<image-tag>
- Command
docker push
pushes an image or a repository to the registry.- Google Cloud Platform (GCP) use Google Container Registry (gcr) for storing, managing, and securing our Docker images.
- We need to tag the images in format
[hostname]/[project-id]/[image]:[tag]
.
- How to check the image exists in Google Container Registry (gcr)?
- [Option 1] Navigate to
Navigation Menu
>Container Registry
. - [Option 2] Visit with the url
http://gcr.io/<project-id>/<image-name>
.
- [Option 1] Navigate to